-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_collions.py
More file actions
59 lines (44 loc) · 1.33 KB
/
circle_collions.py
File metadata and controls
59 lines (44 loc) · 1.33 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
import math
from hypothesis import given
from hypothesis import composite
class Circle():
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
class Quad():
def __init__(self, circle):
self.point = circle
def collide(node):
nr = node.radius + 16
nx1 = node.x - nr
nx2 = node.x + nr
ny1 = node.y - nr
ny2 = node.y + nr
def fixer(quad, x1, y1, x2, y2):
if quad.point and (quad.point != node):
x = node.x - quad.point.x
y = node.y - quad.point.y
l = math.sqrt(x * x + y * y)
r = node.radius + quad.point.radius
if (l < r):
l = (l - r) / l * 0.5
x = x * l
y = y * l
node.x = node.x - x
node.y = node.y - y
quad.point.x = quad.point.x + x
quad.point.y = quad.point.y + y
return x1 > nx2 or x2 < nx1 or y1 > ny2 or y2 < ny1
return fixer
def is_collision(circle1, circle2):
dx = circle1.x - circle2.x
dy = circle1.y - circle2.y
distance = math.sqrt(dx * dx + dy * dy)
return distance < circle1.radius + circle2.radius
@composite
def circle():
pass
@given(text())
def test_decode_inverts_encode(circle1, circle2):
assert is_collision(circle1, circle2)