-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.pde
118 lines (92 loc) · 1.99 KB
/
Vector.pde
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
private float xComp = 0;
private float yComp = 0;
private float mag = 0;
private float vecDir = 0;
public class Vector
{
public Vector()
{
}
public Vector(float dirIn, float magIn)
{
vecDir = dirIn;
mag = magIn;
xComp = mag * cos(radians(-vecDir));
yComp = mag * sin(radians(-vecDir));
}
//setters and getters
public void setX(float xIn)
{
xComp = xIn;
}
public float getX()
{
return xComp;
}
public void setY(float yIn)
{
yComp = yIn;
}
public float getY()
{
return yComp;
}
public void setMag(float magIn)
{
mag = magIn;
}
public float getMag()
{
return mag;
}
public void setDir(float dirIn)
{
vecDir = dirIn;
}
public float getDir()
{
return vecDir;
}
//various doer methods
//adds a vector to the current vector
public void addVec(Vector vecIn) {
//add the x and y compnents appropriately
xComp += vecIn.getX();
yComp += vecIn.getY();
//recalculate the magnitude and direction
mag = (float) Math.sqrt(xComp*xComp + yComp*yComp);
vecDir = acos((xComp / mag));
}
//scales the current vector by a scalar
public void scaleVec(float scaleIn)
{
xComp *= scaleIn;
y *= scaleIn;
//recalculate magnitude
mag = (float) Math.sqrt(xComp*xComp + yComp*yComp);
}
//returns the dot product of the current vector and another vector
public float dotProduct(Vector vecIn)
{
return xComp * vecIn.getX() + yComp * vecIn.getY();
}
//normalizes the vector to a magnitude of 1
public void normalizeVec()
{
if(mag != 0 && mag != 1)
{
mag = 1;
}
//recalculates the x component and y component
xComp = mag * cos(radians(-vecDir));
yComp = mag * sin(radians(-vecDir));
}
//converts the vector into an array of x and y components
public float[] toArray()
{
float[] array = new float[2];
array[0] = xComp;
array[1] = yComp;
return array;
}
}