Implement is_adjacent_to and direction_to_adjacent_point on Point
This commit is contained in:
parent
2266511ec5
commit
c0c8584f45
1 changed files with 24 additions and 1 deletions
|
@ -3,7 +3,7 @@
|
||||||
'''A bunch of geometric primitives'''
|
'''A bunch of geometric primitives'''
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Iterator, overload
|
from typing import Any, Iterator, Optional, overload
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Point:
|
class Point:
|
||||||
|
@ -12,6 +12,29 @@ class Point:
|
||||||
x: int = 0
|
x: int = 0
|
||||||
y: int = 0
|
y: int = 0
|
||||||
|
|
||||||
|
def is_adjacent_to(self, other: 'Point') -> bool:
|
||||||
|
'''Check if this point is adjacent to, but not overlapping the given point
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
other : Point
|
||||||
|
The point to check
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
bool
|
||||||
|
True if this point is adjacent to the other point
|
||||||
|
'''
|
||||||
|
return (self.x in (other.x - 1, other.x + 1)) and (self.y in (other.y -1, other.y + 1))
|
||||||
|
|
||||||
|
def direction_to_adjacent_point(self, other: 'Point') -> Optional['Direction']:
|
||||||
|
for direction in Direction.all():
|
||||||
|
if (self + direction) != other:
|
||||||
|
continue
|
||||||
|
return direction
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __add__(self, other: 'Vector') -> 'Point':
|
def __add__(self, other: 'Vector') -> 'Point':
|
||||||
...
|
...
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue