Skip to content

Commit

Permalink
refactor(examples): update tessellation examples to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 7, 2024
1 parent b326b73 commit 2d85868
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
51 changes: 29 additions & 22 deletions examples/geom-hexgrid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
TESSELLATE_QUAD_FAN,
TESSELLATE_RIM_TRIS,
TESSELLATE_TRI_FAN,
TESSELLATE_TRI_FAN_SPLIT,
asPolygon,
asSvg,
centroid,
circle,
group,
polygon,
groupFromTessellation,
svgDoc,
tessellate,
withAttribs,
} from "@thi.ng/geom";
import {
comp,
Expand All @@ -21,7 +22,7 @@ import {
range2d,
transduce,
} from "@thi.ng/transducers";
import { dist, mag } from "@thi.ng/vectors";
import { dist2 } from "@thi.ng/vectors";

const SIN60 = Math.sin(Math.PI / 3);

Expand Down Expand Up @@ -59,33 +60,39 @@ const polys = transduce(
// actual tessellation: choose one or more tessellation methods
// (applied iteratively), also experiment with duplication and/or
// different orders...
// `tessellate()` will return an iterator of `Vec[]`, where each
// array is a set of new vertices which can then be used to form new
// shapes/polygons (see next step)
tessellate(cell, [
TESSELLATE_EDGE_SPLIT,
TESSELLATE_QUAD_FAN,
// TESSELLATE_TRI_FAN,
// TESSELLATE_RIM_TRIS,
])
// `tessellate()` returns a `Tessellation`, which we convert into
// a group of polygons. Since groups are iterable and we're using
// `mapcat()` here, the output of this step will be just a sequence
// of these result polygons...
groupFromTessellation(
tessellate(cell, [
TESSELLATE_EDGE_SPLIT,
TESSELLATE_QUAD_FAN,
// TESSELLATE_TRI_FAN,
// TESSELLATE_TRI_FAN_SPLIT,
// TESSELLATE_RIM_TRIS,
])
)

// (using the cell position or attribs each cell can also use a
// different configuration, resulting in a more complex/varied
// tessellation... comment out the above tessellate() call and
// uncomment/enable the one below instead...)

// tessellate(
// cell,
// // make tessellation config dependent on distance from center
// dist(CENTER, cell.attribs!.gridPos) < 4
// ? [TESSELLATE_EDGE_SPLIT, TESSELLATE_QUAD_FAN]
// : [TESSELLATE_TRI_FAN, TESSELLATE_TRI_FAN]
// groupFromTessellation(
// tessellate(
// cell,
// // make tessellation config dependent on distance from center
// dist2(CENTER, cell.attribs!.gridPos) < 4
// ? [TESSELLATE_EDGE_SPLIT, TESSELLATE_QUAD_FAN]
// : [TESSELLATE_TRI_FAN, TESSELLATE_TRI_FAN]
// )
// )
),
// create new polygon for each set of tessellated vertices
mapIndexed((i, points) =>
polygon(
points,
// assign color attribs to each polygon
mapIndexed((i, poly) =>
withAttribs(
<Polygon>poly,
// optional attribs
{ fill: i & 1 ? "black" : "yellow" }
)
Expand Down
15 changes: 9 additions & 6 deletions examples/geom-tessel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
centroid,
circle,
group,
groupFromTessellation,
Polygon,
polygon,
tessellate,
Expand Down Expand Up @@ -76,12 +77,14 @@ const tintedPoly = (tint: Tint, points: Vec[]) => {
const tessellation = (t: number, tessel: Tessellator[], tint: Tint) => {
return map(
partial(tintedPoly, tint),
tessellate(
asPolygon(
circle([0, 0], W2),
Math.floor(fit11(Math.sin(t), MIN_RES, MAX_RES))
)[0],
tessel
groupFromTessellation(
tessellate(
asPolygon(
circle([0, 0], W2),
Math.floor(fit11(Math.sin(t), MIN_RES, MAX_RES))
)[0],
tessel
)
)
);
};
Expand Down
14 changes: 6 additions & 8 deletions examples/pointfree-geom/src/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
vertices as _vertices,
centroid,
group,
groupFromTessellation,
polygon,
tessellate,
text,
transformVertices,
} from "@thi.ng/geom";
import type { IHiccupShape2, Tessellator } from "@thi.ng/geom-api";
import { quadFan, tesselInset, triFan } from "@thi.ng/geom-tessellate";
import { inset, quadFan, triFan } from "@thi.ng/geom-tessellate";
import {
rotation23,
scale23,
Expand Down Expand Up @@ -249,13 +250,10 @@ const defTessellator =
const ds = stack(ctx, isHOF ? 2 : 1);
const arg = isHOF ? ds.pop() : undefined;
const shape = ds.pop();
const tessel = <Tessellator>(isHOF ? fn(arg) : fn);
ds.push(
group(
{},
map((pts) => polygon(pts), tessellate(shape, [tessel]))
)
const tessel = <Tessellator>(
(isHOF ? (<Fn<number, Tessellator>>fn)(arg) : fn)
);
ds.push(groupFromTessellation(tessellate(shape, [tessel])));
return ctx;
};

Expand Down Expand Up @@ -295,6 +293,6 @@ const POINTFREE_GEOM = ffi(
// tessellators
trifan: defTessellator(triFan),
quadfan: defTessellator(quadFan),
inset: defTessellator(tesselInset, true),
inset: defTessellator(inset, true),
}
);

0 comments on commit 2d85868

Please sign in to comment.