Skip to content

Commit

Permalink
feat(geom): update vertices() impl for Path, incl. sub-path vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 3, 2024
1 parent ce3a922 commit 824067f
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/geom/src/vertices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isArray } from "@thi.ng/checks/is-array";
import { isNumber } from "@thi.ng/checks/is-number";
import type { MultiFn1O } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type { IShape } from "@thi.ng/geom-api";
import type { IShape, PathSegment } from "@thi.ng/geom-api";
import { DEFAULT_SAMPLES, type SamplingOpts } from "@thi.ng/geom-api/sample";
import { sample as _arcVertices } from "@thi.ng/geom-arc/sample";
import { resample } from "@thi.ng/geom-resample/resample";
Expand Down Expand Up @@ -31,6 +31,7 @@ import type { Quadratic } from "./api/quadratic.js";
import type { Rect } from "./api/rect.js";
import { __dispatch } from "./internal/dispatch.js";
import { __circleOpts, __sampleAttribs } from "./internal/vertices.js";
import { peek } from "@thi.ng/arrays/peek";

/**
* Extracts/samples vertices from given shape's boundary and returns them as
Expand Down Expand Up @@ -179,22 +180,22 @@ export const vertices: MultiFn1O<
path: ($: Path, opts?) => {
opts = __sampleAttribs(opts, $.attribs);
const _opts = isNumber(opts) ? { num: opts } : opts;
let verts: Vec[] = [];
for (
let segs = $.segments, n = segs.length - 1, i = 0;
i <= n;
i++
) {
const s = segs[i];
if (s.geo) {
verts = verts.concat(
vertices(s.geo, {
const verts: Vec[] = [];
const $segmentVerts = (segments: PathSegment[]) => {
const closed = peek(segments)?.type === "z";
for (let n = segments.length - 1, i = 0; i <= n; i++) {
const s = segments[i];
if (!s.geo) continue;
verts.push(
...vertices(s.geo, {
..._opts,
last: i === n && !$.closed,
last: !closed && i === n,
})
);
}
}
};
$segmentVerts($.segments);
for (let sub of $.subPaths) $segmentVerts(sub);
return verts;
},

Expand Down

0 comments on commit 824067f

Please sign in to comment.