|
1 | 1 | #!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
2 | 3 | # Vector2 Class for games |
3 | | -# Original version by Will McGugan, modified extensively by CoolCat467 |
4 | 4 |
|
5 | | -from math import sqrt |
| 5 | +# Created by Will McGugan, modified extensively by CoolCat467 |
| 6 | + |
| 7 | +from __future__ import annotations |
6 | 8 |
|
7 | | -__title__ = 'Vector2' |
8 | | -__author__ = 'CoolCat467 & Will McGugan' |
9 | | -__version__ = '0.1.0' |
| 9 | +__title__ = "Vector2" |
| 10 | +__author__ = "CoolCat467 & Will McGugan" |
| 11 | +__version__ = "0.2.0" |
10 | 12 | __ver_major__ = 0 |
11 | | -__ver_minor__ = 1 |
| 13 | +__ver_minor__ = 2 |
12 | 14 | __ver_patch__ = 0 |
13 | 15 |
|
14 | | -class Vector2(object): |
15 | | - __slots__ 'x', 'y' |
16 | | - def __init__(self, x=0, y=0): |
| 16 | +from math import sqrt |
| 17 | +from typing import Any, Iterable, Iterator |
| 18 | + |
| 19 | + |
| 20 | +class Vector2: |
| 21 | + """Vector with X and Y component""" |
| 22 | + |
| 23 | + __slots__ = ("x", "y") |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + x: int | float | list[int | float] | tuple[int | float, int | float] = 0, |
| 28 | + y: int | float = 0, |
| 29 | + ) -> None: |
17 | 30 | if isinstance(x, (list, tuple)): |
18 | 31 | x, y = x |
19 | 32 | self.x = x |
20 | 33 | self.y = y |
21 | | - |
22 | | - def __str__(self): |
23 | | - return f'({self.x}, {self.y})' |
24 | | - |
25 | | - def __repr__(self): |
26 | | - x, y = self.x, self.y |
27 | | - return f'Vector2({self.x}, {self.y})' |
28 | | - |
29 | | - @staticmethod |
30 | | - def from_points(frompoint, topoint): |
31 | | - """Return a vector with the direction of frompoint to topoint""" |
32 | | - P1, P2 = list(frompoint), list(topoint) |
33 | | - return self.__class__(P2[0] - P1[0], P2[1] - P1[1]) |
34 | | - |
35 | | - def get_magnitude(self): |
| 34 | + |
| 35 | + def __str__(self) -> str: |
| 36 | + return f"({self.x}, {self.y})" |
| 37 | + |
| 38 | + def __repr__(self) -> str: |
| 39 | + return f"Vector2({self.x}, {self.y})" |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def from_points( |
| 43 | + cls, frompoint: Iterable[int | float] | "Vector2", topoint: "Vector2" |
| 44 | + ) -> Any: |
| 45 | + """Return a vector with the direction of from point to to point""" |
| 46 | + p1, p2 = list(frompoint), list(topoint) |
| 47 | + return cls(p2[0] - p1[0], p2[1] - p1[1]) |
| 48 | + |
| 49 | + def get_magnitude(self) -> float: |
36 | 50 | """Return the magnitude (length) of the vector""" |
37 | 51 | return sqrt(self.x**2 + self.y**2) |
38 | | - |
39 | | - def get_distance_to(self, point): |
| 52 | + |
| 53 | + def get_distance_to(self, point: Iterable[int | float] | "Vector2") -> float: |
40 | 54 | """Get the magnitude to a point""" |
41 | | - return self.__class__.from_points(point, self).get_magnitude() |
42 | | - |
43 | | - def normalize(self): |
44 | | - """Normalize self (make into a unit vector)""" |
| 55 | + vec: "Vector2" = self.__class__.from_points(point, self) |
| 56 | + return vec.get_magnitude() |
| 57 | + |
| 58 | + def normalize(self) -> None: |
| 59 | + """Normalize self (make into a unit vector) **IN PLACE**""" |
45 | 60 | magnitude = self.get_magnitude() |
46 | 61 | if not magnitude == 0: |
47 | 62 | self.x /= magnitude |
48 | 63 | self.y /= magnitude |
49 | | - |
50 | | - def copy(self): |
| 64 | + |
| 65 | + def __copy__(self) -> "Vector2": |
51 | 66 | """Make a copy of self""" |
52 | 67 | return self.__class__(self.x, self.y) |
53 | | - |
54 | | - def __copy__(self): |
55 | | - return self.copy() |
56 | | - |
57 | | - def get_normalized(self): |
| 68 | + |
| 69 | + copy = __copy__ |
| 70 | + |
| 71 | + def get_normalized(self) -> "Vector2": |
58 | 72 | """Return a normalized vector (heading)""" |
59 | 73 | vec = self.copy() |
60 | 74 | vec.normalize() |
61 | 75 | return vec |
62 | | - |
63 | | - #rhs is Right Hand Side |
64 | | - def __add__(self, rhs): |
| 76 | + |
| 77 | + # rhs is Right Hand Side |
| 78 | + def __add__(self, rhs: "Vector2") -> "Vector2": |
65 | 79 | return self.__class__(self.x + rhs.x, self.y + rhs.y) |
66 | | - |
67 | | - def __sub__(self, rhs): |
| 80 | + |
| 81 | + def __sub__(self, rhs: "Vector2") -> "Vector2": |
68 | 82 | return self.__class__(self.x - rhs.x, self.y - rhs.y) |
69 | | - |
70 | | - def __neg__(self): |
| 83 | + |
| 84 | + def __neg__(self: "Vector2") -> "Vector2": |
71 | 85 | return self.__class__(-self.x, -self.y) |
72 | | - |
73 | | - def __mul__(self, scalar): |
| 86 | + |
| 87 | + def __mul__(self, scalar: int | float) -> "Vector2": |
74 | 88 | return self.__class__(self.x * scalar, self.y * scalar) |
75 | | - |
76 | | - def __truediv__(self, scalar): |
| 89 | + |
| 90 | + def __truediv__(self, scalar: int | float) -> "Vector2": |
77 | 91 | try: |
78 | 92 | x, y = self.x / scalar, self.y / scalar |
79 | 93 | except ZeroDivisionError: |
80 | 94 | x, y = self.x, self.y |
81 | 95 | return self.__class__(x, y) |
82 | | - |
83 | | - def __len__(self): |
| 96 | + |
| 97 | + def __len__(self) -> int: |
84 | 98 | return 2 |
85 | | - |
86 | | - def __iter__(self): |
| 99 | + |
| 100 | + def __iter__(self) -> Iterator[int | float]: |
87 | 101 | return iter([self.x, self.y]) |
88 | | - |
89 | | - def __getitem__(self, idx): |
90 | | - return [self.x, self.y][idx] |
91 | | - pass |
| 102 | + |
| 103 | + def __getitem__(self, idx: int) -> int | float: |
| 104 | + return (self.x, self.y)[idx] |
0 commit comments