Last active
April 16, 2022 22:14
-
-
Save PyMaster22/342bb5268912bef2c4b63699bcd8aeaf to your computer and use it in GitHub Desktop.
a stupid vector class cause why not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class Vector: | |
"""a stupid vector class cause why not""" | |
def __init__(self,x,y): | |
"""initalize self with x and y""" | |
if((type(x)not in[int,float])or(type(y)not in[int,float])):raise TypeError("no") | |
self.x=x | |
self.y=y | |
def __repr__(self): | |
return(f"V({self.x},{self.y})") | |
def __add__(self,other): | |
"""add self and other""" | |
if(type(other)!=Vector):raise TypeError("no") | |
return(Vector(self.x+other.x,self.y+other.y)) | |
def __mul__(self,other): | |
"""multiply self by other""" | |
if(type(other)not in[int,float]):raise TypeError("no") | |
return(Vector(self.x*other,self.y*other)) | |
def __rmul__(self,other): | |
"""reverse multiply i think""" | |
if(type(other)not in[int,float]):raise TypeError("no") | |
return(Vector(self.x*other,self.y*other)) | |
def __sub__(self,other): | |
"""subtract other from self""" | |
if(type(other)!=Vector):raise TypeError("no") | |
return(self+(other*-1)) | |
def __abs__(self): | |
"""magnitude of vector""" | |
return((self.x**2+self.y**2)**0.5) | |
def dotprod(self,other): | |
"""dotproduct of self and other""" | |
if(type(other)!=Vector):raise TypeError("no") | |
return((self.x*other.x)+(self.y*other.y)) | |
def proj(self,other): | |
"""project other onto self""" | |
if(type(other)!=Vector):raise TypeError("no") | |
return((other.dotprod(self)/self.dotprod(self))*self) | |
def isort(self,other): | |
"""are self and other orthaganol""" | |
if(type(other)!=Vector):raise TypeError("no") | |
return(self.dotprod(other)==0) | |
def angle(self,other): | |
"""find angle between self and other""" | |
if(type(other)!=Vector):raise TypeError("no") | |
return(math.degrees(math.acos(self.dotprod(other)/(abs(self)*abs(other))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment