Newer
Older
fractals / src / app.ts
import * as express from "express";
import { join } from "path";
import { inwardSpiral, Point, Polygon } from "./Polygon";

const app = express();

const port = 8081;

const sideLenth = 270;

let P:Polygon;
P = new Polygon();
P.push(new Point(0,0));
P.push(new Point(0,sideLenth));
P.push(new Point(sideLenth,sideLenth));
P.push(new Point(sideLenth,0));

function createInwardSpiral(input:Polygon, maxiterations:number, scalar:number): Polygon[]
{
    const cycle:Polygon[] = [input];
    let pNext:Polygon;

    for(let i=0;i<maxiterations;i++)
    {
        pNext = inwardSpiral(input, scalar);
        cycle.push(pNext);
        input = pNext;
    }

    return cycle;
}

const iterations = 50;
const fractalCoefficient = 0.1;

const cycles = [
    createInwardSpiral(P, iterations, fractalCoefficient),
];

// P = P.translate(sideLenth,0);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(sideLenth,0);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(-2*sideLenth,sideLenth);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(sideLenth,0);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(sideLenth,0);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(-2*sideLenth,sideLenth);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(sideLenth,0);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

// P = P.translate(sideLenth,0);
// cycles.push(createInwardSpiral(P, iterations, fractalCoefficient));

function toSVG(polygon:Polygon, style:string): string
{
    const svg = [`<polygon points="`];
    for(const corner of polygon)
    {
        svg.push(`${corner.x},${corner.y} `);
    }
    svg.push(`" style="${style}" />`);

    return svg.join("");
}

app.get("/randomFractal.svg", (req, res) => {
    const svg = [`<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink" width="270" height="270"
 style="fill:none;stroke:purple;stroke-width:1">`];

    let s = 0;

    for(const cycle of cycles)
    {
        s = 20;
        for(const iteration of cycle)
        {
            svg.push(toSVG(iteration, `fill:rgb(0,0,${Math.floor(s)})`));
            s += 235/cycle.length;
        }
    }

    svg.push("</svg>");

    res.setHeader("content-type", "image/svg+xml");
    res.send(svg.join(""));
});

app.use(express.static(join(".", "public")));

app.listen(port, () => console.log(`Fractals server started on port ${port}`));