-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest-tan.js
42 lines (36 loc) · 915 Bytes
/
test-tan.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
const coordToAngle = (x, y) => {
let deg = (Math.atan2(-y, x) * 180) / Math.PI;
// Pretty sure this can be simplified with a modulus, but can't see it
if (deg <= 90 && deg >= 0) {
deg = Math.abs(deg - 90);
} else if (deg < 0) {
deg = Math.abs(deg) + 90;
} else {
deg = 450 - deg;
}
return deg;
};
const memoizedCoordToAngle = (() => {
let lookup = {};
return ([y, x]) => {
const coord = `${x},${y}`;
if (lookup[coord] !== undefined) {
return coord;
}
return coordToAngle(x, y);
};
})();
const pts = [
[-4,0],[-4,1],[-4,2],[-4,3],[-4,4],
[-3,4],[-2,4],[-1,4],[0,4],
[1,4],[2,4],[3,4],[4,4],
[4,3],[4,2],[4,1],[4,0],
[4,-1],[4,-2],[4,-3],[4,-4],
[3,-4],[2,-4],[1,-4],[0,-4],
[-1,-4],[-2,-4],[-3,-4],[-4,-4],
[-4,-3],[-4,-2],[-4,-1]
];
for (let pt of pts) {
// These should be output in increasing order
console.log(pt, memoizedCoordToAngle(pt));
}