Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/api/Vector.py
blob: 73a305583a5b4193808e79b35d5f5267d3980c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-

from math import cos
from math import sin
from math import sqrt

class CVector(object):
    
    x = 0.0
    y = 0.0

    EPSILON = 0.000001
    
    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
    
    # 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;
#        }