-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvex.py
175 lines (141 loc) · 5.49 KB
/
convex.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from deq import Deq
from r2point import R2Point
class Figure:
"""Абстрактная фигура"""
def perimeter(self):
return 0.0
def area(self):
return 0.0
# Площадь пересечения двух треугольников
@staticmethod
def intersects(a, b=None):
if b is not None:
c = []
for i in range(3):
flag = 1
for j in range(3):
inter = R2Point.intersect(
a[i], a[(i + 1) % 3], b[j], b[(j + 1) % 3]
)
if R2Point.area(b[j], b[(j + 1) % 3], a[i]) <= 0:
flag = 0
if inter is not None:
c.append(inter)
if flag:
c.append(a[i])
f = Void(0)
square = 0
for i in range(len(c)):
f = f.add(c[i])
if isinstance(f, Polygon):
for i in range(len(f.points.array) - 2):
square += abs(
R2Point.area(
f.points.array[i],
f.points.array[i + 1],
f.points.array[i + 2],
)
)
return square
else:
pass
def intersection_square(self):
return 0.0
class Void(Figure):
""" "Hульугольник" """
def __init__(self, flag):
self.flag = flag
self._intersection = 0.0
def add(self, p):
return Point(p, self.flag)
class Point(Figure):
""" "Одноугольник" """
def __init__(self, p, flag):
self.p = p
self.flag = flag
self._intersection = 0.0
def add(self, q):
return self if self.p == q else Segment(self.p, q, self.flag)
class Segment(Figure):
""" "Двуугольник" """
def __init__(self, p, q, flag):
self.p, self.q = p, q
self.flag = flag
self._intersection = 0.0
def perimeter(self):
return 2.0 * self.p.dist(self.q)
def add(self, r):
if R2Point.is_triangle(self.p, self.q, r):
return Polygon(self.p, self.q, r, self.flag)
elif self.q.is_inside(self.p, r):
return Segment(self.p, r, self.flag)
elif self.p.is_inside(r, self.q):
return Segment(r, self.q, self.flag)
else:
return self
class Polygon(Figure):
"""Многоугольник"""
def __init__(self, a, b, c, flag):
self.points = Deq()
self.flag = flag
self.points.push_first(b)
if b.is_light(a, c):
self.points.push_first(a)
self.points.push_last(c)
else:
self.points.push_last(a)
self.points.push_first(c)
self._perimeter = a.dist(b) + b.dist(c) + c.dist(a)
self._area = abs(R2Point.area(a, b, c))
if flag:
zz = [self.points.array[0], self.points.array[1]]
zzz = [self.points.array[2]]
self._intersection = Figure.intersects(Figure.tr, zz + zzz)
def perimeter(self):
return self._perimeter
def area(self):
return self._area
# добавление новой точки
def add(self, t):
# поиск освещённого ребра
for n in range(self.points.size()):
if t.is_light(self.points.last(), self.points.first()):
break
self.points.push_last(self.points.pop_first())
# хотя бы одно освещённое ребро есть
if t.is_light(self.points.last(), self.points.first()):
# учёт удаления ребра, соединяющего конец и начало дека
self._perimeter -= self.points.first().dist(self.points.last())
ppp = self.points.first()
self._area += abs(R2Point.area(t, self.points.last(), ppp))
if self.flag:
self._intersection += Figure.intersects(
Figure.tr, [t, self.points.first(), self.points.last()]
)
# удаление освещённых рёбер из начала дека
p = self.points.pop_first()
while t.is_light(p, self.points.first()):
self._perimeter -= p.dist(self.points.first())
self._area += abs(R2Point.area(t, p, self.points.first()))
if self.flag:
self._intersection += Figure.intersects(
Figure.tr, [t, self.points.first(), p]
)
p = self.points.pop_first()
self.points.push_first(p)
# удаление освещённых рёбер из конца дека
p = self.points.pop_last()
while t.is_light(self.points.last(), p):
self._perimeter -= p.dist(self.points.last())
self._area += abs(R2Point.area(t, p, self.points.last()))
if self.flag:
self._intersection += Figure.intersects(
Figure.tr, [t, p, self.points.last()]
)
p = self.points.pop_last()
self.points.push_last(p)
# добавление двух новых рёбер
xx = t.dist(self.points.last())
self._perimeter += t.dist(self.points.first()) + xx
self.points.push_first(t)
return self