-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointList.py
103 lines (82 loc) · 3.03 KB
/
pointList.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from vector_2d import Vector
class PointList(object):
def __init__(self):
self.set = set()
self.__lista = []
self.relleno = set()
self.__x_boundaries = None
self.__y_boundaries = None
def calc_bounding_box(self):
ordered_x = sorted(self.set, key=lambda point: point.x)
ordered_y = sorted(self.set, key=lambda point: point.y)
self.__x_boundaries = ordered_x[0].x, ordered_x[-1].x
self.__y_boundaries = ordered_y[0].y, ordered_y[-1].y
print(self.highest)
@property
def leftest(self) -> float:
return self.__x_boundaries[0]
@property
def rightest(self) -> float:
return self.__x_boundaries[1]
@property
def highest(self) -> float:
return self.__y_boundaries[0]
@property
def lowest(self) -> float:
return self.__y_boundaries[1]
def __getitem__(self, item: int) -> Vector:
return self.__lista[item]
def __iter__(self):
for x in range(int(self.leftest + 1), int(self.rightest), 5):
for y in range(int(self.highest - 1), int(self.lowest), 5):
if Vector(x, y) in self.relleno:
yield Vector(x, y)
def order_list(self):
self.__lista = list(self.set)
self.sort()
@property
def lista(self):
return self.__lista
def sort(self):
lista_n = self.__lista[:]
self.__lista = [lista_n.pop(), ]
while len(lista_n) > 0:
nearest = 0
for i, v in enumerate(lista_n):
if abs(self.__lista[-1] - v) < abs(self.__lista[-1] - lista_n[nearest]):
nearest = i
self.__lista.append(lista_n.pop(nearest))
def add(self, element: Vector):
self.set.add(element.int_vector())
def append(self, element: Vector):
self.__lista.append(element)
def remove(self, item: Vector):
self.lista.remove(item)
def pop(self, index: int = -1):
raise NotImplementedError
def __len__(self) -> int:
return len(self.set)
def __str__(self):
return str(self.__lista)
def connect(self):
""" Rellena si faltan puntos entremedias """
self.set = set(self.lista)
original_set = set(self.lista)
for i, p in enumerate(self.lista):
line = (p - self.lista[i - 1]).unit()
while (self.lista[i - 1] + line).int_vector() not in original_set:
self.add(line + self.lista[i - 1])
line += line.unit()
print('connected')
def fill(self):
self.calc_bounding_box()
vertical_set = set()
for x in range(int(self.leftest + 1), int(self.rightest)):
inside = False
for y in range(int(self.highest - 1), int(self.lowest)):
if Vector(x, y) in self.set and Vector(x, y - 1) not in self.set:
inside = not inside
elif inside:
vertical_set.add(Vector(x, y))
self.relleno = vertical_set
print('filled')