-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircleRectIntersect.py
66 lines (58 loc) · 2.09 KB
/
circleRectIntersect.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
from graphics import Circle, Rectangle, Point
def circleRectIntersect(circle,rectangle):
### Python algorithm for intersection of a circle and rectangle created as
### a hybrid of several postings show on the following webpage at StackOverflow
### http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
ccx = circle.getCenter().getX()
ccy = circle.getCenter().getY()
cr = circle.getRadius()
if rectangle.getP1().getX() < rectangle.getP2().getX():
rlx = rectangle.getP1().getX()
rrx = rectangle.getP2().getX()
else:
rlx = rectangle.getP2().getX()
rrx = rectangle.getP1().getX()
if rectangle.getP1().getY() < rectangle.getP2().getY():
rty = rectangle.getP1().getY()
rby = rectangle.getP2().getY()
else:
rty = rectangle.getP2().getY()
rby = rectangle.getP1().getY()
#determine if center point is left, right or within the width range of the rectangle
if ccx < rlx:
#closest to left
closestX = rlx
side = "left"
elif ccx > rrx:
#closest to right
closestX = rrx
side = "right"
else:
#circle center is in width range of the rectangle
closestX = ccx
side = "inrange"
#determine if center point is above, below or within the height range of the rectangle
if ccy < rty:
#closest to top
closestY = rty
tb = "top"
elif ccy > rby:
#closest to bottom
closestY = rby
tb = "bottom"
else:
#circle center is in height range of the rectangle
closestY = ccy
tb = "inrange"
# Calculate the distance between the circle's center and the closest points in rectangle
dx = closestX - ccx
dy = closestY - ccy
# If the distance is less than the circle's radius, an intersection occurs
if (dx * dx + dy * dy) <= cr * cr:
#intersection happened, now determine which side we hit
if side == "inrange":
return tb
else: return side
else:
#no intersection
return "no"