Add induction lines

This commit is contained in:
Florine W. Dekker 2021-10-31 15:42:30 +01:00
parent a2eae95e5b
commit 480d9b899d
Signed by: FWDekker
GPG Key ID: D3DCFAA8A4560BE0
1 changed files with 45 additions and 4 deletions

View File

@ -83,6 +83,19 @@ class Line {
}
/**
* Returns the sum of a slice of this `Line`'s [LineSegment]s.
*
* @param start the start of the slice to sum over
* @param end the end of the slice to sum over
* @return the sum of a slice of this `Line`'s [LineSegment]s
*/
sumSegmentsSlice(start?: number, end?: number): LineSegment {
const slice = this.segments.slice(start, end);
return new LineSegment(slice.reduce((acc, it) => acc + it.x, 0), slice.reduce((acc, it) => acc + it.y, 0));
}
/**
* Returns `true` if and only if this `Line` is exactly the same as `other`.
*
@ -104,12 +117,39 @@ doAfterLoad(() => {
const ctx = canvas.getContext("2d");
// State
const lines: Line[] = [];
lines.push(new Line(10));
const lines: Line[] = [new Line(1), new Line(2)];
let step = 0;
setInterval(() => {
const stepLine = Math.floor(step / 25);
const inductionLine = stepLine + 1;
const superInductionLine = stepLine + 2;
// Add new low-level segment
const length = 5 ** (2 + stepLine);
const angle = (random_normal() - 0.5) * Math.PI * 2;
lines[0].segments.push(new LineSegment(Math.cos(angle) * 25, Math.sin(angle) * 25));
}, 1000);
lines[stepLine].segments.push(new LineSegment(Math.cos(angle) * length, Math.sin(angle) * length));
// Add first-level induction line
if ((step + 1) % 5 === 0) {
if (lines.length <= inductionLine) {
lines[inductionLine] = new Line(inductionLine + 1);
}
lines[inductionLine].segments.push(lines[stepLine].sumSegmentsSlice(-5));
}
// Add second-level induction line
if ((step + 1) % 25 === 0) {
if (lines.length <= superInductionLine) {
lines[superInductionLine] = new Line(superInductionLine + 1);
}
lines[superInductionLine].segments.push(lines[inductionLine].sumSegmentsSlice(-5));
}
step++;
}, 250);
// Resize
const resize = () => {
@ -136,6 +176,7 @@ doAfterLoad(() => {
// Lines
ctx.strokeStyle = "#ffffff";
for (const line of lines) {
ctx.lineWidth = line.thickness;
ctx.beginPath();
ctx.moveTo(center.x, center.y);