@thi.ng/geom-isoline

@thi.ng/geom-isoline

npm versionnpm downloads Mastodon Follow

[!NOTE] This is one of 190 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

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/geom-isoline"></script>

Skypack documentation

For Node.js REPL:

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

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

Dependencies

Usage examples

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

ScreenshotDescriptionLive demoSource
Animated sine plasma effect visualized using contour linesDemoSource

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 from "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

Generated using TypeDoc