Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point():
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __str__(self):
- return "({}, {})".format(self.x, self.y)
- def __repr__(self):
- return "({}, {})".format(self.x, self.y)
- def are_polygons_intersecting(poly_a, poly_b):
- for polygon in (poly_a, poly_b):
- #print("\nPolygon")
- for i1 in range(len(polygon)):
- i2 = (i1 + 1) % len(polygon)
- #print("i1={} i2={}".format(i1, i2))
- p1 = polygon[i1]
- p2 = polygon[i2]
- normal = Point(p2.y - p1.y, p1.x - p2.x)
- minA, maxA, minB, maxB = (None,) * 4
- for p in poly_a:
- projected = normal.x * p.x + normal.y * p.y
- if not minA or projected < minA:
- minA = projected
- if not maxA or projected > maxA:
- maxA = projected
- for p in poly_b:
- projected = normal.x * p.x + normal.y * p.y
- if not minB or projected < minB:
- minB = projected
- if not maxB or projected > maxB:
- maxB = projected
- #print("maxA={} minB={} -- maxB={} minA={}".format(minA, minB, maxA, maxB))
- if maxA < minB or maxB < minA:
- return False
- return True
- p1 = (Point(0.235, 0.305), Point(0.485, 0.305), Point(0.235, 0.495), Point(0.485, 0.495))
- p2 = (Point(-0.125, 0.405), Point(0.125, 0.405), Point(-0.125, 0.595), Point(0.125, 0.595))
- r = are_polygons_intersecting(p1, p2)
- print(r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement