Created
September 7, 2013 14:06
-
-
Save TylerLeite/6475845 to your computer and use it in GitHub Desktop.
2d vector class
This file contains 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
from math import atan2, sin, cos, sqrt, pi, floor | |
class Vec2: | |
def __init__(self, x=0, y=0, form='rect'): | |
self.x = x | |
self.y = y | |
if form == 'rect' or form == 'polar': | |
self.form = form | |
else: | |
print('Invalid Vec2 form: %s' % form) | |
exit(0) | |
def to_str(self): | |
out = self.form + ' (' + str(self.x) + ', ' + str(self.y) + ')' | |
return out | |
def change(self, vec): | |
self.x, self.y = vec.x, vec.y | |
self.form = vec.form | |
def get_polar(self): | |
out = Vec2(self.x, self.y, self.form) | |
if self.form == 'rect': | |
x, y = self.x, self.y | |
out.x = sqrt(x*x + y*y) | |
out.y = atan2(y, x)*180/pi | |
out.form = 'polar' | |
return out | |
def to_polar(self): | |
if self.form == 'rect': | |
self = self.get_polar() | |
self.form = 'polar' | |
return self | |
def get_rect(self): | |
out = Vec2(self.x, self.y, self.form) | |
if out.form == 'polar': | |
out.x = self.x*cos(self.y*pi/180) | |
out.y = self.x*sin(self.y*pi/180) | |
out.form = 'rect' | |
return out | |
def to_rect(self): | |
if self.form == 'polar': | |
self = self.get_rect() | |
self.form = 'rect' | |
return self | |
def get_normalized(self): | |
out = Vec2(self.x, self.y, self.form) | |
if out.form == 'polar': | |
out.x = 1.0 | |
else: | |
len = out.get_length() | |
if len != 0: | |
out.x /= len | |
out.y /= len | |
return out | |
def normalize(self): | |
self = self.get_normalized() | |
return self | |
def dot_product(self, vec): | |
x = self.get_x()*vec.get_x() | |
y = self.get_y()*vec.get_y() | |
return x + y | |
def cross_product(self, vec): | |
x = self.get_x()*vec.get_x() | |
y = self.get_y()*vec.get_y() | |
return x - y | |
def get_length(self): | |
return self.get_polar().x | |
def get_angle(self): | |
return self.get_polar().y | |
def get_x(self): | |
return self.get_rect().x | |
def get_y(self): | |
return self.get_rect().y | |
def is_colinear(self, vec): | |
xM = max(self.get_x(), vec.get_x()) | |
xm = min(self.get_x(), vec.get_x()) | |
yM = max(self.get_y(), vec.get_y()) | |
ym = min(self.get_y(), vec.get_y()) | |
return xm/xM == floor(xm/xM) and ym/yM == floor(ym/yM) | |
def is_orthogonal(self, vec): | |
return bool(self.dot_product(vec)) | |
def __add__(self, vec): | |
tmp = Vec2(self.get_x() + vec.get_x(), | |
self.get_y() + vec.get_y(), | |
'rect') | |
if self.form == 'rect': | |
return tmp | |
else: | |
return tmp.get_polar() | |
def __sub__(self, vec): | |
tmp = Vec2(self.get_x() - vec.get_x(), | |
self.get_y() - vec.get_y(), | |
'rect') | |
if self.form == 'rect': | |
return tmp | |
else: | |
return tmp.get_polar() | |
def __mul__(self, n): | |
x = self.x * n | |
if self.form == 'rect': | |
y = self.y * n | |
else: | |
y = self.y | |
return Vec2(x, y, self.form) | |
def __div__(self, n): | |
if n: | |
x = self.x/float(n) | |
if self.form == 'rect': | |
y = self.y/float(n) | |
else: | |
y = self.y | |
return Vec2(x, y, self.form) | |
else: | |
return None | |
def __iadd__(self, vec): | |
if self.form == 'polar': | |
self.to_rect() | |
revert = True | |
else: | |
revert = False | |
self.x += vec.get_x() | |
self.y += vec.get_y() | |
if revert: | |
self.to_polar() | |
return self | |
def __isub__(self, vec): | |
if self.form == 'polar': | |
self.to_rect() | |
revert = True | |
else: | |
revert = False | |
self.x -= vec.get_x() | |
self.y -= vec.get_y() | |
if revert: | |
self.to_polar() | |
return self | |
def __imul__(self, n): | |
self.x *= n | |
if self.form == 'rect': | |
self.y *= n | |
return self | |
def __idiv__(self, n): | |
if n: | |
self.x /= float(n) | |
if self.form == 'rect': | |
self.y /= float(n) | |
return self | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment