forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproof.ci.js
More file actions
41 lines (32 loc) · 994 Bytes
/
proof.ci.js
File metadata and controls
41 lines (32 loc) · 994 Bytes
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
export const findSequence = (start, prisms) => {
let { x, y, angle } = start;
const sequence = [];
while (true) {
const rad = (angle * Math.PI) / 180;
const dirX = Math.cos(rad);
const dirY = Math.sin(rad);
let nearest = null;
let nearestDist = Infinity;
for (const prism of prisms) {
const dx = prism.x - x;
const dy = prism.y - y;
const dist = dx * dirX + dy * dirY;
const baseTolerance = 1e-6;
if (dist <= baseTolerance) continue;
const crossProductSquared =
(dx - dist * dirX) ** 2 + (dy - dist * dirY) ** 2;
const relativeTolerance = baseTolerance * Math.max(1, dist * dist);
if (crossProductSquared >= relativeTolerance) continue;
if (dist < nearestDist) {
nearestDist = dist;
nearest = prism;
}
}
if (!nearest) break;
sequence.push(nearest.id);
x = nearest.x;
y = nearest.y;
angle = (angle + nearest.angle) % 360;
}
return sequence;
};