diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts new file mode 100644 index 0000000..a3e5e64 --- /dev/null +++ b/src/FractalSVG.ts @@ -0,0 +1,91 @@ +import { Polygon, Point } from "./Polygon"; + +function inwardSpiral(input:Polygon, scalar:number): Polygon +{ + const output = new Polygon(); + + let a:Point; + let b:Point; + let cx: number; + let cy: number; + for(let i=0;i`); + + return svg.join(""); +} + +export function fractalToSVG(fractal:Polygon[][], sideLength:number): string +{ + const svg = [``]; + + let colorStrength = 0; + const initialColorStrength = 20; + for(const segment of fractal) + { + colorStrength = initialColorStrength; + for(const iteration of segment) + { + svg.push(polygonToSVG(iteration, `fill:rgb(0,0,${Math.floor(colorStrength)})`)); + colorStrength += (255-initialColorStrength)/segment.length; + } + } + + svg.push(""); + + return svg.join(""); +} + +export function createSquare(side:number): Polygon +{ + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,side)); + square.push(new Point(side,side)); + square.push(new Point(side,0)); + return square; +} + +export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number): Polygon[][] +{ + const fractal = [ + createInwardSpiral(shape, iterations, shrinkRate), + ]; + return fractal; +} diff --git a/src/FractalSVG.ts b/src/FractalSVG.ts new file mode 100644 index 0000000..a3e5e64 --- /dev/null +++ b/src/FractalSVG.ts @@ -0,0 +1,91 @@ +import { Polygon, Point } from "./Polygon"; + +function inwardSpiral(input:Polygon, scalar:number): Polygon +{ + const output = new Polygon(); + + let a:Point; + let b:Point; + let cx: number; + let cy: number; + for(let i=0;i`); + + return svg.join(""); +} + +export function fractalToSVG(fractal:Polygon[][], sideLength:number): string +{ + const svg = [``]; + + let colorStrength = 0; + const initialColorStrength = 20; + for(const segment of fractal) + { + colorStrength = initialColorStrength; + for(const iteration of segment) + { + svg.push(polygonToSVG(iteration, `fill:rgb(0,0,${Math.floor(colorStrength)})`)); + colorStrength += (255-initialColorStrength)/segment.length; + } + } + + svg.push(""); + + return svg.join(""); +} + +export function createSquare(side:number): Polygon +{ + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,side)); + square.push(new Point(side,side)); + square.push(new Point(side,0)); + return square; +} + +export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number): Polygon[][] +{ + const fractal = [ + createInwardSpiral(shape, iterations, shrinkRate), + ]; + return fractal; +} diff --git a/src/Polygon.ts b/src/Polygon.ts index 30cb2ca..9ffe96f 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -23,25 +23,3 @@ return newCorners; } } - -export function inwardSpiral(input:Polygon, scalar:number): Polygon -{ - const output = new Polygon(); - - let a:Point; - let b:Point; - let cx: number; - let cy: number; - for(let i=0;i`); + + return svg.join(""); +} + +export function fractalToSVG(fractal:Polygon[][], sideLength:number): string +{ + const svg = [``]; + + let colorStrength = 0; + const initialColorStrength = 20; + for(const segment of fractal) + { + colorStrength = initialColorStrength; + for(const iteration of segment) + { + svg.push(polygonToSVG(iteration, `fill:rgb(0,0,${Math.floor(colorStrength)})`)); + colorStrength += (255-initialColorStrength)/segment.length; + } + } + + svg.push(""); + + return svg.join(""); +} + +export function createSquare(side:number): Polygon +{ + const square = new Polygon(); + square.push(new Point(0,0)); + square.push(new Point(0,side)); + square.push(new Point(side,side)); + square.push(new Point(side,0)); + return square; +} + +export function inwardSpiralFractal(shape:Polygon, iterations:number, shrinkRate:number): Polygon[][] +{ + const fractal = [ + createInwardSpiral(shape, iterations, shrinkRate), + ]; + return fractal; +} diff --git a/src/Polygon.ts b/src/Polygon.ts index 30cb2ca..9ffe96f 100644 --- a/src/Polygon.ts +++ b/src/Polygon.ts @@ -23,25 +23,3 @@ return newCorners; } } - -export function inwardSpiral(input:Polygon, scalar:number): Polygon -{ - const output = new Polygon(); - - let a:Point; - let b:Point; - let cx: number; - let cy: number; - for(let i=0;i`); - - return svg.join(""); -} - app.get("/randomFractal.svg", (req, res) => { - const svg = [``]; + const sideLength = 400; + const iterations = 50; + const shrinkRate = 0.1; - let colorStrength = 0; - const initialColorStrength = 20; - for(const cycle of cycles) - { - colorStrength = initialColorStrength; - for(const iteration of cycle) - { - svg.push(toSVG(iteration, `fill:rgb(0,0,${Math.floor(colorStrength)})`)); - colorStrength += (255-initialColorStrength)/cycle.length; - } - } - - svg.push(""); + const startShape = createSquare(sideLength); + const fractal = inwardSpiralFractal(startShape, iterations, shrinkRate); + const svg = fractalToSVG(fractal, sideLength); res.setHeader("content-type", "image/svg+xml"); - res.send(svg.join("")); + res.send(svg); }); app.use(express.static(join(".", "public")));