-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoord.py
More file actions
173 lines (144 loc) · 3.87 KB
/
coord.py
File metadata and controls
173 lines (144 loc) · 3.87 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
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
"""
2019 refactor of my coord library from 2016
"""
from dataclasses import dataclass
from math import atan2, degrees, atan2, sqrt
@dataclass(order=False, repr=False, frozen=True)
class Coord:
"""
>>> Coord(1, 1) == Coord(1, 1)
True
>>> Coord(1, 0) == Coord(-1, 0)
False
>>> c1 = Coord()
>>> c2 = Coord()
>>> c3 = Coord(1, 0)
>>> hash(c1) == hash(c2)
True
>>> hash(c1) == hash(c3)
False
>>> hash(c2) == hash(c3)
False
"""
x: int = 0
y: int = 0
def __add__(self, other):
"""
>>> c = Coord()
>>> c + Coord(1, 4)
(1, 4)
>>> c
(0, 0)
>>> Coord(-4, 3) + Coord(6, 2)
(2, 5)
"""
return self.__class__(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return self.__class__(self.x - other.x, self.y - other.y)
def __mul__(self, other):
"""
>>> c1 = Coord(1, 4)
>>> c1 * 3
(3, 12)
"""
return self.__class__(self.x * other, self.y * other)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __lt__(self, other):
return self.x < other.x and self.y < other.y
def __str__(self):
"""
>>> Coord(0, 0)
(0, 0)
>>> Coord(1, 4)
(1, 4)
"""
return f'({self.x}, {self.y})'
def __repr__(self):
return str(self)
def rotate_left(self):
"""
>>> c = Coord(0, 1)
>>> c.rotate_left()
(-1, 0)
"""
return self.__class__(-self.y, self.x)
def rotate_right(self):
"""
>>> c = Coord(0, 1)
>>> c.rotate_right()
(1, 0)
"""
return self.__class__(self.y, -self.x)
def up(self):
return self.__class__(self.x, self.y + 1)
def down(self):
return self.__class__(self.x, self.y - 1)
def left(self):
return self.__class__(self.x - 1, self.y)
def right(self):
return self.__class__(self.x + 1, self.y)
@property
def neighbours(self):
for neighbour in [self.up(), self.left(), self.down(), self.right()]:
yield neighbour
def manhatten_dist(self, start=None):
"""
>>> c = Coord(2, 3)
>>> c.manhatten_dist()
5
>>> c = Coord(0, -2)
>>> c.manhatten_dist()
2
>>> c = Coord(0, -2)
>>> c.manhatten_dist(Coord(1, 2))
5
"""
start = start or Coord(0, 0)
return abs(self.x - start.x) + abs(self.y - start.y)
@property
def angle(self):
return atan2(self.y, self.x)
def dotproduct(self, other):
return self.x * other.x + self.y * other.y
def determinant(self, other):
return self.x * other.y - self.y * other.x
def vector_length(self):
return sqrt(self.dotproduct(self))
def angle_to(self, other=None):
"""
>>> Coord(1, 0).angle_to()
90
>>> Coord(-1, 0).angle_to()
270
>>> Coord(0, -1).angle_to()
180
>>> Coord(0, 1).angle_to()
0
>>> Coord(-2,-3).angle_to()
214
>>> Coord(-1,-3).angle_to()
199
"""
if other is None:
other = Coord(0, 1)
result = degrees(atan2(self.determinant(other), self.dotproduct(other)))
if result < 0:
result = 360 + result
return result
@dataclass(order=False, repr=False, frozen=True)
class Coord3(Coord):
z: int = 0
def __add__(self, other):
"""
>>> Coord3(1, 2, 3) + Coord3(-1, 2, 0)
(0, 4, 3)
"""
return self.__class__(self.x + other.x, self.y + other.y, self.z + other.z)
def __str__(self):
return f"({self.x}, {self.y}, {self.z})"
def __repr__(self):
return str(self)
if __name__ == "__main__":
import doctest
doctest.testmod()