
Untitled
By: a guest on
Jul 24th, 2012 | syntax:
None | size: 1.08 KB | hits: 11 | expires: Never
from __future__ import division
import math
class Vector(object):
def __init__(self, (x, y)=(0, 0)):
self.coord = (x, y)
self.length = math.sqrt(x**2 + y**2)
if x == 0:
if y < 0:
self.angle = 270
else:
self.angle = 90
else:
self.angle = math.atan(y/x)
def normalize(self, newlength=1):
if self.length == 0: return
self.coord = (self.coord[0]*newlength/self.length, self.coord[1]*newlength/self.length)
self.length = newlength
def setangle(self, newangle=0):
self.angle = newangle
x = self.length*math.cos(self.angle)
y = self.length*math.sin(self.angle)
self.coord = (x, y)
def setcoord(self, (x, y)=(0, 0)):
self.coord = (x, y)
self.angle = math.atan(y/x)
self.length = math.sqrt(x**2 + y**2)
def dotproduct(self, othervector):
return self.coord[0]*othervector.coord[0] + self.coord[1]*othervector.coord[1]
def add(self, othervector):
return Vector((self.coord[0]+othervector.coord[0], self.coord[1]+othervector.coord[1]))
def subtract(self, othervector):
return Vector((self.coord[0]-othervector.coord[0], self.coord[1]-othervector.coord[1]))