-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUtil.pde
More file actions
36 lines (29 loc) · 1.03 KB
/
Util.pde
File metadata and controls
36 lines (29 loc) · 1.03 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
// Constrain the angle to be within a certain range of the anchor
float constrainAngle(float angle, float anchor, float constraint) {
if (abs(relativeAngleDiff(angle, anchor)) <= constraint) {
return simplifyAngle(angle);
}
if (relativeAngleDiff(angle, anchor) > constraint) {
return simplifyAngle(anchor - constraint);
}
return simplifyAngle(anchor + constraint);
}
// i.e. How many radians do you need to turn the angle to match the anchor?
float relativeAngleDiff(float angle, float anchor) {
// Since angles are represented by values in [0, 2pi), it's helpful to rotate
// the coordinate space such that PI is at the anchor. That way we don't have
// to worry about the "seam" between 0 and 2pi.
angle = simplifyAngle(angle + PI - anchor);
anchor = PI;
return anchor - angle;
}
// Simplify the angle to be in the range [0, 2pi)
float simplifyAngle(float angle) {
while (angle >= TWO_PI) {
angle -= TWO_PI;
}
while (angle < 0) {
angle += TWO_PI;
}
return angle;
}