-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathxy.py
37 lines (27 loc) · 1.19 KB
/
xy.py
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
28
29
30
31
32
33
34
35
36
37
"""Represent x,y coords with properly overloaded operations."""
# Copyright 2016-2023 Florian Pigorsch & Contributors. All rights reserved.
#
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file.
from typing import Tuple, Union
class XY:
"""Represent x,y coords with properly overloaded operations."""
def __init__(self, x: float = 0, y: float = 0) -> None:
self.x = x
self.y = y
def __mul__(self, factor: Union[float, "XY"]) -> "XY":
if isinstance(factor, XY):
return XY(self.x * factor.x, self.y * factor.y)
return XY(self.x * factor, self.y * factor)
def __rmul__(self, factor: Union[float, "XY"]) -> "XY":
if isinstance(factor, XY):
return XY(self.x * factor.x, self.y * factor.y)
return XY(self.x * factor, self.y * factor)
def __add__(self, other: "XY") -> "XY":
return XY(self.x + other.x, self.y + other.y)
def __sub__(self, other: "XY") -> "XY":
return XY(self.x - other.x, self.y - other.y)
def __repr__(self) -> str:
return f"XY: {self.x}/{self.y}"
def tuple(self) -> Tuple[float, float]:
return self.x, self.y