-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstars.ts
180 lines (150 loc) · 3.86 KB
/
stars.ts
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
// simple euclidean algorithm
export function gcd(a: number, b: number): number {
if (a === b) {
return a;
}
return a < b ? gcd(a, b - a) : gcd(b, a - b);
}
// find possible jumps if we want to make a star from
// a given number of points lying on a circle
export function jumps(points: number): number[] {
const result: number[] = [];
if (points < 5) {
// no stars under 5 points
return result;
}
for (let i = 2; i < points - 1; ++i) {
// jumps will reach all the points iff
// the jump interval and the number of
// points are coprime
if (gcd(points, i) === 1) {
// the jump set of (points - x) is just the
// jump set of x in reverse order, so we can
// safely ignore it
if (!result.includes(points - i)) {
result.push(i);
}
}
}
return result;
}
export function edges(points: number, jump: number): [number, number][] {
const result: [number, number][] = [];
let start = 0;
do {
result.push([start, (start + jump) % points]);
start = (start + jump) % points;
} while (start !== 0);
return result;
}
export function pointCoords(
points: number,
radius: number,
): [number, number][] {
const angle = 2 * Math.PI / points;
const coords: [number, number][] = [];
for (let i = 0; i < points; ++i) {
// we want the first point to be at the middle top, so
// we're basically making a 90° counter-clockwise rotation
// (assuming the star center is at (0, 0))
coords.push([
-Math.sin(i * angle) * radius,
Math.cos(i * angle) * radius,
]);
}
return coords;
}
export function pathLength(
pointCoords: [number, number][],
jump: number,
): number {
// since gcd(points, jump) = 1, lcm = points * jump
const jumpCount = pointCoords.length;
const [x1, y1] = pointCoords[0];
const [x2, y2] = pointCoords[jump];
const jumpLength = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
return jumpLength * jumpCount;
}
export function svgCoords(
coords: [number, number][],
radius: number,
): [string, string][] {
// translate coords into the svg coordinates
// system ((0, 0) to (500, 500)) and fix decimals
return coords.map(([x, y]) => {
return [
(x + radius).toFixed(3),
(radius - y).toFixed(3),
];
});
}
export function svgPath(
svgCoords: [string, string][],
edges: [number, number][],
): string {
const svgEdges = edges.map((
[start, end],
) => ([svgCoords[start], svgCoords[end]]));
return svgEdges.reduce((acc, [start, end]) => {
if (acc === "") {
return `M ${start[0]} ${start[1]} L ${end[0]} ${end[1]}`;
}
return `${acc} L ${end[0]} ${end[1]}`;
}, "");
}
interface Star {
points: number;
jump: number;
path: string;
pathLength: string;
availableJumps: number[];
}
export function star(
radius: number,
points: number,
jump?: number,
): Star | undefined {
const availableJumps = jumps(points);
let actualJump: number;
if (availableJumps.length === 0) {
return;
}
if (jump) {
if (!availableJumps.includes(jump)) {
return;
}
actualJump = jump;
} else {
const sorted = availableJumps.sort((a, b) =>
points % a > points % b ? -1 : 1
);
actualJump = sorted[0];
}
const coords = pointCoords(points, radius);
const length = pathLength(coords, actualJump);
const path = svgPath(svgCoords(coords, radius), edges(points, actualJump));
return {
points,
jump: actualJump,
path,
pathLength: length.toFixed(3),
availableJumps,
};
}
interface StarData {
points: number;
availableJumps: number[];
}
export function starData(start: number, end: number): StarData[] {
const infos: StarData[] = [];
for (let i = start; i <= end; ++i) {
const availableJumps = jumps(i);
if (availableJumps.length > 0) {
infos.push({
points: i,
availableJumps
});
}
}
return infos;
}