# -*- coding: utf-8 -*- from math import cos from math import sin from math import sqrt class CVector(object): EPSILON = 0.000001 def __init__(self, aX = 0, aY = 0): self.x = aX self.y = aY # Initialize the vector, giving (x,y) cartesian coordinates. def setXY(self, aX, aY): self.x = aX self.y = aY # Sets the x value of the vector. def setX(self, aX): self.x = aX # Sets the y value of the vector. def setY(self, aY): self.y = aY # Gets the x value of the vector def getX(self): return self.x # Gets the y value of the vector def getY(self): return self.y def setVec(self, aVec): self.x = aVec.x; self.y = aVec.y; # Initialize the vector, giving polar coordinates (angle and magnitude). def setPolar(self, aAngle, aMag): # Calculate cartesian coordinates. self.x = aMag * cos(aAngle) self.y = aMag * sin(aAngle) # Add the vector aVec to itself. def add(self, aVec): self.x += aVec.x self.y += aVec.y def addXY(self, aX, aY): self.x += aX self.y += aY # Substract the vector aVec from itself. def sub(self, aVec): self.x -= aVec.x self.y -= aVec.y def subXY(self, aX, aY): self.x -= aX self.y -= aY # Multiplies the vector by a scalar. def mul(self, aScale): self.x *= aScale self.y *= aScale # Returns the magnitude of the vector. def magnitude(self): return sqrt(self.x * self.x + self.y * self.y) def magnitudeSquared(self): return (self.x * self.x + self.y * self.y) # Normalizes the vector. The vector will have a magnitude of 1. def normalize(self): m = self.magnitude(); # Check division by zero. if (m > self.EPSILON): self.x /= m; self.y /= m; # Truncates the vector assuring that the magnitude not exceed the limit. def truncate(self, aLength): if (self.magnitude() > aLength): self.normalize() self.mul(aLength) #def heading(self): # """Return the direction of the Vector in radians.""" # return direction(self.x, self.y) #def direction(self): # """Return the direction component of a vector (in radians), given # cartesian coordinates. # """ # if x > 0: # if y >= 0: # return atan(y / x) # else: # return atan(y / x) + TwoPI # elif x == 0: # if y > 0: # return HalfPI # elif y == 0: # return 0 # else: # return OneAndHalfPI # else: # return atan(y / x) + PI def destroy(self): pass # public function dot( aVector:CVector ): Number # { # return x * aVector.x + y * aVector.y; # } # # public function dotXY( aX:Number, aY:Number ):Number # { # return x * aX + y * aY; # }