-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec2.py
More file actions
113 lines (86 loc) · 3.15 KB
/
vec2.py
File metadata and controls
113 lines (86 loc) · 3.15 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Simple float only Vec2 class for 3D graphics, very similar to the pyngl ones
NumPy-based implementation with VectorBase inheritance for code reuse.
"""
import numpy as np
from .vector_base import VectorBase, _create_properties
class Vec2(VectorBase["Vec2"]):
"""
A simple 2D vector class for graphics, using numpy for efficient operations.
Attributes:
x (float): The x-coordinate of the vector.
y (float): The y-coordinate of the vector.
"""
DIMENSION = 2
COMPONENT_NAMES = ("x", "y")
DEFAULT_VALUES = (0.0, 0.0)
__slots__ = ["_data"]
def cross(self, rhs: "Vec2") -> float:
"""
Cross product of two vectors a x b (2D version returns scalar).
Args:
rhs (Vec2): The right-hand side vector to cross product with.
Returns:
float: 2D cross product (perpendicular dot product).
"""
return self._data[0] * rhs._data[1] - self._data[1] * rhs._data[0]
def reflect(self, n: "Vec2") -> "Vec2":
"""
Reflect a vector about a normal.
Args:
n (Vec2): The normal to reflect about.
Returns:
Vec2: A new vector that is the result of reflecting this vector about the normal.
"""
d = self.dot(n)
# I - 2.0 * dot(N, I) * N
return Vec2(
self._data[0] - 2.0 * d * n._data[0], self._data[1] - 2.0 * d * n._data[1]
)
def outer(self, rhs: "Vec2"):
"""
Outer product of two vectors a x b.
Args:
rhs (Vec2): The right-hand side vector to outer product with.
Returns:
Mat2: A new 2x2 matrix that is the result of the outer product.
"""
from .mat2 import Mat2
result = Mat2()
result.m = np.outer(self._data, rhs._data).astype(np.float64)
return result
def __matmul__(self, rhs):
"""
Vec2 @ Mat2 matrix multiplication.
Args:
rhs (Mat2): The matrix to multiply by.
Returns:
Vec2: A new vector that is the result of multiplying this vector by the matrix.
"""
return Vec2(
self._data[0] * rhs.m[0, 0] + self._data[1] * rhs.m[1, 0],
self._data[0] * rhs.m[0, 1] + self._data[1] * rhs.m[1, 1],
)
def set(self, *args: float) -> None:
"""
Set the x,y values of the vector.
Args:
*args: Component values (x, y).
Raises:
ValueError: If wrong number of arguments or they are not floats.
"""
if len(args) != 2:
raise ValueError(f"Vec2.set requires 2 arguments, got {len(args)}")
try:
self._data[0] = float(args[0])
self._data[1] = float(args[1])
except ValueError:
raise ValueError(f"Vec2.set {args=} all need to be float")
def __repr__(self) -> str:
"""Object representation for debugging."""
return f"Vec2 [{self._data[0]},{self._data[1]}]"
def __str__(self) -> str:
"""String representation of the vector."""
return f"[{self._data[0]},{self._data[1]}]"
# Add properties for x, y components
_create_properties(Vec2)