diff --git a/src/main/js/Main.ts b/src/main/js/Main.ts index 5cc5b0d..4be2a35 100644 --- a/src/main/js/Main.ts +++ b/src/main/js/Main.ts @@ -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);