-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.py
68 lines (55 loc) · 1.86 KB
/
point.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
from mathobject import MathObject
import config
from config import LABEL_FONT
from vector import Vector
class Point(MathObject):
"""A 2d point."""
def __init__(self, x, y, label=None):
super().__init__()
self._point = (x, y)
self._label = label
def copy(self):
return Point(self._point[0], self._point[1])
def draw(self):
super().draw()
transformed = config.command_interpretter.initial_transform * Vector(self._point[0], self._point[1])
o = self._canvas.create_oval(
transformed[0] - 3,
transformed[1] - 3,
transformed[0] + 3,
transformed[1] + 3,
fill=self._color,
outline=self._color,
)
self._canvas_items.append(o)
if self._label is not None:
t = self._canvas.create_text(
transformed[0] + 10,
transformed[1] + 10,
text=self._label,
font=LABEL_FONT,
fill=self._color
)
self._canvas_items.append(t)
def __add__(self, other):
if isinstance(other, Vector):
return Point(
self._point[0] + other._vector[0], self._point[1] + other._vector[1]
)
return NotImplemented
def __sub__(self, other):
if isinstance(other, Point):
v = Vector(self._point[0] - other._point[0], self._point[1] - other._point[1])
v.position = other._point
return v
return NotImplemented
@property
def x(self):
return self._point[0]
@property
def y(self):
return self._point[1]
def __getitem__(self, key):
return self._point[key]
def __str__(self) -> str:
return self._point.__str__()