Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit 978264c

Browse files
committed
Moved Coord to seperate module.
1 parent 86cb18b commit 978264c

File tree

2 files changed

+37
-79
lines changed

2 files changed

+37
-79
lines changed

src/animation.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/coord.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,29 @@ class Coord(NamedTuple):
99
"""
1010
Helper class for managing coordinate values.
1111
12-
Coord overloads many of the numeric
12+
Coord overloads many of the numeric operators by mapping
13+
it to the x and y values individually.
1314
14-
param
15-
x: float -- X position.
16-
y: float -- Y position
15+
param:
16+
x: int -- X position.
17+
y: int -- Y position.
18+
19+
example::
20+
21+
```
22+
c1 = c2 = Coord(1, 1)
23+
c1 + c2
24+
>>> Coord(2, 2)
25+
# For convenience, integers are accepted as well
26+
c1 = Coord(1, 1)
27+
c1 + 1 # 1 is cast to Coord(1, 1)
28+
>>> Coord(2, 2)
29+
```
30+
31+
note:
32+
33+
True divide `round`s in order to remain compatible with tkinter
34+
coordinate values (`int`).
1735
"""
1836

1937
x: int
@@ -29,14 +47,25 @@ def __apply(self, op: Callable, other: Coord.Operand) -> Coord:
2947
y = op(self.y, other.y)
3048
return Coord(x, y)
3149

32-
@property
33-
def midpoint(self) -> Coord:
34-
return self // Coord(2, 2)
50+
def midpoint(self, other: Coord) -> Coord:
51+
"""
52+
The Coord that is equal distance from `self` and `other`.
53+
54+
param:
55+
other: Coord -- The point to consider.
56+
57+
return:
58+
Coord -- The resulting coordinate.
59+
"""
60+
return (self + other) / 2
61+
62+
def __truediv__(self, other):
63+
result = self.__apply(operator.truediv, other)
64+
return Coord(*map(round, result))
3565

3666
__add__ = partialmethod(__apply, operator.add)
3767
__sub__ = partialmethod(__apply, operator.sub)
3868
__mul__ = partialmethod(__apply, operator.mul)
3969
__mod__ = partialmethod(__apply, operator.mod)
4070
__pow__ = partialmethod(__apply, operator.pow)
41-
__truediv__ = partialmethod(__apply, operator.truediv)
4271
__floordiv__ = partialmethod(__apply, operator.floordiv)

0 commit comments

Comments
 (0)