initial commit
This commit is contained in:
27
vec.py
Normal file
27
vec.py
Normal file
@@ -0,0 +1,27 @@
|
||||
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
|
||||
Reference in New Issue
Block a user