rivet/vec.py

29 lines
703 B
Python

from __future__ import annotations
from math import sqrt
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