-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.py
More file actions
774 lines (550 loc) · 26.9 KB
/
Geometry.py
File metadata and controls
774 lines (550 loc) · 26.9 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
import math
import sys
import unittest
class Point (object):
#constructor
def __init__ (self, x = 0, y = 0, z = 0):
self.x = x
self.y = y
self.z = z
# get dist to another point object
def distance(self, other):
#distance formula
return math.sqrt(((self.x - other.x)**2)+ ((self.y - other.y)**2) + ((self.z - other.z)**2))
#string representation of a Point object
def __str__ (self):
return '(' + str(float(self.x)) + ', ' + str(float(self.y)) + ', '+ str(float(self.z))+ ')'
#test for equality
def __eq__ (self,other):
tol = 1.0e-6
return ((abs(self.x - other.x))< tol) and ((abs(self.y - other.y))< tol) and ((abs(self.z - other.z))< tol)
class Sphere (object):
# constructor with default values
def __init__ (self, x = 0, y = 0, z = 0, radius = 1):
self.center = Point(x,y,z)
self.x = x
self.y = y
self.z = z
self.radius = radius
# returns string representation of a Sphere of the form:
# Center: (x, y, z), Radius: value
def __str__ (self):
return 'Center: '+ self.center.__str__()+', '+ 'Radius: '+ str(float(self.radius))
# compute surface area of Sphere
# returns a floating point number
def area (self):
return 4*math.pi*(self.radius**2)
# 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):
return self.center.distance(p)<self.radius
# determine if another Sphere is strictly inside this Sphere
# other is a Sphere object
# returns a Boolean
def is_inside_sphere (self, other):
return (self.center.distance(other.center) + other.radius) <self.radius
# determine if another Sphere is strictly outside this Sphere
# other is a Sphere object
# returns a Boolean
def is_outside_sphere (self, other):
return (self.center.distance(other.center) - other.radius) >self.radius
# 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):
yes = True
x = a_cube.x
y = a_cube.y
z = a_cube.z
half_s = (a_cube.side/2)
p = [Point(x+half_s, y+half_s, z+half_s), #top left corner of upper face of other cube
Point(x-half_s, y+half_s, z+half_s), #top right corner of upper face of other cube
Point(x-half_s, y-half_s, z+half_s), # bottom left corner of upper face of other cube
Point(x+half_s, y-half_s, z+half_s), # bottom right corner of upper face of other cube
Point(x-half_s, y+half_s, z-half_s), #top left corner of lower face of other cube
Point(x+half_s, y-half_s, z-half_s), #bottom right corner of lower face of other cube
Point(x-half_s, y-half_s, z-half_s), #bottom left corner of lower face of other cube
Point(x+half_s, y+half_s, z-half_s)] # top right corner of lower face of other cube
#loops through all corners and invokes is_inside_point method for each one to check if all are in sphere
for point in p:
if (not self.is_inside_point(point)):
yes = False
return yes
# determine if a Cube is strictly outside this Sphere
# determine if the eight corners of the Cube are strictly
# outside the Sphere
# a_cube is a Cube object
# returns a Boolean
def is_outside_cube (self, a_cube):
yes = True
x = a_cube.x
y = a_cube.y
z = a_cube.z
half_s = (a_cube.side/2)
p = [Point(x+half_s, y+half_s, z+half_s), #top left corner of upper face of other cube
Point(x-half_s, y+half_s, z+half_s), #top right corner of upper face of other cube
Point(x-half_s, y-half_s, z+half_s), # bottom left corner of upper face of other cube
Point(x+half_s, y-half_s, z+half_s), # bottom right corner of upper face of other cube
Point(x-half_s, y+half_s, z-half_s), #top left corner of lower face of other cube
Point(x+half_s, y-half_s, z-half_s), #bottom right corner of lower face of other cube
Point(x-half_s, y-half_s, z-half_s), #bottom left corner of lower face of other cube
Point(x+half_s, y+half_s, z-half_s)] # top right corner of lower face of other cube
#loops through all corners and invokes opposite of is_inside_point method for each one to check if all are not in sphere
for point in p:
if (self.is_inside_point(point)):
yes = False
return yes
# 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):
yes = True
#fills list with all extreme points of cylinder
p = [Point(a_cyl.x+a_cyl.radius, a_cyl.y, a_cyl.z-(a_cyl.height-2)), #bottom east point
Point(a_cyl.x, a_cyl.y+a_cyl.radius, a_cyl.z-(a_cyl.height-2)), # bottom north point
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom north east
Point(a_cyl.x-a_cyl.radius, a_cyl.y, a_cyl.z-(a_cyl.height-2)), # bottom west point
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom north west
Point(a_cyl.x, a_cyl.y-a_cyl.radius, a_cyl.z-(a_cyl.height-2)), # bottom south point
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom south west
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom south east
Point(a_cyl.x+a_cyl.radius,a_cyl.y,a_cyl.z+(a_cyl.height-2)),
Point(a_cyl.x,a_cyl.y+a_cyl.radius,a_cyl.z+(a_cyl.height-2)),
Point(a_cyl.x-a_cyl.radius,a_cyl.y,a_cyl.z+(a_cyl.height-2)),
Point(a_cyl.x,a_cyl.y-a_cyl.radius,a_cyl.z+(a_cyl.height-2)),
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)), #top north east
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)), #top north west
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)), #top south west
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)) #top south east
]
#loops through list of extreme points, and sees if each one is within the sphere
for extreme in p:
if (not self.is_inside_point(extreme)):
yes = False
return yes
# 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):
return (not self.is_inside_sphere(other)) and (not self.is_outside_sphere(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):
#returns boolean True if both methods are False
return (not self.is_inside_cube(a_cube)) and (not self.is_outside_cube(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):
#returns a cube with diagonal equal to the radius of the sphere.
return Cube(self.x,self.y,self.z,((2*self.radius)/math.sqrt(3)))
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):
self.center = Point(x,y,z)
self.x = float(x)
self.y = float(y)
self.z = float(z)
self.side = side
# string representation of a Cube of the form:
# Center: (x, y, z), Side: value
def __str__ (self):
return 'Center: '+ self.center.__str__()+', '+ 'Side: '+ str(float(self.side))
# compute the total surface area of Cube (all 6 sides)
# returns a floating point number
def area (self):
return 6*(self.side**2)
# compute volume of a Cube
# returns a floating point number
def volume (self):
return (self.side**3)
# determines if a Point is strictly inside this Cube
# p is a point object
# returns a Boolean
def is_inside_point (self, p):
x_cen = self.x
y_cen = self.y
z_cen = self.z
half_s = (self.side/2)
#defines critical points of the cube
xmax,xmin = x_cen+half_s, x_cen-half_s
ymax,ymin = y_cen+half_s, y_cen-half_s
zmax,zmin = z_cen+half_s, z_cen-half_s
px = p.x
py = p.y
pz = p.z
#returns true if all points of P lie within the cube's critical points
return ((xmin<px<xmax) and (ymin<py<ymax) and (zmin<pz<zmax))
# 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):
yes = True
sx_cen = a_sphere.x
sy_cen = a_sphere.y
sz_cen = a_sphere.z
r = a_sphere.radius
#creates list of all extreme points in sphere
extremes = [Point(sx_cen, sy_cen + r, sz_cen),
Point(sx_cen, sy_cen - r, sz_cen),
Point(sx_cen - r, sy_cen, sz_cen),
Point(sx_cen + r , sy_cen, sz_cen),
Point(sx_cen,sy_cen, sz_cen +r),
Point(sx_cen, sy_cen, sz_cen - r)]
#loops through list to see if any of the extremes are outside the cube
for point in extremes:
if (not self.is_inside_point(point)):
yes = False
return yes
# determine if another Cube is strictly inside this Cube
# other is a Cube object
# returns a Boolean
def is_inside_cube (self, other):
yes = True
x = other.x
y = other.y
z = other.z
half_s = (other.side/2)
p = [Point(x+half_s, y+half_s, z+half_s), #top left corner of upper face of other cube
Point(x-half_s, y+half_s, z+half_s), #top right corner of upper face of other cube
Point(x-half_s, y-half_s, z+half_s), # bottom left corner of upper face of other cube
Point(x+half_s, y-half_s, z+half_s), # bottom right corner of upper face of other cube
Point(x-half_s, y+half_s, z-half_s), #top left corner of lower face of other cube
Point(x+half_s, y-half_s, z-half_s), #bottom right corner of lower face of other cube
Point(x-half_s, y-half_s, z-half_s), #bottom left corner of lower face of other cube
Point(x+half_s, y+half_s, z-half_s)] # top right corner of lower face of other cube
#loops through list to see if any of the extremes are outside the cube
for point in p:
if (not self.is_inside_point(point)):
yes = False
return yes
# determine if another Cube is strictly outside this Cube
# other is a Cube object
# returns a Boolean
def is_outside_cube (self, other):
yes = True
x = other.x
y = other.y
z = other.z
half_s = (other.side/2)
p = [Point(x+half_s, y+half_s, z+half_s), #top left corner of upper face of other cube
Point(x-half_s, y+half_s, z+half_s), #top right corner of upper face of other cube
Point(x-half_s, y-half_s, z+half_s), # bottom left corner of upper face of other cube
Point(x+half_s, y-half_s, z+half_s), # bottom right corner of upper face of other cube
Point(x-half_s, y+half_s, z-half_s), #top left corner of lower face of other cube
Point(x+half_s, y-half_s, z-half_s), #bottom right corner of lower face of other cube
Point(x-half_s, y-half_s, z-half_s), #bottom left corner of lower face of other cube
Point(x+half_s, y+half_s, z-half_s)] # top right corner of lower face of other cube
#loops through list to see if any of the extremes are inside the cube
for point in p:
if self.is_inside_point(point):
yes = False
return yes
# 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):
yes = True
#fills list with all extreme points of cylinder
p = [Point(a_cyl.x+a_cyl.radius, a_cyl.y, a_cyl.z-(a_cyl.height-2)), #bottom east point
Point(a_cyl.x, a_cyl.y+a_cyl.radius, a_cyl.z-(a_cyl.height-2)), # bottom north point
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom north east
Point(a_cyl.x-a_cyl.radius, a_cyl.y, a_cyl.z-(a_cyl.height-2)), # bottom west point
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom north west
Point(a_cyl.x, a_cyl.y-a_cyl.radius, a_cyl.z-(a_cyl.height-2)), # bottom south point
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom south west
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z-(a_cyl.height-2)), #bottom south east
Point(a_cyl.x+a_cyl.radius,a_cyl.y,a_cyl.z+(a_cyl.height-2)),
Point(a_cyl.x,a_cyl.y+a_cyl.radius,a_cyl.z+(a_cyl.height-2)),
Point(a_cyl.x-a_cyl.radius,a_cyl.y,a_cyl.z+(a_cyl.height-2)),
Point(a_cyl.x,a_cyl.y-a_cyl.radius,a_cyl.z+(a_cyl.height-2)),
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)), #top north east
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y+a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)), #top north west
Point(((a_cyl.x-a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)), #top south west
Point(((a_cyl.x+a_cyl.radius+a_cyl.x)/2)+(a_cyl.radius/2), ((a_cyl.y-a_cyl.radius+a_cyl.y)/2)+(a_cyl.radius/2),a_cyl.z+(a_cyl.height-2)) #top south east
]
#loops through list of extreme points, and sees if each one is within the sphere
for extreme in p:
if (not self.is_inside_point(extreme)):
yes = False
return yes
# 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):
return ((not self.is_outside_cube(other)) and (not self.is_inside_cube(other))) or ((not other.is_outside_cube(self)) and (not other.is_inside_cube(self)))
# 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):
volume = 0.0
if self.does_intersect_cube(other):
inter_lenx = 0
inter_leny = 0
inter_lenz = 0
#sets critical values of cube a (self)
A_zmax = self.z+(self.side/2)
A_zmin = self.z-(self.side/2)
A_xmax = self.x+(self.side/2)
A_xmin = self.x-(self.side/2)
A_ymax = self.y+(self.side/2)
A_ymin = self.y-(self.side/2)
#sets critical values of cube b (other)
B_zmax = other.z+(other.side/2)
B_zmin = other.z-(other.side/2)
B_xmax = other.x+(other.side/2)
B_xmin = other.x-(other.side/2)
B_ymax = other.y+(other.side/2)
B_ymin = other.y-(other.side/2)
#compares critical values in all axises to determine dimensions of intersectional volume
if B_xmin<A_xmax<B_xmax:
inter_lenx = A_xmax-B_xmin
if A_xmin<B_xmax<A_xmax:
inter_lenx = B_xmax-A_xmin
if B_zmin<A_zmax<B_zmax:
inter_lenz = A_zmax-B_zmin
if A_zmin<B_zmax<A_zmax:
inter_lenz = B_zmax-A_zmin
if B_ymin<A_ymax<B_ymax:
inter_leny = A_ymax-B_ymin
if A_ymin<B_ymax<A_ymax:
inter_leny = B_ymax-A_ymin
#computes volume given intersectional dimensions
volume = (inter_lenx)*(inter_leny)*(inter_lenz)
return volume
# 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):
return Sphere(self.x,self.y,self.z, (self.side/2))
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):
self.center = Point(x,y,z)
self.x = x
self.y = y
self.z = z
self.radius = radius
self.height = height
# returns a string representation of a Cylinder of the form:
# Center: (x, y, z), Radius: value, Height: value
def __str__ (self):
return 'Center: '+ self.center.__str__()+', '+ 'Radius: '+ str(float(self.radius))+', '+ 'Height: '+ str(float(self.height))
# compute surface area of Cylinder
# returns a floating point number
def area (self):
return ((2*math.pi*self.radius*self.height) + (2*math.pi*(self.radius**2)))
# compute volume of a Cylinder
# returns a floating point number
def volume (self):
return (math.pi*(self.radius**2)*self.height)
# determine if a Point is strictly inside this Cylinder
# p is a Point object
# returns a Boolean
def is_inside_point (self, p):
yes = True
zmin = self.z-(self.height/2)
zmax = self.z+(self.height/2)
if (Point(p.x,p.y,self.z).distance(self.center)<self.radius):
if not (zmin<p.z<zmax):
yes = False
else:
yes = False
return yes
# 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):
yes = True
sx_cen = a_sphere.x
sy_cen = a_sphere.y
sz_cen = a_sphere.z
r = a_sphere.radius
#list of extreme values
extremes = [Point(sx_cen, sy_cen + r, sz_cen),
Point(sx_cen, sy_cen - r, sz_cen),
Point(sx_cen - r, sy_cen, sz_cen),
Point(sx_cen + r , sy_cen, sz_cen),
Point(sx_cen,sy_cen, sz_cen +r),
Point(sx_cen, sy_cen, sz_cen - r)]
#loops through list to see if they are contained
for point in extremes:
if (not self.is_inside_point(point)):
yes = False
return yes
# 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):
yes = True
x = a_cube.x
y = a_cube.y
z = a_cube.z
half_s = (a_cube.side/2)
p = [Point(x+half_s, y+half_s, z+half_s), #top left corner of upper face of other cube
Point(x-half_s, y+half_s, z+half_s), #top right corner of upper face of other cube
Point(x-half_s, y-half_s, z+half_s), # bottom left corner of upper face of other cube
Point(x+half_s, y-half_s, z+half_s), # bottom right corner of upper face of other cube
Point(x-half_s, y+half_s, z-half_s), #top left corner of lower face of other cube
Point(x+half_s, y-half_s, z-half_s), #bottom right corner of lower face of other cube
Point(x-half_s, y-half_s, z-half_s), #bottom left corner of lower face of other cube
Point(x+half_s, y+half_s, z-half_s)] # top right corner of lower face of other cube
for point in p:
if (not self.is_inside_point(point)):
yes = False
return yes
# determine if another Cylinder is strictly inside this Cylinder
# other is Cylinder object
# returns a Boolean
def is_inside_cylinder (self, other):
yes = False
zmin = self.z-(self.height/2)
zmax = self.z+(self.height/2)
# checks if other is contained within the Z min and maxes, as well as if the
# 2d cross-section of other at self.z is inside the range of self's radius.
if (Point(other.x,other.y,self.z).distance(self.center) + other.radius) < self.radius:
if (zmin<(other.z-(other.height/2))) and ((other.z+(other.height/2))<zmax):
yes = True
return yes
# determine if another Cylinder is strictly outside this Cylinder
# other is Cylinder object
# returns a Boolean
def is_outside_cylinder (self, other):
zmin = self.z-(self.height/2)
zmax = self.z+(self.height/2)
other_zmin = other.z-(other.height/2)
other_zmax = other.z+(other.height/2)
# returns opposite conditions of inside method above
return ((Point(other.x,other.y,self.z).distance(self.center) - other.radius) > self.radius) or (other_zmin>zmax) or (other_zmax<zmin)
# 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):
return ((not self.is_inside_cylinder(other)) and (not self.is_outside_cylinder(other)))
def main():
f = sys.stdin.readlines()
p = f[0].strip().split(' ')
q = f[1].strip().split(' ')
sA = f[2].strip().split(' ')
sB = f[3].strip().split(' ')
cA = f[4].strip().split(' ')
cB = f[5].strip().split(' ')
cyA = f[6].strip().split(' ')
cyB = f[7].strip().split(' ')
P = Point(float(p[0]), float(p[1]), float(p[2]))
Q = Point(float(q[0]), float(q[1]), float(q[2]))
origin = Point(0,0,0)
sphere_A = Sphere(float(sA[0]), float(sA[1]), float(sA[2]), float(sA[3]))
sphere_B = Sphere(float(sB[0]), float(sB[1]), float(sB[2]), float(sB[3]))
cube_A = Cube(float(cA[0]), float(cA[1]), float(cA[2]), float(cA[3]))
cube_B = Cube(float(cB[0]), float(cB[1]), float(cB[2]), float(cB[3]))
cyl_A = Cylinder(float(cyA[0]), float(cyA[1]), float(cyA[2]), float(cyA[3]), float(cyA[4]))
cyl_B = Cylinder(float(cyB[0]), float(cyB[1]), float(cyB[2]), float(cyB[3]), float(cyB[4]))
if P.distance(origin)>Q.distance(origin):
print('Distance of Point p from the origin is greater than the distance of Point q from the origin')
else:
print('Distance of Point p from the origin is not greater than the distance of Point q from the origin')
if sphere_A.is_inside_point(P):
print('Point p is inside sphereA')
else:
print('Point p is not inside sphereA')
if sphere_A.is_inside_sphere(sphere_B):
print('sphereB is inside sphereA')
else:
print('sphereB is not inside sphereA')
if sphere_A.is_inside_cube(cube_A):
print('cubeA is inside sphereA')
else:
print('cubeA is not inside sphereA')
if sphere_A.is_inside_cyl(cyl_A):
print('cylA is inside sphereA')
else:
print('cylA is not inside sphereA')
if sphere_B.does_intersect_sphere(sphere_A):
print('sphereA does intersect sphereB')
else:
print('sphereA does not intersect sphereB')
if sphere_B.does_intersect_cube(cube_B):
print('cubeB does intersect sphereB')
else:
print('cubeB does not intersect sphereB')
if sphere_A.circumscribe_cube().volume() > cyl_A.volume():
print('Volume of the largest Cube that is circumscribed by sphereA is greater than the volume of cylA')
else:
print('Volume of the largest Cube that is circumscribed by sphereA is not greater than the volume of cylA')
if cube_A.is_inside_point(P):
print('Point p is inside cubeA')
else:
print('Point p is not inside cubeA')
if cube_A.is_inside_sphere(sphere_A):
print('sphereA is inside cubeA')
else:
print('sphereA is not inside cubeA')
if cube_A.is_inside_cube(cube_B):
print('cubeB is inside cubeA')
else:
print('cubeB is not inside cubeA')
if cube_A.is_inside_cylinder(cyl_A):
print('cylA is inside cubeA')
else:
print('cylA is not inside cubeA')
if cube_B.does_intersect_cube(cube_A):
print('cubeA does intersect cubeB')
else:
print('cubeA does not intersect cubeB')
if cube_B.intersection_volume(cube_A) > sphere_A.volume():
print('Intersection volume of cubeA and cubeB is greater than the volume of sphereA')
else:
print('Intersection volume of cubeA and cubeB is not greater than the volume of sphereA')
if cube_A.inscribe_sphere().area()> cyl_A.area():
print('Surface area of the largest Sphere object inscribed by cubeA is greater than the surface area of cylA')
else:
print('Surface area of the largest Sphere object inscribed by cubeA is not greater than the surface area of cylA')
if cyl_A.is_inside_point(P):
print('Point p is inside cylA')
else:
print('Point p is not inside cylA')
if cyl_A.is_inside_sphere(sphere_A):
print('sphereA is inside cylA')
else:
print('sphereA is not inside cylA')
if cyl_A.is_inside_cube(cube_A):
print('cubeA is inside cylA')
else:
print('cubeA is not inside cylA')
if cyl_A.is_inside_cylinder(cyl_B):
print('cylB is inside cylA')
else:
print('cylB is not inside cylA')
if cyl_A.does_intersect_cylinder(cyl_B):
print('cylB does intersect cylA')
else:
print('cylB does not intersect cylA')
if __name__ == "__main__":
main()