-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector3d.pde
More file actions
177 lines (106 loc) · 2.47 KB
/
Vector3d.pde
File metadata and controls
177 lines (106 loc) · 2.47 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
public class Vector3D {
private float x;
private float y;
private float z;
public Vector3D(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}
public Vector3D(int x_, int y_, int z_) {
x = x_;
y = y_;
z = z_;
}
public Vector3D() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
public void setX(float x_) {
x = x_;
}
public void setY(float y_) {
y = y_;
}
public void setZ(float z_) {
z = z_;
}
public void setXY(float x_, float y_) {
x = x_;
y = y_;
}
public void setXYZ(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}
public void setVector(Vector3D v) {
x = v.getX();
y = v.getY();
z = v.getZ();
}
public Vector3D copy() {
return new Vector3D(x, y, z);
}
public float magnitude() {
return (float)Math.sqrt( x * x + y * y + z * z);
}
public Vector3D add(Vector3D v) {
return new Vector3D(x + v.getX(), y + v.getY(), z + v.getZ());
}
public Vector3D subtract(Vector3D v) {
return new Vector3D(x - v.getX(), y - v.getY(), z - v.getZ());
}
public Vector3D multiply(float n) {
return new Vector3D(x * n, y * n, z * n);
}
public Vector3D divide(float n) {
return new Vector3D(x / n, y / n, z / n);
}
public void normalize() {
float m = magnitude();
x = x / m;
y = y / m;
z = z / m;
}
public void limit(float max) {
if (magnitude() > max) {
normalize();
x *= max;
y *= max;
z *= max;
}
}
public float heading2D() {
float angle = (float)Math.atan2(y * -1.0f, x);
return -1.0f * angle;
}
public Vector3D add(Vector3D v1, Vector3D v2) {
return new Vector3D(v1.getX() + v2.getX(), v1.getY() + v2.getY(), v1.getZ() + v2.getZ());
}
public Vector3D subtract(Vector3D v1, Vector3D v2) {
return new Vector3D(v1.getX() - v2.getX(), v1.getY() - v2.getY(), v1.getZ() - v2.getZ());
}
public Vector3D divide(Vector3D v1, float n) {
return new Vector3D(v1.getX() / n, v1.getY() / n, v1.getZ() / n);
}
public Vector3D multiply(Vector3D v1, float n) {
return new Vector3D(v1.getX() * n , v1.getY() * n, v1.getZ() * n);
}
public float distance(Vector3D v1, Vector3D v2) {
float dx = v1.getX() - v2.getX();
float dy = v1.getY() - v2.getY();
float dz = v1.getZ() - v2.getZ();
return (float)Math.sqrt(dx * dx + dy * dy + dz * dz);
}
}