-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeometry.py
331 lines (242 loc) · 8.87 KB
/
Geometry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File: Geometry.py
# Description:
# Student Name: Nicholas Webb
# Student UT EID: nw6887
# Partner Name: EJ Porras
# Partner UT EID:
# Course Name: CS 313E
# Unique Number:
# Date Created:
# Date Last Modified:
import math
import re
class Point (object):
# constructor with default values
def __init__ (self, x = 0, y = 0, z = 0):
self.x = x
self.y = y
self.z = z
# create a string representation of a Point
# returns a string of the form (x, y, z)
def __str__ (self):
return "(" + self.x + ", " + self.y + ", " + self.z + ")"
def is_equal (self, a, b):
tol = 1.0e-6
return (abs(a-b) < tol )
# get distance to another Point object
# other is a Point object
# returns the distance as a floating point number
def distance (self, other):
math.sqrt((self.x - other.x)**2 - (self.y - other.y)**2 + (self.z - other.z)**2)
# test for equality between two points
# other is a Point object
# returns a Boolean
def __eq__ (self, other):
if (self.is_equal(self.x, other.x) and self.is_equal(self.y, other.y)):
return True
else:
return False
class Sphere (object):
# constructor with default values
def __init__ (self, x = 0, y = 0, z = 0, radius = 1):
self.x = x
self.y = y
self.z = z
self.radius = radius
self.center = Point(x, y, z)
# returns string representation of a Sphere of the form:
# Center: (x, y, z), Radius: value
def __str__ (self):
return "(" + self.x + ", " + self.y + ", " + self.z + ")"
# compute surface area of Sphere
# returns a floating point number
def area (self):
return 4*math.pi()*self.radius
# compute volume of a Sphere
# returns a floating point number
def volume (self):
return (4/3)*math.pi()*(self.radius)**3
# determines if a Point is strictly inside the Sphere
# p is Point object
# returns a Boolean
def is_inside_point (self, p):
if p.x < self.x and p.y < self.y and p.z < self.z:
return True
else:
return False
# determine if another Sphere is strictly inside this Sphere
# other is a Sphere object
# returns a Boolean
def is_inside_sphere (self, other):
if other.radius < self.radius:
if other.x < self.x and other.y < self.y and other.z < self.z:
return True
else:
return False
else:
return False
# determine if a Cube is strictly inside this Sphere
# determine if the eight corners of the Cube are strictly
# inside the Sphere
# a_cube is a Cube object
# returns a Boolean
def is_inside_cube (self, a_cube):
cube_verticies = []
half_points = [(-.5, .5, .5),
(-.5, -.5, .5), (-.5, -.5, -.5), (.5, -.5, .5), (.5, .5, -.5), (.5, -.5, -.5), (.5, -.5, -.5), (.5, .5, .5)]
for half_points in half_points:
cube_verticies = cube_verticies.append(Point(a_cube.x + (half_points[0] * a_cube.side), a_cube.y + (half_points[1] * a_cube.side), a_cube.z + (half_points[2] * a_cube.side)))
for i in range(0,8):
if self.center.distance(half_points[i]) <= self.radius:
return True
return False
# determine if a Cylinder is strictly inside this Sphere
# a_cyl is a Cylinder object
# returns a Boolean
def is_inside_cyl (self, a_cyl):
# determine if another Sphere intersects this Sphere
# other is a Sphere object
# two spheres intersect if they are not strictly inside
# or not strictly outside each other
# returns a Boolean
def does_intersect_sphere (self, other):
# determine if a Cube intersects this Sphere
# the Cube and Sphere intersect if they are not
# strictly inside or not strictly outside the other
# a_cube is a Cube object
# returns a Boolean
def does_intersect_cube (self, a_cube):
# return the largest Cube object that is circumscribed
# by this Sphere
# all eight corners of the Cube are on the Sphere
# returns a Cube object
def circumscribe_cube (self):
class Cube (object):
# Cube is defined by its center (which is a Point object)
# and side. The faces of the Cube are parallel to x-y, y-z,
# and x-z planes.
def __init__ (self, x = 0, y = 0, z = 0, side = 1):
# string representation of a Cube of the form:
# Center: (x, y, z), Side: value
def __str__ (self):
# compute the total surface area of Cube (all 6 sides)
# returns a floating point number
def area (self):
# compute volume of a Cube
# returns a floating point number
def volume (self):
# determines if a Point is strictly inside this Cube
# p is a point object
# returns a Boolean
def is_inside_point (self, p):
# determine if a Sphere is strictly inside this Cube
# a_sphere is a Sphere object
# returns a Boolean
def is_inside_sphere (self, a_sphere):
# determine if another Cube is strictly inside this Cube
# other is a Cube object
# returns a Boolean
def is_inside_cube (self, other):
# determine if a Cylinder is strictly inside this Cube
# a_cyl is a Cylinder object
# returns a Boolean
def is_inside_cylinder (self, a_cyl):
# determine if another Cube intersects this Cube
# two Cube objects intersect if they are not strictly
# inside and not strictly outside each other
# other is a Cube object
# returns a Boolean
def does_intersect_cube (self, other):
# determine the volume of intersection if this Cube
# intersects with another Cube
# other is a Cube object
# returns a floating point number
def intersection_volume (self, other):
# return the largest Sphere object that is inscribed
# by this Cube
# Sphere object is inside the Cube and the faces of the
# Cube are tangential planes of the Sphere
# returns a Sphere object
def inscribe_sphere (self):
class Cylinder (object):
# Cylinder is defined by its center (which is a Point object),
# radius and height. The main axis of the Cylinder is along the
# z-axis and height is measured along this axis
def __init__ (self, x = 0, y = 0, z = 0, radius = 1, height = 1):
# returns a string representation of a Cylinder of the form:
# Center: (x, y, z), Radius: value, Height: value
def __str__ (self):
# compute surface area of Cylinder
# returns a floating point number
def area (self):
# compute volume of a Cylinder
# returns a floating point number
def volume (self):
# determine if a Point is strictly inside this Cylinder
# p is a Point object
# returns a Boolean
def is_inside_point (self, p):
# determine if a Sphere is strictly inside this Cylinder
# a_sphere is a Sphere object
# returns a Boolean
def is_inside_sphere (self, a_sphere):
# determine if a Cube is strictly inside this Cylinder
# determine if all eight corners of the Cube are inside
# the Cylinder
# a_cube is a Cube object
# returns a Boolean
def is_inside_cube (self, a_cube):
# determine if another Cylinder is strictly inside this Cylinder
# other is Cylinder object
# returns a Boolean
def is_inside_cylinder (self, other):
# determine if another Cylinder intersects this Cylinder
# two Cylinder object intersect if they are not strictly
# inside and not strictly outside each other
# other is a Cylinder object
# returns a Boolean
def does_intersect_cylinder (self, other):
def main():
# read data from standard input
# read the coordinates of the first Point p
# create a Point object
# read the coordinates of the second Point q
# create a Point object
# read the coordinates of the center and radius of sphereA
# create a Sphere object
# read the coordinates of the center and radius of sphereB
# create a Sphere object
# read the coordinates of the center and side of cubeA
# create a Cube object
# read the coordinates of the center and side of cubeB
# create a Cube object
# read the coordinates of the center, radius and height of cylA
# create a Cylinder object
# read the coordinates of the center, radius and height of cylB
# create a Cylinder object
# print if the distance of p from the origin is greater
# than the distance of q from the origin
# print if Point p is inside sphereA
# print if sphereB is inside sphereA
# print if cubeA is inside sphereA
# print if cylA is inside sphereA
# print if sphereA intersects with sphereB
# print if cubeB intersects with sphereB
# print if the volume of the largest Cube that is circumscribed
# by sphereA is greater than the volume of cylA
# print if Point p is inside cubeA
# print if sphereA is inside cubeA
# print if cubeB is inside cubeA
# print if cylA is inside cubeA
# print if cubeA intersects with cubeB
# print if the intersection volume of cubeA and cubeB
# is greater than the volume of sphereA
# print if the surface area of the largest Sphere object inscribed
# by cubeA is greater than the surface area of cylA
# print if Point p is inside cylA
# print if sphereA is inside cylA
# print if cubeA is inside cylA
# print if cylB is inside cylA
# print if cylB intersects with cylA
if __name__ == "__main__":
main()