This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathex15_1.py
86 lines (68 loc) · 2.07 KB
/
ex15_1.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
class Point:
'''Represents a point in 2-D space.'''
def print_point(p):
print( '(%g, %g)' % (p.x, p.y) )
def distance_between_points(p1, p2):
distance = math.sqrt( (p1.x - p2.x)**2 + (p1.y - p2.y)**2 )
return distance
class Rectangle:
'''Represents a rectangle
Attributes: width, height, corner.
'''
def find_center(rect):
p = Point()
p.x = rect.corner.x + rect.width/2
p.y = rect.corner.y + rect.height/2
return p
def move_rectangle(rect, dx, dy):
'''Creates and returns a new rectangle
'''
rect_new = copy.deepcopy(rect) #not only copy the object but also the references it contains
rect_new.corner.x += dx
rect_new.corner.y += dy
class Circle:
'''Represents a circle.
Attributes: center, radius
'''
def point_in_circle(crc, pt):
d = distance_between_points(crc.center, pt)
print(d)
return d <= crc.radius
def rect_in_circle(crc, rect):
d = distance_between_points(crc.center, rect.corner)
return d <= crc.radius
def rect_circle_overlap(crc, rect):
#reference: http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
rect_center = find_center(rect)
d_center = Point()
d_center.x = abs(rect_center.x - crc.center.x)
d_center.y = abs(rect_center.y - crc.center.y)
if(d_center.x > (rect.width/2 + crc.radius)):
return False
if(d_center.y > (rect.height/2 + crc.radius)):
return False
if(d_center.x <= (rect.width/2)):
return True
if(d_center.y <= (rect.height/2)):
return True
#check whether the corner of rectangle is inside the circle
cornerDistance_sq = (d_center.x - rect.width/2) **2 + (d_center.y - rect.height/2) **2
return cornerDistance_sq <= (crc.radius ** 2)
import math
import copy
if __name__ == '__main__':
bob = Rectangle()
bob.width = 100.0
bob.height = 200.0
bob.corner = Point()
bob.corner.x = 50.0
bob.corner.y = 50.0
print_point(find_center(bob))
crc = Circle()
crc.center = Point()
crc.center.x = 150.0
crc.center.y = 100.0
crc.radius = 75.0
print(point_in_circle(crc, bob.corner))
print(rect_in_circle(crc, bob))
print(rect_circle_overlap(crc,bob))