rivet/vec.py

29 lines
703 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:
self.x = x
self.y = y
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()
if mag == 0: return Vec()
return self/mag