From 54e57e7ba5ad7ae0508e6005c798db0a497e6578 Mon Sep 17 00:00:00 2001 From: Alan Aguiar Date: Sun, 26 Feb 2012 23:15:06 +0000 Subject: add CVecvtor file --- diff --git a/src/api/CVector.py b/src/api/CVector.py new file mode 100644 index 0000000..bae033c --- /dev/null +++ b/src/api/CVector.py @@ -0,0 +1,119 @@ +import math +from math import * + +EPSILON = 0.000001 + +class CVector(object): + + x = 0.0 + y = 0.0 + + def __init__(self, aX = 0.0, aY = 0.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 + + 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 math.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): + self.m = self.magnitude(); + + # Check division by zero. + if (self.m > EPSILON): + self.x /= self.m; + self.y /= self.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; +# } \ No newline at end of file -- cgit v0.9.1