-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxypoint.py
518 lines (389 loc) · 12.1 KB
/
xypoint.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
from typing import List
import math
class XYPoint():
"""
A class representing a point in 2D space
"""
""" The epsilon value for comparing floats. """
EPSILON = 0.0001
def __init__(self, x: float, y: float):
"""
Constructor for Point
:param x: The x coordinate
:type x: float
:param y: The y coordinate
:type y: float
"""
self.x = x
self.y = y
def __str__(self):
"""
__str__ method override
:return: The string representation of the Point
:rtype: str
"""
return f'[{self.x}, {self.y}]'
def __repr__(self):
"""
__repr__ method override
:return: The string representation of the Point
:rtype: str
"""
return f'[{self.x}, {self.y}]'
def __eq__(self, other):
"""
__eq__ method override
:param other: The other Point to compare to
:type other: Point
:return: True if the Points are equal, False otherwise
:rtype: bool
"""
return abs(self.x - other.x) < self.EPSILON and abs(self.y - other.y) < self.EPSILON
def __ne__(self, other):
"""
__ne__ method override
:param other: The other Point to compare to
:type other: Point
"""
return not self.__eq__(other)
def __hash__(self):
"""
__hash__ method override
:return: The hash of the Point
:rtype: int
"""
return hash((self.x, self.y))
def __lt__(self, other):
"""
__lt__ method override
:param other: The other Point to compare to
:type other: Point
:return: True if the Point is less than the other Point, False otherwise
:rtype: bool
"""
return self.x < other.x or (self.x == other.x and self.y < other.y)
def __le__(self, other):
"""
__le__ method override
:param other: The other Point to compare to
:type other: Point
:return: True if the Point is less than or equal to the other Point, False otherwise
:rtype: bool
"""
return self.__lt__(other) or self.__eq__(other)
def __gt__(self, other):
"""
__gt__ method override
:param other: The other Point to compare to
:type other: Point
:return: True if the Point is greater than the other Point, False otherwise
:rtype: bool
"""
return self.x > other.x or (self.x == other.x and self.y > other.y)
def __ge__(self, other):
"""
__ge__ method override
:param other: The other Point to compare to
:type other: Point
:return: True if the Point is greater than or equal to the other Point, False otherwise
:rtype: bool
"""
return self.__gt__(other) or self.__eq__(other)
def __add__(self, other):
"""
__add__ method override
:param other: The other Point to add to
:type other: Point
:return: The sum of the Points
:rtype: Point
"""
return XYPoint(self.x + other.x, self.y + other.y)
def __sub__(self, other):
"""
__sub__ method override
:param other: The other Point to subtract from
:type other: Point
:return: The difference of the Points
:rtype: Point
"""
return XYPoint(self.x - other.x, self.y - other.y)
def __mul__(self, other):
"""
__mul__ method override
:param other: The other Point to multiply by
:type other: Point
:return: The product of the Points
:rtype: Point
"""
return XYPoint(self.x * other.x, self.y * other.y)
def __truediv__(self, other):
"""
__truediv__ method override
:param other: The other Point to divide by
:type other: Point
:return: The quotient of the Points
:rtype: Point
"""
return XYPoint(self.x / other.x, self.y / other.y)
def __floordiv__(self, other):
"""
__floordiv__ method override
:param other: The other Point to divide by
:type other: Point
:return: The quotient of the Points
:rtype: Point
"""
return XYPoint(self.x // other.x, self.y // other.y)
def __mod__(self, other):
"""
__mod__ method override
:param other: The other Point to divide by
:type other: Point
:return: The remainder of the Points
:rtype: Point
"""
return XYPoint(self.x % other.x, self.y % other.y)
def __pow__(self, other):
"""
__pow__ method override
:param other: The other Point to raise to
:type other: Point
:return: The power of the Points
:rtype: Point
"""
return XYPoint(self.x ** other.x, self.y ** other.y)
def __abs__(self):
"""
__abs__ method override
:return: The absolute value of the Point
:rtype: Point
"""
return XYPoint(abs(self.x), abs(self.y))
def __neg__(self):
"""
__neg__ method override
:return: The negated Point
:rtype: Point
"""
return XYPoint(-self.x, -self.y)
def __pos__(self):
"""
__pos__ method override
:return: The positive Point
:rtype: Point
"""
return self.__abs__()
def __invert__(self):
"""
__invert__ method override
:return: The inverted Point
:rtype: Point
"""
return XYPoint(~self.x, ~self.y)
def __round__(self, n=None):
"""
__round__ method override
:param n: The number of decimal places to round to
:type n: int
:return: The rounded Point
:rtype: Point
"""
return XYPoint(round(self.x, n), round(self.y, n))
def __floor__(self):
"""
__floor__ method override
:return: The floored Point
:rtype: Point
"""
return XYPoint(math.floor(self.x), math.floor(self.y))
def __ceil__(self):
"""
__ceil__ method override
:return: The ceilinged Point
:rtype: Point
"""
return XYPoint(math.ceil(self.x), math.ceil(self.y))
def __trunc__(self):
"""
__trunc__ method override
:return: The truncated Point
:rtype: Point
"""
return XYPoint(math.trunc(self.x), math.trunc(self.y))
def __radd__(self, other):
"""
__radd__ method override
:param other: The other Point to add to
:type other: Point
:return: The sum of the Points
:rtype: Point
"""
return self.__add__(other)
def __rsub__(self, other):
"""
__rsub__ method override
:param other: The other Point to subtract from
:type other: Point
:return: The difference of the Points
:rtype: Point
"""
return self.__sub__(other)
def __rmul__(self, other):
"""
__rmul__ method override
:param other: The other Point to multiply by
:type other: Point
:return: The product of the Points
:rtype: Point
"""
return self.__mul__(other)
def __rtruediv__(self, other):
"""
__rtruediv__ method override
:param other: The other Point to divide by
:type other: Point
:return: The quotient of the Points
:rtype: Point
"""
return self.__truediv__(other)
def __rfloordiv__(self, other):
"""
__rfloordiv__ method override
:param other: The other Point to divide by
:type other: Point
:return: The quotient of the Points
:rtype: Point
"""
return self.__floordiv__(other)
def __rmod__(self, other):
"""
__rmod__ method override
:param other: The other Point to divide by
:type other: Point
:return: The remainder of the Points
:rtype: Point
"""
return self.__mod__(other)
def __rpow__(self, other):
"""
__rpow__ method override
:param other: The other Point to raise to
:type other: Point
:return: The power of the Points
:rtype: Point
"""
return self.__pow__(other)
def __iadd__(self, other):
"""
__iadd__ method override
:param other: The other Point to add to
:type other: Point
:return: The sum of the Points
:rtype: Point
"""
return self.__add__(other)
def __isub__(self, other):
"""
__isub__ method override
:param other: The other Point to subtract from
:type other: Point
:return: The difference of the Points
:rtype: Point
"""
return self.__sub__(other)
def __imul__(self, other):
"""
__imul__ method override
:param other: The other Point to multiply by
:type other: Point
:return: The product of the Points
:rtype: Point
"""
return self.__mul__(other)
def __itruediv__(self, other):
"""
__itruediv__ method override
:param other: The other Point to divide by
:type other: Point
:return: The quotient of the Points
:rtype: Point
"""
return self.__truediv__(other)
def __ifloordiv__(self, other):
"""
__ifloordiv__ method override
:param other: The other Point to divide by
:type other: Point
:return: The quotient of the Points
:rtype: Point
"""
return self.__floordiv__(other)
def __imod__(self, other):
"""
__imod__ method override
:param other: The other Point to divide by
:type other: Point
:return: The remainder of the Points
:rtype: Point
"""
return self.__mod__(other)
def __ipow__(self, other):
"""
__ipow__ method override
:param other: The other Point to raise to
:type other: Point
:return: The power of the Points
:rtype: Point
"""
return self.__pow__(other)
def __ilshift__(self, other):
"""
__ilshift__ method override
:param other: The other Point to shift by
:type other: Point
:return: The shifted Point
:rtype: Point
"""
return XYPoint(self.x << other.x, self.y << other.y)
def __irshift__(self, other):
"""
__irshift__ method override
:param other: The other Point to shift by
:type other: Point
:return: The shifted Point
:rtype: Point
"""
return XYPoint(self.x >> other.x, self.y >> other.y)
def euclidean_distance(p1: XYPoint, p2: XYPoint) -> float:
"""
Calculates the Euclidean distance between two points
:param p1: The first point
:type p1: Point
:param p2: The second point
:type p2: Point
:return: The Euclidean distance between the points
:rtype: float
"""
return math.sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2)
def manhattan_distance(p1: XYPoint, p2: XYPoint) -> float:
"""
Calculates the Manhattan distance between two points
:param p1: The first point
:type p1: Point
:param p2: The second point
:type p2: Point
:return: The Manhattan distance between the points
:rtype: float
"""
return abs(p1.x - p2.x) + abs(p1.y - p2.y)
def cosine_similarity(p1: XYPoint, p2: XYPoint) -> float:
"""
Calculates the cosine similarity between two points
:param p1: The first point
:type p1: Point
:param p2: The second point
:type p2: Point
:return: The cosine similarity between the points
:rtype: float
"""
return (p1.x * p2.x + p1.y * p2.y) / (math.sqrt(p1.x**2 + p1.y**2) * math.sqrt(p2.x**2 + p2.y**2))