Skip to content

Commit

Permalink
feat(geom): add/update iterator impls, add IEmpty impls
Browse files Browse the repository at this point in the history
- update ComplexPolygon
- update Path/Path3
  • Loading branch information
postspectacular committed Jun 20, 2024
1 parent 3e65f82 commit 0a15df5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/geom/src/api/complex-polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class ComplexPolygon implements IHiccupShape2<ComplexPolygon> {
this.children = children ? ensureArray(children) : [];
}

*[Symbol.iterator]() {
yield this.boundary;
yield* this.children;
}

addChild(poly: Polygon) {
this.children.push(poly);
}
Expand Down
10 changes: 8 additions & 2 deletions packages/geom/src/api/path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Fn, IClear } from "@thi.ng/api";
import type { Fn, IClear, IEmpty } from "@thi.ng/api";
import { ensureArray } from "@thi.ng/arrays/ensure-array";
import { peek } from "@thi.ng/arrays/peek";
import { equiv } from "@thi.ng/equiv";
import { illegalState } from "@thi.ng/errors/illegal-state";
import { flatten1 } from "@thi.ng/transducers/flatten1";
import type { Attribs, IHiccupShape2, PathSegment2 } from "../api.js";
import { __copyAttribs, __copySegment } from "../internal/copy.js";

export class Path implements IClear, IHiccupShape2<Path> {
export class Path implements IClear, IEmpty<Path>, IHiccupShape2<Path> {
readonly type = "path";
readonly dim = 2;

Expand All @@ -32,12 +33,17 @@ export class Path implements IClear, IHiccupShape2<Path> {

*[Symbol.iterator]() {
yield* this.segments;
yield* flatten1(this.subPaths);
}

clear() {
this.segments.length = 0;
}

empty() {
return new Path();
}

close() {
if (!this.closed) this.segments.push({ type: "z" });
return this;
Expand Down
10 changes: 8 additions & 2 deletions packages/geom/src/api/path3.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Fn, IClear } from "@thi.ng/api";
import type { Fn, IClear, IEmpty } from "@thi.ng/api";
import { ensureArray } from "@thi.ng/arrays/ensure-array";
import { peek } from "@thi.ng/arrays/peek";
import { equiv } from "@thi.ng/equiv";
import { illegalState } from "@thi.ng/errors/illegal-state";
import { flatten1 } from "@thi.ng/transducers/flatten1";
import type { Attribs, IHiccupShape3, PathSegment3 } from "../api.js";
import { __copyAttribs, __copySegment } from "../internal/copy.js";

export class Path3 implements IClear, IHiccupShape3<Path3> {
export class Path3 implements IClear, IEmpty<Path3>, IHiccupShape3<Path3> {
readonly type = "path3";
readonly dim = 3;

Expand All @@ -32,12 +33,17 @@ export class Path3 implements IClear, IHiccupShape3<Path3> {

*[Symbol.iterator]() {
yield* this.segments;
yield* flatten1(this.subPaths);
}

clear() {
this.segments.length = 0;
}

empty() {
return new Path3();
}

close() {
if (!this.closed) this.segments.push({ type: "z" });
return this;
Expand Down

0 comments on commit 0a15df5

Please sign in to comment.