diff --git a/src/BaseShapes.ts b/src/BaseShapes.ts new file mode 100644 index 0000000..dacf94c --- /dev/null +++ b/src/BaseShapes.ts @@ -0,0 +1,56 @@ +import { Polygon, Point } from "./Polygon"; +import { zip } from "./utils"; + +export function createHexagonalBase(sideLength: number): Polygon[] { + const max = sideLength - 1; + const center = new Point(max / 2, max / 2); + const hexagon = createHexagonInSquare(max); + const lines = zip(hexagon, hexagon, 1); + const base = lines.map(([a, b]) => { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/BaseShapes.ts b/src/BaseShapes.ts new file mode 100644 index 0000000..dacf94c --- /dev/null +++ b/src/BaseShapes.ts @@ -0,0 +1,56 @@ +import { Polygon, Point } from "./Polygon"; +import { zip } from "./utils"; + +export function createHexagonalBase(sideLength: number): Polygon[] { + const max = sideLength - 1; + const center = new Point(max / 2, max / 2); + const hexagon = createHexagonInSquare(max); + const lines = zip(hexagon, hexagon, 1); + const base = lines.map(([a, b]) => { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts deleted file mode 100644 index 4797d41..0000000 --- a/src/FractalSVG.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { Polygon, Point } from "./Polygon"; - -/** - * Concatenates two arrays component wise. - * a[i], b[i+offset] => [a[i], b[i+offset]] - */ -function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> -{ - if(offset < 0) - { - throw new Error("The offset is negative."); - } - if(offset % 1 !== 0) - { - throw new Error("The offset is not an integer."); - } - if(a.length !== b.length) - { - throw new Error("Input arrays have different lengths."); - } - const output:Array<[S, T]> = []; - const length = a.length; - for(let i=0;i - midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); - return new Polygon(...newPoints); -} - -export function createInwardSpiral( - startShape:Polygon, - maxiterations:number, - shrinkRate:number, - randomizeShrinkRate:number, -): Polygon[] -{ - const cycle:Polygon[] = [startShape]; - let pNext:Polygon; - for(let i=0;i { - return new Polygon(...[ - a, b, center, - ]); - }); - - const corner1 = new Point(sideLength, 0); - const corner2 = new Point(0,0); - const corner3 = new Point(0, sideLength); - const corner4 = new Point(sideLength, sideLength); - - base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); - base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); - base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); - base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); - - return base; -} - -export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number, randomize:number): Polygon[][] -{ - const fractal = [ - createInwardSpiral(shape, iterations, shrinkRate, randomize), - ]; - return fractal; -} - -export function createTriangle(sideLength:number): Polygon -{ - return new Polygon( - new Point(sideLength/2, 0), - new Point(0,sideLength), - new Point(sideLength, sideLength), - ); -} diff --git a/src/BaseShapes.ts b/src/BaseShapes.ts new file mode 100644 index 0000000..dacf94c --- /dev/null +++ b/src/BaseShapes.ts @@ -0,0 +1,56 @@ +import { Polygon, Point } from "./Polygon"; +import { zip } from "./utils"; + +export function createHexagonalBase(sideLength: number): Polygon[] { + const max = sideLength - 1; + const center = new Point(max / 2, max / 2); + const hexagon = createHexagonInSquare(max); + const lines = zip(hexagon, hexagon, 1); + const base = lines.map(([a, b]) => { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts deleted file mode 100644 index 4797d41..0000000 --- a/src/FractalSVG.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { Polygon, Point } from "./Polygon"; - -/** - * Concatenates two arrays component wise. - * a[i], b[i+offset] => [a[i], b[i+offset]] - */ -function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> -{ - if(offset < 0) - { - throw new Error("The offset is negative."); - } - if(offset % 1 !== 0) - { - throw new Error("The offset is not an integer."); - } - if(a.length !== b.length) - { - throw new Error("Input arrays have different lengths."); - } - const output:Array<[S, T]> = []; - const length = a.length; - for(let i=0;i - midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); - return new Polygon(...newPoints); -} - -export function createInwardSpiral( - startShape:Polygon, - maxiterations:number, - shrinkRate:number, - randomizeShrinkRate:number, -): Polygon[] -{ - const cycle:Polygon[] = [startShape]; - let pNext:Polygon; - for(let i=0;i { - return new Polygon(...[ - a, b, center, - ]); - }); - - const corner1 = new Point(sideLength, 0); - const corner2 = new Point(0,0); - const corner3 = new Point(0, sideLength); - const corner4 = new Point(sideLength, sideLength); - - base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); - base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); - base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); - base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); - - return base; -} - -export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number, randomize:number): Polygon[][] -{ - const fractal = [ - createInwardSpiral(shape, iterations, shrinkRate, randomize), - ]; - return fractal; -} - -export function createTriangle(sideLength:number): Polygon -{ - return new Polygon( - new Point(sideLength/2, 0), - new Point(0,sideLength), - new Point(sideLength, sideLength), - ); -} diff --git a/src/Polygon.ts b/src/Polygon.ts index 9ffe96f..85430f6 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -1,12 +1,9 @@ export class Point { - public x:number; - public y:number; - constructor(x:number, y:number) - { - this.x = x; - this.y = y; - } + constructor( + public x:number, + public y:number, + ) {} } export class Polygon extends Array diff --git a/src/BaseShapes.ts b/src/BaseShapes.ts new file mode 100644 index 0000000..dacf94c --- /dev/null +++ b/src/BaseShapes.ts @@ -0,0 +1,56 @@ +import { Polygon, Point } from "./Polygon"; +import { zip } from "./utils"; + +export function createHexagonalBase(sideLength: number): Polygon[] { + const max = sideLength - 1; + const center = new Point(max / 2, max / 2); + const hexagon = createHexagonInSquare(max); + const lines = zip(hexagon, hexagon, 1); + const base = lines.map(([a, b]) => { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts deleted file mode 100644 index 4797d41..0000000 --- a/src/FractalSVG.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { Polygon, Point } from "./Polygon"; - -/** - * Concatenates two arrays component wise. - * a[i], b[i+offset] => [a[i], b[i+offset]] - */ -function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> -{ - if(offset < 0) - { - throw new Error("The offset is negative."); - } - if(offset % 1 !== 0) - { - throw new Error("The offset is not an integer."); - } - if(a.length !== b.length) - { - throw new Error("Input arrays have different lengths."); - } - const output:Array<[S, T]> = []; - const length = a.length; - for(let i=0;i - midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); - return new Polygon(...newPoints); -} - -export function createInwardSpiral( - startShape:Polygon, - maxiterations:number, - shrinkRate:number, - randomizeShrinkRate:number, -): Polygon[] -{ - const cycle:Polygon[] = [startShape]; - let pNext:Polygon; - for(let i=0;i { - return new Polygon(...[ - a, b, center, - ]); - }); - - const corner1 = new Point(sideLength, 0); - const corner2 = new Point(0,0); - const corner3 = new Point(0, sideLength); - const corner4 = new Point(sideLength, sideLength); - - base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); - base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); - base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); - base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); - - return base; -} - -export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number, randomize:number): Polygon[][] -{ - const fractal = [ - createInwardSpiral(shape, iterations, shrinkRate, randomize), - ]; - return fractal; -} - -export function createTriangle(sideLength:number): Polygon -{ - return new Polygon( - new Point(sideLength/2, 0), - new Point(0,sideLength), - new Point(sideLength, sideLength), - ); -} diff --git a/src/Polygon.ts b/src/Polygon.ts index 9ffe96f..85430f6 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -1,12 +1,9 @@ export class Point { - public x:number; - public y:number; - constructor(x:number, y:number) - { - this.x = x; - this.y = y; - } + constructor( + public x:number, + public y:number, + ) {} } export class Polygon extends Array diff --git a/src/SpiralFractal.ts b/src/SpiralFractal.ts new file mode 100644 index 0000000..3a3c477 --- /dev/null +++ b/src/SpiralFractal.ts @@ -0,0 +1,29 @@ +import { Polygon } from "./Polygon"; +import { zip, midPoint, randomizeScalar } from "./utils"; + +function inwardSpiralStep(input:Polygon, scalar:number, randomizeShrinkRate:number): Polygon +{ + const lines = zip(input, input, 1); + const newPoints = lines.map(([a,b]) => + midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); + return new Polygon(...newPoints); +} + +export function createInwardSpiral( + startShape:Polygon, + maxiterations:number, + shrinkRate:number, + randomizeShrinkRate:number, +): Polygon[] +{ + const cycle:Polygon[] = [startShape]; + let pNext:Polygon; + for(let i=0;i { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts deleted file mode 100644 index 4797d41..0000000 --- a/src/FractalSVG.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { Polygon, Point } from "./Polygon"; - -/** - * Concatenates two arrays component wise. - * a[i], b[i+offset] => [a[i], b[i+offset]] - */ -function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> -{ - if(offset < 0) - { - throw new Error("The offset is negative."); - } - if(offset % 1 !== 0) - { - throw new Error("The offset is not an integer."); - } - if(a.length !== b.length) - { - throw new Error("Input arrays have different lengths."); - } - const output:Array<[S, T]> = []; - const length = a.length; - for(let i=0;i - midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); - return new Polygon(...newPoints); -} - -export function createInwardSpiral( - startShape:Polygon, - maxiterations:number, - shrinkRate:number, - randomizeShrinkRate:number, -): Polygon[] -{ - const cycle:Polygon[] = [startShape]; - let pNext:Polygon; - for(let i=0;i { - return new Polygon(...[ - a, b, center, - ]); - }); - - const corner1 = new Point(sideLength, 0); - const corner2 = new Point(0,0); - const corner3 = new Point(0, sideLength); - const corner4 = new Point(sideLength, sideLength); - - base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); - base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); - base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); - base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); - - return base; -} - -export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number, randomize:number): Polygon[][] -{ - const fractal = [ - createInwardSpiral(shape, iterations, shrinkRate, randomize), - ]; - return fractal; -} - -export function createTriangle(sideLength:number): Polygon -{ - return new Polygon( - new Point(sideLength/2, 0), - new Point(0,sideLength), - new Point(sideLength, sideLength), - ); -} diff --git a/src/Polygon.ts b/src/Polygon.ts index 9ffe96f..85430f6 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -1,12 +1,9 @@ export class Point { - public x:number; - public y:number; - constructor(x:number, y:number) - { - this.x = x; - this.y = y; - } + constructor( + public x:number, + public y:number, + ) {} } export class Polygon extends Array diff --git a/src/SpiralFractal.ts b/src/SpiralFractal.ts new file mode 100644 index 0000000..3a3c477 --- /dev/null +++ b/src/SpiralFractal.ts @@ -0,0 +1,29 @@ +import { Polygon } from "./Polygon"; +import { zip, midPoint, randomizeScalar } from "./utils"; + +function inwardSpiralStep(input:Polygon, scalar:number, randomizeShrinkRate:number): Polygon +{ + const lines = zip(input, input, 1); + const newPoints = lines.map(([a,b]) => + midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); + return new Polygon(...newPoints); +} + +export function createInwardSpiral( + startShape:Polygon, + maxiterations:number, + shrinkRate:number, + randomizeShrinkRate:number, +): Polygon[] +{ + const cycle:Polygon[] = [startShape]; + let pNext:Polygon; + for(let i=0;i { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts deleted file mode 100644 index 4797d41..0000000 --- a/src/FractalSVG.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { Polygon, Point } from "./Polygon"; - -/** - * Concatenates two arrays component wise. - * a[i], b[i+offset] => [a[i], b[i+offset]] - */ -function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> -{ - if(offset < 0) - { - throw new Error("The offset is negative."); - } - if(offset % 1 !== 0) - { - throw new Error("The offset is not an integer."); - } - if(a.length !== b.length) - { - throw new Error("Input arrays have different lengths."); - } - const output:Array<[S, T]> = []; - const length = a.length; - for(let i=0;i - midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); - return new Polygon(...newPoints); -} - -export function createInwardSpiral( - startShape:Polygon, - maxiterations:number, - shrinkRate:number, - randomizeShrinkRate:number, -): Polygon[] -{ - const cycle:Polygon[] = [startShape]; - let pNext:Polygon; - for(let i=0;i { - return new Polygon(...[ - a, b, center, - ]); - }); - - const corner1 = new Point(sideLength, 0); - const corner2 = new Point(0,0); - const corner3 = new Point(0, sideLength); - const corner4 = new Point(sideLength, sideLength); - - base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); - base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); - base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); - base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); - - return base; -} - -export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number, randomize:number): Polygon[][] -{ - const fractal = [ - createInwardSpiral(shape, iterations, shrinkRate, randomize), - ]; - return fractal; -} - -export function createTriangle(sideLength:number): Polygon -{ - return new Polygon( - new Point(sideLength/2, 0), - new Point(0,sideLength), - new Point(sideLength, sideLength), - ); -} diff --git a/src/Polygon.ts b/src/Polygon.ts index 9ffe96f..85430f6 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -1,12 +1,9 @@ export class Point { - public x:number; - public y:number; - constructor(x:number, y:number) - { - this.x = x; - this.y = y; - } + constructor( + public x:number, + public y:number, + ) {} } export class Polygon extends Array diff --git a/src/SpiralFractal.ts b/src/SpiralFractal.ts new file mode 100644 index 0000000..3a3c477 --- /dev/null +++ b/src/SpiralFractal.ts @@ -0,0 +1,29 @@ +import { Polygon } from "./Polygon"; +import { zip, midPoint, randomizeScalar } from "./utils"; + +function inwardSpiralStep(input:Polygon, scalar:number, randomizeShrinkRate:number): Polygon +{ + const lines = zip(input, input, 1); + const newPoints = lines.map(([a,b]) => + midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); + return new Polygon(...newPoints); +} + +export function createInwardSpiral( + startShape:Polygon, + maxiterations:number, + shrinkRate:number, + randomizeShrinkRate:number, +): Polygon[] +{ + const cycle:Polygon[] = [startShape]; + let pNext:Polygon; + for(let i=0;i { + return new Polygon(...[ + a, b, center, + ]); + }); + const corner1 = new Point(max, 0); + const corner2 = new Point(0, 0); + const corner3 = new Point(0, max); + const corner4 = new Point(max, max); + base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); + base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); + base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); + base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); + return base; +} + +export function createSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,max)); + square.push(new Point(max,max)); + square.push(new Point(max,0)); + return square; +} + +export function createHexagonInSquare(sideLength:number): Polygon +{ + const max = sideLength - 1; + return new Polygon(...[ + new Point(max, max/2), + new Point(3 * max / 4, 0), + new Point(max / 4, 0), + new Point(0, max / 2), + new Point(max/4, max), + new Point(3*max/4, max), + ]); +} + +export function createTriangle(sideLength:number): Polygon +{ + return new Polygon( + new Point(sideLength/2, 0), + new Point(0,sideLength), + new Point(sideLength, sideLength), + ); +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts deleted file mode 100644 index 4797d41..0000000 --- a/src/FractalSVG.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { Polygon, Point } from "./Polygon"; - -/** - * Concatenates two arrays component wise. - * a[i], b[i+offset] => [a[i], b[i+offset]] - */ -function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> -{ - if(offset < 0) - { - throw new Error("The offset is negative."); - } - if(offset % 1 !== 0) - { - throw new Error("The offset is not an integer."); - } - if(a.length !== b.length) - { - throw new Error("Input arrays have different lengths."); - } - const output:Array<[S, T]> = []; - const length = a.length; - for(let i=0;i - midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); - return new Polygon(...newPoints); -} - -export function createInwardSpiral( - startShape:Polygon, - maxiterations:number, - shrinkRate:number, - randomizeShrinkRate:number, -): Polygon[] -{ - const cycle:Polygon[] = [startShape]; - let pNext:Polygon; - for(let i=0;i { - return new Polygon(...[ - a, b, center, - ]); - }); - - const corner1 = new Point(sideLength, 0); - const corner2 = new Point(0,0); - const corner3 = new Point(0, sideLength); - const corner4 = new Point(sideLength, sideLength); - - base.push(new Polygon(...[hexagon[0], corner1, hexagon[1]])); - base.push(new Polygon(...[hexagon[2], corner2, hexagon[3]])); - base.push(new Polygon(...[hexagon[3], corner3, hexagon[4]])); - base.push(new Polygon(...[hexagon[5], corner4, hexagon[0]])); - - return base; -} - -export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number, randomize:number): Polygon[][] -{ - const fractal = [ - createInwardSpiral(shape, iterations, shrinkRate, randomize), - ]; - return fractal; -} - -export function createTriangle(sideLength:number): Polygon -{ - return new Polygon( - new Point(sideLength/2, 0), - new Point(0,sideLength), - new Point(sideLength, sideLength), - ); -} diff --git a/src/Polygon.ts b/src/Polygon.ts index 9ffe96f..85430f6 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -1,12 +1,9 @@ export class Point { - public x:number; - public y:number; - constructor(x:number, y:number) - { - this.x = x; - this.y = y; - } + constructor( + public x:number, + public y:number, + ) {} } export class Polygon extends Array diff --git a/src/SpiralFractal.ts b/src/SpiralFractal.ts new file mode 100644 index 0000000..3a3c477 --- /dev/null +++ b/src/SpiralFractal.ts @@ -0,0 +1,29 @@ +import { Polygon } from "./Polygon"; +import { zip, midPoint, randomizeScalar } from "./utils"; + +function inwardSpiralStep(input:Polygon, scalar:number, randomizeShrinkRate:number): Polygon +{ + const lines = zip(input, input, 1); + const newPoints = lines.map(([a,b]) => + midPoint(a, b, randomizeScalar(scalar, randomizeShrinkRate, 0, 0.9))); + return new Polygon(...newPoints); +} + +export function createInwardSpiral( + startShape:Polygon, + maxiterations:number, + shrinkRate:number, + randomizeShrinkRate:number, +): Polygon[] +{ + const cycle:Polygon[] = [startShape]; + let pNext:Polygon; + for(let i=0;i [a[i], b[i+offset]] + */ +export function zip(a:S[], b:T[], offset:number = 0): Array<[S, T]> +{ + if(offset < 0) + { + throw new Error("The offset is negative."); + } + if(offset % 1 !== 0) + { + throw new Error("The offset is not an integer."); + } + if(a.length !== b.length) + { + throw new Error("Input arrays have different lengths."); + } + const output:Array<[S, T]> = []; + const length = a.length; + for(let i=0;i