Fix up the type annotations for geometry.Direction
This commit is contained in:
parent
175f94798b
commit
391f84b21b
1 changed files with 12 additions and 4 deletions
|
@ -6,6 +6,7 @@ import math
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Iterator, Optional, overload
|
from typing import Any, Iterator, Optional, overload
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Point:
|
class Point:
|
||||||
'''A two-dimensional point, with coordinates in X and Y axes'''
|
'''A two-dimensional point, with coordinates in X and Y axes'''
|
||||||
|
@ -32,9 +33,9 @@ class Point:
|
||||||
bool
|
bool
|
||||||
True if this point is adjacent to the other point
|
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))
|
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']:
|
def direction_to_adjacent_point(self, other: 'Point') -> Optional['Vector']:
|
||||||
for direction in Direction.all():
|
for direction in Direction.all():
|
||||||
if (self + direction) != other:
|
if (self + direction) != other:
|
||||||
continue
|
continue
|
||||||
|
@ -62,6 +63,7 @@ class Point:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'(x:{self.x}, y:{self.y})'
|
return f'(x:{self.x}, y:{self.y})'
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Vector:
|
class Vector:
|
||||||
'''A two-dimensional vector, representing change in position in X and Y axes'''
|
'''A two-dimensional vector, representing change in position in X and Y axes'''
|
||||||
|
@ -76,8 +78,12 @@ class Vector:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'(δx:{self.dx}, δy:{self.dy})'
|
return f'(δx:{self.dx}, δy:{self.dy})'
|
||||||
|
|
||||||
|
|
||||||
class Direction:
|
class Direction:
|
||||||
'''A collection of simple uint vectors in each of the eight major compass directions. This is a namespace, not a class.'''
|
'''
|
||||||
|
A collection of simple uint vectors in each of the eight major compass
|
||||||
|
directions. This is a namespace, not a class.
|
||||||
|
'''
|
||||||
|
|
||||||
North = Vector(0, -1)
|
North = Vector(0, -1)
|
||||||
NorthEast = Vector(1, -1)
|
NorthEast = Vector(1, -1)
|
||||||
|
@ -89,7 +95,7 @@ class Direction:
|
||||||
NorthWest = Vector(-1, -1)
|
NorthWest = Vector(-1, -1)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all(cls) -> Iterator['Direction']:
|
def all(cls) -> Iterator[Vector]:
|
||||||
'''Iterate through all directions, starting with North and proceeding clockwise'''
|
'''Iterate through all directions, starting with North and proceeding clockwise'''
|
||||||
yield Direction.North
|
yield Direction.North
|
||||||
yield Direction.NorthEast
|
yield Direction.NorthEast
|
||||||
|
@ -100,6 +106,7 @@ class Direction:
|
||||||
yield Direction.West
|
yield Direction.West
|
||||||
yield Direction.NorthWest
|
yield Direction.NorthWest
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Size:
|
class Size:
|
||||||
'''A two-dimensional size, representing size in X (width) and Y (height) axes'''
|
'''A two-dimensional size, representing size in X (width) and Y (height) axes'''
|
||||||
|
@ -114,6 +121,7 @@ class Size:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'(w:{self.width}, h:{self.height})'
|
return f'(w:{self.width}, h:{self.height})'
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Rect:
|
class Rect:
|
||||||
'''A two-dimensional rectangle, defined by an origin point and size'''
|
'''A two-dimensional rectangle, defined by an origin point and size'''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue