-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.py
436 lines (343 loc) · 13.2 KB
/
shapes.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from __future__ import annotations
from dataclasses import dataclass
from math import prod
from operator import attrgetter
from typing import TYPE_CHECKING, Any, Optional, Self, TypeVar
import numpy as np
from matplotlib.transforms import IdentityTransform, Transform
from mpmath import mp # type: ignore[import-untyped]
from numpy.typing import NDArray
from util import SideType
if TYPE_CHECKING:
from data import Data
from util import RealNumber
DataType = TypeVar('DataType', bound='Data')
@dataclass(frozen=True)
class Point:
x: RealNumber
y: RealNumber
@classmethod
def fromarray(cls, /, array: NDArray[Any]) -> Self:
assert np.shape(array) == (2,)
return cls(array[0], array[1])
def __str__(self, /) -> str:
return f"{float(self.x):.5f}, {float(self.y):.5f}"
@property
def angle(self) -> RealNumber:
return mp.arg(self.x + 1j*self.y) # % (2*mp.pi)
def __add__(self, other: Point, /) -> Self:
return self.__class__(self.x + other.x, self.y + other.y)
def __sub__(self, other: Self, /) -> Self:
return self.__class__(self.x - other.x, self.y - other.y)
def __mul__(self, other: RealNumber, /) -> Self:
return self.__class__(self.x * other, self.y * other)
def __rmul__(self, other: RealNumber, /) -> Self:
return self.__class__(other * self.x, other * self.y)
def __truediv__(self, other: RealNumber, /) -> Self:
return self.__class__(self.x / other, self.y / other)
def rotate(
self, angle: RealNumber, /,
*, around: Optional[Self] = None
) -> Self:
around_ = around or ORIGIN
s, c = mp.sin(angle), mp.cos(angle)
rotation_matrix = np.array(((c, s), (-s, c)), dtype=object).T
x = self.x
y = self.y
x -= around_.x
y -= around_.y
coords = np.dot(rotation_matrix, ((x,), (y,)))
x = coords[0][0]
y = coords[1][0]
x += around_.x
y += around_.y
return self.__class__(x, y)
def dist(self, other: Optional[Self] = None, /) -> RealNumber:
otherx = other.x if other else 0
othery = other.y if other else 0
return mp.sqrt((self.x-otherx)**2 + (self.y-othery)**2)
def transform(
self, /,
transform_from: Optional[Transform] = None,
transform_to: Optional[Transform] = None,
) -> Self:
tfrom = transform_from or IdentityTransform()
tto = (transform_to or IdentityTransform()).inverted()
x, y = tto.transform(tfrom.transform((self.x, self.y)))
return self.__class__(x, y)
def coord(self, /) -> tuple[RealNumber, RealNumber]:
return float(self.x), float(self.y)
def normalize(self, /) -> Self:
return self / self.dist()
def rotate90(self, /) -> Self:
return self.__class__(-self.y, self.x)
def rotate180(self, /) -> Self:
return self.__class__(-self.x, -self.y)
def rotate270(self, /) -> Self:
return self.__class__(self.y, -self.x)
def dot(self, /, *others: Point) -> RealNumber:
return (self.x*prod(map(attrgetter('x'), others)) +
self.y*prod(map(attrgetter('y'), others)))
ORIGIN = Point(0, 0)
NAN_POINT = Point(float('nan'), float('nan'))
@dataclass(frozen=True)
class Triangle:
a: Point
b: Point
c: Point
def __str__(self, /) -> str:
return (f"{self.__class__.__name__}("
f"A({self.a}), "
f"B({self.b}), "
f"C({self.c}))")
@classmethod
def from_coords(
cls, coords: (NDArray[Any] |
tuple[tuple[RealNumber, RealNumber, RealNumber],
tuple[RealNumber, RealNumber, RealNumber]]),
/,
) -> Self:
if isinstance(coords, np.ndarray):
assert np.shape(coords) == (2, 3)
return cls(Point(coords[0][0], coords[1][0]),
Point(coords[0][1], coords[1][1]),
Point(coords[0][2], coords[1][2]))
@property
def coords(self) -> NDArray[Any]:
return np.array(((self.a.x, self.b.x, self.c.x),
(self.a.y, self.b.y, self.c.y)),
dtype=object)
@property
def draw_coords(self) -> NDArray[Any]:
return np.array(((self.a.x, self.b.x, self.c.x, self.a.x),
(self.a.y, self.b.y, self.c.y, self.a.y)),
dtype=object)
def rotate_coords(
self, angle: RealNumber, /,
*, around: Point = ORIGIN,
) -> NDArray[Any]:
coords = self.coords
s, c = mp.sin(angle), mp.cos(angle)
rotation_matrix = np.array(((c, s), (-s, c)), dtype=object).T
coords[0] -= around.x
coords[1] -= around.y
coords = np.dot(rotation_matrix, coords)
coords[0] += around.x
coords[1] += around.y
return coords
def rotate(self, angle: RealNumber, /, *, around: Point = ORIGIN) -> Self:
coords = self.rotate_coords(angle, around=around)
return self.__class__.from_coords(coords)
@property
def points(self) -> tuple[Point, Point, Point]:
return (self.a, self.b, self.c)
class PositiveTriangle(Triangle):
@property
def top(self) -> Point:
return self.a
@property
def left(self) -> Point:
return self.b
@property
def right(self) -> Point:
return self.c
def __str__(self, /) -> str:
return (
f"{self.__class__.__name__}("
f"T({self.top}), "
f"L({self.left}), "
f"R({self.right}))"
)
class NegativeTriangle(Triangle):
@property
def bottom(self) -> Point:
return self.a
@property
def right(self) -> Point:
return self.b
@property
def left(self) -> Point:
return self.c
def __str__(self, /) -> str:
return (
f"{self.__class__.__name__}("
f"B({self.bottom}), "
f"R({self.right}), "
f"L({self.left}))"
)
@dataclass(frozen=True)
class Arc:
point: Point
angle_start: RealNumber
angle_end: RealNumber
radius: RealNumber = 1
def rotate(self, angle: RealNumber, /) -> Self:
angle_start = self.angle_start
angle_end = self.angle_end
new_point = self.point.rotate(angle)
angle_start += angle
angle_end += angle
if angle_start > angle_end:
angle_end += 2*mp.pi
return self.__class__(new_point, angle_start, angle_end, self.radius)
@property
def coords(self) -> NDArray[Any]:
angles = np.linspace(float(self.angle_start), float(self.angle_end))
r = self.radius
x = np.insert(r*np.cos(angles), 0, self.point.x)
y = np.insert(r*np.sin(angles), 0, self.point.y)
return np.row_stack((x, y))
@property
def draw_coords(self) -> NDArray[Any]:
angles = np.linspace(float(self.angle_start), float(self.angle_end))
r = self.radius
x = np.concatenate(((self.point.x,),
r*np.cos(angles),
(self.point.x,)))
y = np.concatenate(((self.point.y,),
r*np.sin(angles),
(self.point.y,)))
return np.row_stack((x, y))
@property
def triangle_coords(self) -> NDArray[Any]:
p, s, e, r = self.point, self.angle_start, self.angle_end, self.radius
return np.array(((p.x, r*mp.cos(s), r*mp.cos(e)),
(p.y, r*mp.sin(s), r*mp.sin(e))),
dtype=object)
@property
def triangle_draw_coords(self) -> NDArray[Any]:
p, s, e, r = self.point, self.angle_start, self.angle_end, self.radius
return np.array(((p.x, r*mp.cos(s), r*mp.cos(e), p.x),
(p.y, r*mp.sin(s), r*mp.sin(e), p.x)),
dtype=object)
@dataclass(frozen=True)
class Segment:
angle_start: RealNumber
angle_end: RealNumber
radius: RealNumber = 1
def rotate(self, angle: RealNumber, /) -> Self:
angle_start = self.angle_start
angle_end = self.angle_end
angle_start += angle
angle_end += angle
if angle_start > angle_end:
angle_end += 2*mp.pi
return self.__class__(angle_start, angle_end, self.radius)
@property
def coords(self) -> NDArray[Any]:
r = self.radius
angles = np.linspace(float(self.angle_start), float(self.angle_end))
return np.row_stack((r*np.cos(angles), r*np.sin(angles)))
@property
def draw_coords(self) -> NDArray[Any]:
r = self.radius
angles = np.linspace(float(self.angle_start), float(self.angle_end))
x = np.append(r*np.cos(angles), r*np.cos(float(self.angle_start)))
y = np.append(r*np.sin(angles), r*np.sin(float(self.angle_start)))
return np.row_stack((x, y))
@property
def triangle_coords(self) -> NDArray[Any]:
s, e, r = self.angle_start, self.angle_end, self.radius
c = (s + e)/2
return np.array(((r*mp.cos(s), r*mp.cos(c), r*mp.cos(e)),
(r*mp.sin(s), r*mp.sin(c), r*mp.sin(e))),
dtype=object)
@property
def triangle_draw_coords(self) -> NDArray[Any]:
s, e, r = self.angle_start, self.angle_end, self.radius
c = (s + e)/2
return np.array(((r*mp.cos(s), r*mp.cos(c), r*mp.cos(e), r*mp.cos(s)),
(r*mp.sin(s), r*mp.sin(c), r*mp.sin(e), r*mp.sin(s))),
dtype=object)
@dataclass
class ZeroShapeCollection:
triangle: PositiveTriangle
segment: Segment
@classmethod
def create(cls, /, triangle: PositiveTriangle) -> Self:
segment = Segment(triangle.left.angle,
triangle.right.angle,
triangle.top.dist())
return cls(triangle, segment)
def rotate(self, angle: RealNumber, /) -> Self:
return self.__class__(self.triangle.rotate(angle),
self.segment.rotate(angle))
@dataclass(frozen=True)
class BaseShapeCollection:
triangle: PositiveTriangle
left_arc: Arc
right_arc: Arc
segment: Segment
@classmethod
def create(
cls, /, triangle: PositiveTriangle, above: PositiveTriangle,
) -> Self:
r = triangle.left.dist()
la = triangle.left.angle
ra = triangle.right.angle
ala = above.left.angle
ara = above.right.angle
left_arc = Arc(triangle.top, ala, la, r)
right_arc = Arc(triangle.top, ra, ara, r)
segment = Segment(la, ra, r)
return cls(triangle, left_arc, right_arc, segment)
def rotate(self, angle: RealNumber, /) -> Self:
return self.__class__(self.triangle.rotate(angle),
self.left_arc.rotate(angle),
self.right_arc.rotate(angle),
self.segment.rotate(angle))
@dataclass(frozen=True)
class NormalShapeCollection:
triangle: PositiveTriangle
negative_triangle: NegativeTriangle
horizontal_arc: Arc
vertical_arc: Arc
@classmethod
def create(
cls, /,
side: SideType,
triangle: PositiveTriangle,
touching_horizontal: PositiveTriangle,
touching_vertical: PositiveTriangle,
) -> Self:
radius = (triangle.left
if side == SideType.LEFT else
triangle.right).dist()
horizontal_angle_start = (touching_vertical.left
if side == SideType.LEFT else
triangle.right).angle
horizontal_angle_end = (triangle.left
if side == SideType.LEFT else
touching_vertical.right).angle
vertical_angle_start = (triangle.left
if side == SideType.LEFT else
touching_horizontal.right).angle
vertical_angle_end = (touching_horizontal.left
if side == SideType.LEFT else
triangle.right).angle
horizontal_arc = Arc(
triangle.top,
horizontal_angle_start,
horizontal_angle_end,
radius,
)
vertical_arc = Arc(
triangle.right if side == SideType.LEFT else triangle.left,
vertical_angle_start,
vertical_angle_end,
radius,
)
rotation_side_point = (triangle.right
if side == SideType.LEFT else
triangle.left)
rotation_center = 0.5*(triangle.top + rotation_side_point)
bottom = 2*rotation_center - triangle.top
right = 2*rotation_center - triangle.left
left = 2*rotation_center - triangle.right
negative_triangle = NegativeTriangle(bottom, right, left)
return cls(triangle, negative_triangle, horizontal_arc, vertical_arc)
def rotate(self, angle: RealNumber, /) -> Self:
return self.__class__(self.triangle.rotate(angle),
self.negative_triangle.rotate(angle),
self.horizontal_arc.rotate(angle),
self.vertical_arc.rotate(angle))