blob: c49f5647d0cd3c34a668dd0d577462fbab8f1ca4 (
plain)
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
|
# -*- coding: utf-8 -*-
from collections import namedtuple
class Coordinate(namedtuple("Coordinate", ["x", "y"])):
def __sub__(self, other: "Coordinate"):
return Coordinate(self.x - other.x, self.y - other.y)
def __add__(self, other: "Coordinate"):
return Coordinate(self.x + other.x, self.y + other.y)
def manhattan_distance(self, other: "Coordinate"):
return abs(self.x - other.x) + abs(self.y - other.y)
@property
def polarity_x(self):
try:
return abs(self.x) / self.x
except ZeroDivisionError:
return 0
@property
def polarity_y(self):
try:
return abs(self.y) / self.y
except ZeroDivisionError:
return 0
|