-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPolygon.py
More file actions
61 lines (51 loc) · 1.91 KB
/
Polygon.py
File metadata and controls
61 lines (51 loc) · 1.91 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
from Point import Point
from LineSegment import LineSegment
class Polygon:
"""
Class representing a polygon with a set of points and edges between them.
"""
def __init__(self, points):
assert isinstance(points, list) and all(isinstance(p, Point) for p in points)
self.V = points
# check if points assume general position
# assert self.is_general_position, 'Input points must have distinct x-coordinates'
# create edges and randomize
self.E = []
for i, p in enumerate(points):
self.E.append(LineSegment(p, points[(i + 1) % len(points)]))
# check if points represent a simple polygon
# assert self.is_simple_polygon, 'Input polygon must be simple'
@property
def is_general_position(self) -> bool:
"""
Check if points assume general position by checking each
vertex has distinct x-coordinate
:return:
"""
x_s = [p.x for p in self.V]
print(sorted(x_s))
return len(x_s) == len(set(x_s))
@property
def is_simple_polygon(self) -> bool:
"""
Check if the given polygon is simple or complex.
:return: True if simple. False otherwise.
"""
for i, s1 in enumerate(self.E):
for j, s2 in enumerate(self.E[i + 1:]):
if s1.intersects(s2):
print('Segments %s and %s intersect' % (str(s1), str(s2)))
return False
return True
def __hash__(self):
return super().__hash__()
def __eq__(self, other):
"""Override the default Equals behavior"""
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return NotImplemented
def __ne__(self, other):
"""Define a non-equality test"""
if isinstance(other, self.__class__):
return not self == other
return NotImplemented