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

Commit 86cb18b

Browse files
committed
Adjusted true div behavior to remain consistent.
1 parent 876b141 commit 86cb18b

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/animation.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class Coord(NamedTuple):
1313
it to the x and y values individually.
1414
1515
param:
16-
x: float -- X position.
17-
y: float -- Y position.
16+
x: int -- X position.
17+
y: int -- Y position.
1818
1919
example::
2020
@@ -27,6 +27,11 @@ class Coord(NamedTuple):
2727
c1 + 1 # 1 is cast to Coord(1, 1)
2828
>>> Coord(2, 2)
2929
```
30+
31+
note:
32+
33+
True divide `round`s in order to remain compatible with tkinter
34+
coordinate values (`int`).
3035
"""
3136

3237
x: int
@@ -43,12 +48,24 @@ def __apply(self, op: Callable, other: Coord.Operand) -> Coord:
4348
return Coord(x, y)
4449

4550
def midpoint(self, other: Coord) -> Coord:
46-
return (self + other) // 2
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))
4765

4866
__add__ = partialmethod(__apply, operator.add)
4967
__sub__ = partialmethod(__apply, operator.sub)
5068
__mul__ = partialmethod(__apply, operator.mul)
5169
__mod__ = partialmethod(__apply, operator.mod)
5270
__pow__ = partialmethod(__apply, operator.pow)
53-
__truediv__ = partialmethod(__apply, operator.truediv)
5471
__floordiv__ = partialmethod(__apply, operator.floordiv)

src/test/test_coord.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def test_pow():
3030

3131

3232
def test_truediv():
33-
assert coord1 / coord2 == Coord(1.0, 1.0)
34-
assert coord1 / coord2 == Coord(1.0, 1.0)
33+
assert coord1 / coord2 == Coord(1, 1)
34+
assert Coord(2, 2) / 3 == Coord(1, 1)
3535

3636

3737
def test_floordiv():

0 commit comments

Comments
 (0)