Skip to content

Commit 633f5a3

Browse files
committed
Clean up code and fix type annotations
1 parent 04b7dab commit 633f5a3

File tree

3 files changed

+814
-477
lines changed

3 files changed

+814
-477
lines changed

.gitignore

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Saved example model
2+
AI_Data.json
3+
4+
# Binaries from virtual environment
5+
bin/
6+
7+
# Byte-compiled / optimized / DLL files
8+
__pycache__/
9+
*.py[cod]
10+
*$py.class
11+
12+
# C extensions
13+
*.so
14+
15+
# Distribution / packaging
16+
.Python
17+
build/
18+
develop-eggs/
19+
dist/
20+
downloads/
21+
eggs/
22+
.eggs/
23+
lib/
24+
lib64/
25+
parts/
26+
sdist/
27+
var/
28+
wheels/
29+
pip-wheel-metadata/
30+
share/python-wheels/
31+
*.egg-info/
32+
.installed.cfg
33+
*.egg
34+
MANIFEST
35+
36+
# PyInstaller
37+
# Usually these files are written by a python script from a template
38+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
39+
*.manifest
40+
*.spec
41+
42+
# Installer logs
43+
pip-log.txt
44+
pip-delete-this-directory.txt
45+
46+
# Unit test / coverage reports
47+
htmlcov/
48+
.tox/
49+
.nox/
50+
.coverage
51+
.coverage.*
52+
.cache
53+
nosetests.xml
54+
coverage.xml
55+
*.cover
56+
*.py,cover
57+
.hypothesis/
58+
.pytest_cache/
59+
60+
# Translations
61+
*.mo
62+
*.pot
63+
64+
# Django stuff:
65+
*.log
66+
local_settings.py
67+
db.sqlite3
68+
db.sqlite3-journal
69+
70+
# Flask stuff:
71+
instance/
72+
.webassets-cache
73+
74+
# Scrapy stuff:
75+
.scrapy
76+
77+
# Sphinx documentation
78+
docs/_build/
79+
80+
# PyBuilder
81+
target/
82+
83+
# Jupyter Notebook
84+
.ipynb_checkpoints
85+
86+
# IPython
87+
profile_default/
88+
ipython_config.py
89+
90+
# pyenv
91+
.python-version
92+
93+
# pipenv
94+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
96+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
97+
# install all needed dependencies.
98+
#Pipfile.lock
99+
100+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
101+
__pypackages__/
102+
103+
# Celery stuff
104+
celerybeat-schedule
105+
celerybeat.pid
106+
107+
# SageMath parsed files
108+
*.sage.py
109+
110+
# Environments
111+
.env
112+
.venv
113+
env/
114+
venv/
115+
ENV/
116+
env.bak/
117+
venv.bak/
118+
119+
# Spyder project settings
120+
.spyderproject
121+
.spyproject
122+
123+
# Rope project settings
124+
.ropeproject
125+
126+
# mkdocs documentation
127+
/site
128+
129+
# mypy
130+
.mypy_cache/
131+
.dmypy.json
132+
dmypy.json
133+
134+
# Pyre type checker
135+
.pyre/

Vector2.py

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,104 @@
11
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
23
# Vector2 Class for games
3-
# Original version by Will McGugan, modified extensively by CoolCat467
44

5-
from math import sqrt
5+
# Created by Will McGugan, modified extensively by CoolCat467
6+
7+
from __future__ import annotations
68

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"
1012
__ver_major__ = 0
11-
__ver_minor__ = 1
13+
__ver_minor__ = 2
1214
__ver_patch__ = 0
1315

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:
1730
if isinstance(x, (list, tuple)):
1831
x, y = x
1932
self.x = x
2033
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:
3650
"""Return the magnitude (length) of the vector"""
3751
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:
4054
"""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**"""
4560
magnitude = self.get_magnitude()
4661
if not magnitude == 0:
4762
self.x /= magnitude
4863
self.y /= magnitude
49-
50-
def copy(self):
64+
65+
def __copy__(self) -> "Vector2":
5166
"""Make a copy of self"""
5267
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":
5872
"""Return a normalized vector (heading)"""
5973
vec = self.copy()
6074
vec.normalize()
6175
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":
6579
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":
6882
return self.__class__(self.x - rhs.x, self.y - rhs.y)
69-
70-
def __neg__(self):
83+
84+
def __neg__(self: "Vector2") -> "Vector2":
7185
return self.__class__(-self.x, -self.y)
72-
73-
def __mul__(self, scalar):
86+
87+
def __mul__(self, scalar: int | float) -> "Vector2":
7488
return self.__class__(self.x * scalar, self.y * scalar)
75-
76-
def __truediv__(self, scalar):
89+
90+
def __truediv__(self, scalar: int | float) -> "Vector2":
7791
try:
7892
x, y = self.x / scalar, self.y / scalar
7993
except ZeroDivisionError:
8094
x, y = self.x, self.y
8195
return self.__class__(x, y)
82-
83-
def __len__(self):
96+
97+
def __len__(self) -> int:
8498
return 2
85-
86-
def __iter__(self):
99+
100+
def __iter__(self) -> Iterator[int | float]:
87101
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

Comments
 (0)