-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirection.pde
79 lines (64 loc) · 1.45 KB
/
Direction.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
abstract class Direction extends Tuple {
static final String printformat = "(%.4f, %.4f, %.4f)";
Direction() {
super();
}
Direction(float x, float y) {
set(x, y);
}
Direction(float x, float y, float z) {
super(x, y, z);
}
String toString() {
return String.format(printformat, x, y, z);
}
float dot(Direction dir) {
return x * dir.x + y * dir.y + z * dir.z;
}
float dot(float dirx, float diry, float diz) {
return x * dirx + y * diry + z * diz;
}
Direction fromAngle(float theta) {
set(cos(theta), sin(theta), 0.0);
return this;
}
Direction fromAngle(float theta, float phi) {
float sinphi = sin(phi);
set(sinphi * cos(theta), sinphi * sin(theta), cos(phi));
return this;
}
float magnitude() {
return sqrt(x * x + y * y + z * z);
}
float magnitudeSq() {
return x * x + y * y + z * z;
}
Direction normalize() {
float m = magnitudeSq();
if (m != 0.0 && m != 1.0) {
div(sqrt(m));
}
return this;
}
Direction normalize(Direction in) {
float m = magnitudeSq();
if (m != 0.0 && m != 1.0) {
m = 1.0 / sqrt(m);
x = in.x * m;
y = in.y * m;
z = in.z * m;
}
return this;
}
Direction randomPolar() {
return fromAngle(random(TWO_PI));
}
Direction randomSpherical() {
return fromAngle(random(TWO_PI), random(PI));
}
Direction set(float x, float y) {
this.x = x;
this.y = y;
return this;
}
}