-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathUtil.pde
41 lines (33 loc) · 1.21 KB
/
Util.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
// Constrain the vector to be at a certain range of the anchor
PVector constrainDistance(PVector pos, PVector anchor, float constraint) {
return PVector.add(anchor, PVector.sub(pos, anchor).setMag(constraint));
}
// 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;
}