Skip to content

Latest commit

 

History

History
158 lines (119 loc) · 4.9 KB

README.md

File metadata and controls

158 lines (119 loc) · 4.9 KB

@thi.ng/geom-isoline

npm version npm downloads Mastodon Follow

Note

This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

2D isoline / contour extraction, using Marching squares. Ported from the Clojure version of thi.ng/ndarray.

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/geom-isoline

ESM import:

import * as iso from "@thi.ng/geom-isoline";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/geom-isoline"></script>

JSDelivr documentation

For Node.js REPL:

const iso = await import("@thi.ng/geom-isoline");

Package sizes (brotli'd, pre-treeshake): ESM: 716 bytes

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

Usage examples

One project in this repo's /examples directory is using this package:

Screenshot Description Live demo Source
Animated sine plasma effect visualized using contour lines Demo Source

API

Generated API docs

import * as g from "@thi.ng/geom";
import * as iso from "@thi.ng/geom-isoline";
import * as tx from "@thi.ng/transducers";

import * as fs "node:fs";

// evaluate fn for each [x,y], create array
const makeField = (fn, width, height) =>
    iso.setBorder(
        [...tx.map(fn, tx.range2d(width, height))],
        width,
        height,
        1000
    );

// precompute field with given fn
const src = makeField(
    ([x, y]) => Math.sin(x * 0.1) * Math.cos(y * 0.1),
    100,
    100
);

// contour iterator
const contours = tx.iterator(
    tx.comp(
        // iso value => RGB color from
        tx.mapIndexed((i, x) => [x, [i / 20, 1 - i / 40, 1 - i / 20]]),
        // contour & color tuples
        tx.mapcat(([i, col]) => tx.map((pts)=> [pts, col], iso.isolines(src, 100, 100, i))),
        // wrap as polygon for svg
        tx.map(([pts, col]) => g.polygon(pts, { stroke: col}))
    ),
    // iso value range
    tx.range(-1, 1, 0.05)
);

// svg document wrapper
const doc = g.svgDoc(
    {
        width: 600,
        height: 600,
        fill: "none",
        "stroke-width": 0.1
    },
    ...contours
);

// output
fs.writeFileSync("contours.svg", g.asSvg(doc));

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-geom-isoline,
  title = "@thi.ng/geom-isoline",
  author = "Karsten Schmidt",
  note = "https://thi.ng/geom-isoline",
  year = 2015
}

License

© 2015 - 2024 Karsten Schmidt // Apache License 2.0