-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotationalPolygon.js
53 lines (45 loc) · 1.46 KB
/
RotationalPolygon.js
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
import { Polygon } from "./Polygon.js";
export class RotationalPolygon {
constructor(points) {
this.xpoints = [];
this.ypoints = [];
this.npoints = 0;
this.distances = [];
this.angles = [];
this.currentAngle = 0;
for (let i = 0; i < points.length; i++) {
this.addPoint(points[i][0], points[i][1]);
}
this.polygon = new Polygon(this.xpoints, this.ypoints, this.npoints);
}
addDistAnglePoint(distance, angle) {
this.distances.push(distance);
this.angles.push(angle * 0.017453292519943295);
let x = Math.cos(this.angles[this.npoints]) * distance;
let y = Math.sin(this.angles[this.npoints]) * distance;
this.xpoints.push(x);
this.ypoints.push(y);
this.npoints++;
}
addPoint(xval, yval) {
let i = Math.hypot(xval, yval);
let d = Math.atan2(yval, xval) * 57.29577951308232;
this.addDistAnglePoint(i, d);
}
rotate(angle) {
this.setAngle((this.currentAngle + angle * 0.017453292519943295) % 360);
}
setAngle(angle) {
if (angle == this.currentAngle) return;
this.currentAngle = angle;
this.currentAngle %= 360;
for (let i = 0; i < this.npoints; i++) {
this.xpoints[i] = Math.cos(this.angles[i] + this.currentAngle) *
this.distances[i];
this.ypoints[i] = Math.sin(this.angles[i] + this.currentAngle) *
this.distances[i];
}
// update the polygon with the new points
this.polygon = new Polygon(this.xpoints, this.ypoints, this.npoints);
}
}