2024-04-15 19:18:52 +00:00
|
|
|
from math import sqrt
|
|
|
|
|
|
|
|
|
|
|
|
class Vec2:
|
|
|
|
def __init__(self, x: float = 0, y: float = 0):
|
|
|
|
self.x: float = x
|
|
|
|
self.y: float = y
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mag(self) -> float:
|
|
|
|
return sqrt(self.x ** 2 + self.y ** 2)
|
|
|
|
|
|
|
|
def normalized(self) -> "Vec2":
|
|
|
|
mag = self.mag
|
|
|
|
if mag == 0:
|
|
|
|
return Vec2()
|
|
|
|
return self / mag
|
|
|
|
|
|
|
|
def __add__(self, v: "Vec2") -> "Vec2":
|
|
|
|
return Vec2(self.x + v.x, self.y + v.y)
|
|
|
|
|
|
|
|
def __sub__(self, v: "Vec2") -> "Vec2":
|
|
|
|
return Vec2(self.x - v.x, self.y - v.y)
|
|
|
|
|
|
|
|
def __mul__(self, f: float) -> "Vec2":
|
|
|
|
return Vec2(self.x * f, self.y * f)
|
|
|
|
|
|
|
|
def __truediv__(self, f: float) -> "Vec2":
|
|
|
|
return Vec2(self.x / f, self.y / f)
|
|
|
|
|
|
|
|
def __neg__(self) -> "Vec2":
|
|
|
|
return Vec2(-self.x, -self.y)
|
|
|
|
|
|
|
|
def cross(self, v: "Vec2") -> float:
|
|
|
|
return self.x * v.y - self.y * v.x
|
2024-04-15 21:26:41 +00:00
|
|
|
|
|
|
|
def scale(self, v: "Vec2") -> "Vec2":
|
|
|
|
return Vec2(self.x * v.x, self.y * v.y)
|