Skip to content

Commit

Permalink
refactor(examples): update geom examples (recent API changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 4, 2024
1 parent bdba298 commit f0f5ea7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/geom-tessel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const tessellation = (t: number, tessel: Tessellator[], tint: Tint) => {
asPolygon(
circle([0, 0], W2),
Math.floor(fit11(Math.sin(t), MIN_RES, MAX_RES))
),
)[0],
tessel
)
);
Expand Down
2 changes: 1 addition & 1 deletion examples/poly-subdiv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function* update() {
// https://mastodon.thi.ng/@toxi/110972322869333970
const shapes = threadLast(
// original shape: circle w/ radius=300, converted to polygon with N vertices
[asPolygon(circle(300), 40)],
[asPolygon(circle(300), 40)[0]],
// lazy, iterative & infinite(!) subdivision: `iterate()` produces the
// inductive sequence: f(x+1) = f(f(x)), i.e. the result of the current
// iteration becomes the input for the next iteration...
Expand Down
4 changes: 2 additions & 2 deletions examples/scenegraph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const hex = new GeomNode(
[300, 300],
0,
200,
asPolygon(circle(0.5, { fill: "#0ff" }), 6)
asPolygon(circle(0.5, { fill: "#0ff" }), 6)[0]
);

// rotated child node
Expand All @@ -74,7 +74,7 @@ const triangle = new GeomNode(
[0, 0],
PI / 4,
1,
asPolygon(circle(0.5, { fill: "#f0f" }), 3)
asPolygon(circle(0.5, { fill: "#f0f" }), 3)[0]
);

// secondary children
Expand Down
15 changes: 5 additions & 10 deletions examples/svg-resample/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import { fit11 } from "@thi.ng/math";
import { $compile, $replace } from "@thi.ng/rdom";
import { fromRAF } from "@thi.ng/rstream";
import { parse, Type } from "@thi.ng/sax";
import {
comp,
filter,
map,
mapcat,
push,
transduce,
} from "@thi.ng/transducers";
import { comp, filter, map, push, transduce } from "@thi.ng/transducers";
import SVG from "./example.svg";

(async () => {
Expand All @@ -32,7 +25,7 @@ import SVG from "./example.svg";
filter((ev) => ev.type === Type.ELEM_END && ev.tag === "path"),
// pathFromSvg() returns an array of subpaths
// using mapcat() flattens that array
mapcat((ev) => pathFromSvg(ev.attribs!.d))
map((ev) => pathFromSvg(ev.attribs!.d))
),
push<Path>(),
await raw.text()
Expand All @@ -46,7 +39,9 @@ import SVG from "./example.svg";
// - first into a polygon
// - then resample poly using uniform distance and extract vertices
// - wrap in `points` container (aka point cloud)
return paths.map((path) => points(vertices(asPolygon(path), { dist })));
return paths.map((path) =>
points(vertices(asPolygon(path)[0], { dist }))
);
};

// now create reactive requestAnimationFrame() stream which:
Expand Down
18 changes: 16 additions & 2 deletions packages/geom/test/complex-poly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Polygon,
arcLength,
area,
asPath,
asPolygon,
asPolyline,
asSvg,
Expand All @@ -21,11 +22,24 @@ import {
vertices,
} from "../src/index.js";

const A = complexPolygon(asPolygon(rectWithCentroid([0, 0], 100)), [
<Polygon>flip(asPolygon(rectWithCentroid([0, 0], 50))),
const A = complexPolygon(asPolygon(rectWithCentroid([0, 0], 100))[0], [
<Polygon>flip(asPolygon(rectWithCentroid([0, 0], 50))[0]),
]);

describe("complex poly", () => {
test("asPath", () => {
expect(asSvg(asPath(A, { linear: true }))).toBe(
'<path d="M-50,-50H50V50H-50zM-25,25H25V-25H-25z"/>'
);
expect(asSvg(asPath(A, {}))).toBe(
'<path d="M0,-50C16.667,-50,50,-16.667,50,0C50,16.667,16.667,50,0,50C-16.667,50,-50,16.667,-50,0C-50,-16.667,-16.667,-50,0,-50M0,25C8.333,25,25,8.333,25,0C25,-8.333,8.333,-25,0,-25C-8.333,-25,-25,-8.333,-25,0C-25,8.333,-8.333,25,0,25"/>'
);
});

test("asPolygon", () => {
expect(asPolygon(A)).toEqual([A.boundary, ...A.children]);
});

test("asPolyline", () => {
expect(asPolyline(A)).toEqual([
asPolyline(A.boundary)[0],
Expand Down
29 changes: 28 additions & 1 deletion packages/geom/test/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { eqDeltaArray } from "@thi.ng/vectors";
import { describe, expect, test } from "bun:test";
import {
Path,
asPolyline,
asSvg,
line,
pathFromSvg,
polyline,
simplify,
Expand All @@ -11,7 +13,32 @@ import {

const A = pathFromSvg("M0,0h100v100h-100zM10,10v80h80v-80z");

describe.only("path", () => {
describe("path", () => {
test("fromSvg", () => {
expect(A).toEqual(
new Path(
[
{ type: "m", point: [0, 0] },
{ type: "l", geo: line([0, 0], [100, 0]) },
{ type: "l", geo: line([100, 0], [100, 100]) },
{ type: "l", geo: line([100, 100], [0, 100]) },
{ type: "l", geo: line([0, 100], [0, 0]) },
{ type: "z" },
],
[
[
{ type: "m", point: [10, 10] },
{ type: "l", geo: line([10, 10], [10, 90]) },
{ type: "l", geo: line([10, 90], [90, 90]) },
{ type: "l", geo: line([90, 90], [90, 10]) },
{ type: "l", geo: line([90, 10], [10, 10]) },
{ type: "z" },
],
]
)
);
});

test("asSvg", () => {
expect(asSvg(A)).toBe(
'<path d="M0,0H100V100H0V0zM10,10V90H90V10H10z"/>'
Expand Down

0 comments on commit f0f5ea7

Please sign in to comment.