-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-circle.js
More file actions
79 lines (66 loc) · 1.62 KB
/
unit-circle.js
File metadata and controls
79 lines (66 loc) · 1.62 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
const { atan2, cos, PI, sin } = Math;
export default class UnitCircle {
constructor(drawCallBack) {
this.draw = drawCallBack;
this.snapping = false;
this.showRadians = false;
this.showSnapPoints = false;
this.snapPoints = [];
this.createSnapPoints();
}
createSnapPoints() {
// Add every multiple of 30°
let max = 360 / 30 + 1;
for (let i = 0; i < max; ++i) {
this.snapPoints.push(i * PI / 6);
}
// Add multiples of 45° (skipping multiples of 90°)
max = 360 / 45;
for (let i = 1; i < max; i += 2) {
this.snapPoints.push(i * PI / 4);
}
this.snapPoints.sort();
}
update({x, y}) {
this.angle = (atan2(y, x) + (2 * PI)) % (2 * PI);
if (this.snapping) {
const snapIndex = this.snapPoints.findIndex((point) => {
return point > this.angle;
});
const a = this.snapPoints[snapIndex - 1];
const b = this.snapPoints[snapIndex];
const middle = a + ((b - a) / 2);
if (middle < this.angle) {
this.angle = this.snapPoints[snapIndex];
} else {
this.angle = this.snapPoints[snapIndex - 1];
}
}
this.draw();
}
getSin() {
return sin(this.angle);
}
getCos() {
return cos(this.angle);
}
getAngleDegrees() {
const angle = (this.angle * 180 / PI);
return {
angle,
angleAlt: -360 + angle,
};
}
toggleSnapping() {
this.snapping = !this.snapping;
this.draw();
}
toggleSnapPoints() {
this.showSnapPoints = !this.showSnapPoints;
this.draw();
}
toggleRadians() {
this.showRadians = !this.showRadians;
this.draw();
}
}