rivet/vec.py

31 lines
732 B
Python
Raw Normal View History

2023-11-24 13:34:44 +00:00
from __future__ import annotations
2024-03-24 10:33:34 +00:00
2023-11-24 13:34:44 +00:00
from math import sqrt
2024-03-24 10:33:34 +00:00
2023-11-24 13:34:44 +00:00
class Vec:
def __init__(self, x: float = 0, y: float = 0) -> None:
2024-04-12 21:33:09 +00:00
self.x: float = x
self.y: float = y
2023-11-24 13:34:44 +00:00
def __add__(self, v: Vec) -> Vec:
return Vec(self.x+v.x, self.y+v.y)
def __sub__(self, v: Vec) -> Vec:
return Vec(self.x-v.x, self.y-v.y)
def __mul__(self, f: float) -> Vec:
return Vec(self.x*f, self.y*f)
def __truediv__(self, f: float) -> Vec:
return Vec(self.x/f, self.y/f)
def mag(self) -> float:
return sqrt(self.x**2 + self.y**2)
def norm(self) -> Vec:
mag = self.mag()
2024-04-12 21:33:09 +00:00
if mag == 0:
return Vec()
return self / mag