-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
217 lines (185 loc) · 7.79 KB
/
script.js
File metadata and controls
217 lines (185 loc) · 7.79 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const RESOLUTION = 800;
let canvas;
let ctxt;
let polygon;
window.onload = () => {
canvas = document.getElementById("canvas");
canvas.width = RESOLUTION;
canvas.height = RESOLUTION;
ctxt = canvas.getContext("2d");
polygon = new StarPolygon(RESOLUTION / 2, RESOLUTION / 2, 20, RESOLUTION / 10, RESOLUTION / 2.25, 1);
render();
}
function getRotation(angle1, angle2) {
let rotation = angle2 - angle1;
if (rotation > Math.PI) {
rotation -= 2 * Math.PI;
}
if (rotation < -Math.PI) {
rotation += 2 * Math.PI;
}
return rotation;
}
function render() {
let showMaxEdgeAngle = document.getElementById("show-max-edge-angle").checked;
let showSortedEdgeSubSet1 = document.getElementById("show-sorted-edge-sub-set-1").checked;
let showSortedEdgeSubSet2 = document.getElementById("show-sorted-edge-sub-set-2").checked;
ctxt.clearRect(0, 0, RESOLUTION, RESOLUTION);
polygon.render(ctxt, showMaxEdgeAngle, showSortedEdgeSubSet1, showSortedEdgeSubSet2);
}
class StarPolygon {
constructor(centerX, centerY, maxVertexCount, minDistance, maxDistance, vertexProbability) {
this.points = [];
for (let i = 0; i < maxVertexCount; i++) {
if (Math.random() > vertexProbability) {
continue;
}
let angle = 2 * Math.PI * (i / maxVertexCount);
let distance = Math.random() * (maxDistance - minDistance) + minDistance;
this.points.push([
centerX + distance * Math.cos(angle),
centerY + distance * Math.sin(angle),
]);
}
this.edgeAngles = [];
for (let i = 0; i < this.points.length - 1; i++) {
this.edgeAngles.push(
Math.atan2(this.points[i][1] - this.points[i + 1][1], this.points[i][0] - this.points[i + 1][0])
);
}
this.edgeAngles.push(
Math.atan2(this.points[this.points.length - 1][1] - this.points[0][1], this.points[this.points.length - 1][0] - this.points[0][0])
);
this.rotations = [];
for (let i = 0; i < this.edgeAngles.length - 1; i++) {
this.rotations.push(
getRotation(this.edgeAngles[i], this.edgeAngles[i + 1])
);
}
this.rotations.push(
getRotation(this.edgeAngles[this.edgeAngles.length - 1], this.edgeAngles[0])
);
// compute offset so when computing chains we don't accidentally start inside a chain but instead start at a definite chain start
let lowestRotation = Math.PI;
let indexOffset = 0;
for (let i = 0; i < this.rotations.length; i++) {
if (this.rotations[i] < lowestRotation) {
lowestRotation = this.rotations[i];
indexOffset = (i + 1) % this.rotations.length;
}
}
let maxRotationSum = 0;
let currentRotationSum = 0;
let currentChainIndex1 = indexOffset;
let currentChainIndex2 = 0;
this.maxAngleIndex1 = 0;
this.maxAngleIndex2 = 0;
for (let i = 0; i < this.rotations.length; i++) {
let index = (i + indexOffset) % this.rotations.length;
let rotation = this.rotations[index];
let nextValue = currentRotationSum + rotation;
if (nextValue <= 0) {
currentRotationSum = 0;
currentChainIndex1 = (index + 1) % this.rotations.length;
} else {
currentRotationSum = nextValue;
currentChainIndex2 = (index + 1) % this.rotations.length;
}
if (currentRotationSum > maxRotationSum) {
maxRotationSum = currentRotationSum;
this.maxAngleIndex1 = currentChainIndex1;
this.maxAngleIndex2 = currentChainIndex2;
}
}
// compute the two edge sets
this.sortedEdgeSubSet1 = [this.maxAngleIndex1];
this.sortedEdgeSubSet2 = [];
let currentRotation = 0;
let maxRotation = -Math.PI;
for (let i = 0; i < this.rotations.length; i++) {
let index = (this.maxAngleIndex1 + i) % this.rotations.length;
if (index === this.maxAngleIndex2) {
this.sortedEdgeSubSet1.push(index);
break;
}
currentRotation += this.rotations[index];
if (currentRotation > maxRotation) {
maxRotation = currentRotation;
this.sortedEdgeSubSet1.push((index + 1) % this.rotations.length);
}
}
currentRotation = 0;
maxRotation = Math.PI;
for (let i = 0; i < this.rotations.length; i++) {
let index = (this.maxAngleIndex2 - i + this.rotations.length) % this.rotations.length;
if (index === this.maxAngleIndex1) {
this.sortedEdgeSubSet2.push(index);
break;
}
currentRotation -= this.rotations[index];
if (currentRotation < maxRotation) {
maxRotation = currentRotation;
this.sortedEdgeSubSet2.push(index);
}
}
// here it would be possible to compute the kernel in linear time
// by computing the convex-hulls of the dual-points of the edges (possible because sets are sorted)
// and merging the resulting two convex polygons
}
render(ctxt, showMaxEdgeAngle, showSortedEdgeSubSet1, showSortedEdgeSubSet2) {
ctxt.lineWidth = 5;
ctxt.strokeStyle = "#FFF";
ctxt.beginPath();
ctxt.moveTo(this.points[0][0], this.points[0][1]);
for (let i = 1; i < this.points.length; i++) {
ctxt.lineTo(this.points[i][0], this.points[i][1]);
}
ctxt.closePath();
ctxt.stroke();
ctxt.setLineDash([10, 10]);
if(showSortedEdgeSubSet1) {
ctxt.strokeStyle = "#F00";
for (let i = 0; i < this.sortedEdgeSubSet1.length; i++) {
let index1 = this.sortedEdgeSubSet1[i]
let index2 = (this.sortedEdgeSubSet1[i] + 1) % this.points.length
ctxt.beginPath();
ctxt.moveTo(this.points[index1][0], this.points[index1][1]);
ctxt.lineTo(this.points[index2][0], this.points[index2][1]);
ctxt.stroke();
}
}
if(showSortedEdgeSubSet2) {
ctxt.strokeStyle = "#00F";
ctxt.lineDashOffset = 10;
for (let i = 0; i < this.sortedEdgeSubSet2.length; i++) {
let index1 = this.sortedEdgeSubSet2[i]
let index2 = (this.sortedEdgeSubSet2[i] + 1) % this.points.length
ctxt.beginPath();
ctxt.moveTo(this.points[index1][0], this.points[index1][1]);
ctxt.lineTo(this.points[index2][0], this.points[index2][1]);
ctxt.stroke();
}
}
ctxt.lineDashOffset = 0;
ctxt.setLineDash([]);
let index3 = (this.maxAngleIndex1 + 1) % this.points.length;
let index4 = (this.maxAngleIndex2 + 1) % this.points.length;
if(showMaxEdgeAngle) {
ctxt.strokeStyle = "#F00";
ctxt.beginPath();
ctxt.moveTo(this.points[this.maxAngleIndex1][0], this.points[this.maxAngleIndex1][1]);
ctxt.lineTo(this.points[index3][0], this.points[index3][1]);
ctxt.stroke();
ctxt.strokeStyle = "#00F";
ctxt.beginPath();
ctxt.moveTo(this.points[this.maxAngleIndex2][0], this.points[this.maxAngleIndex2][1]);
ctxt.lineTo(this.points[index4][0], this.points[index4][1]);
ctxt.stroke();
}
for (let i = 0; i < this.points.length; i++) {
ctxt.beginPath();
ctxt.arc(this.points[i][0], this.points[i][1], 7, 0, 2 * Math.PI);
ctxt.fill();
}
}
}