Skip to content

Commit

Permalink
feat(geom): update API for various shape types
Browse files Browse the repository at this point in the history
- add IClear impls for APC, Group, Path
- add .add() methods for APC, Group, Polygon, Polyline
- update Path.add() to accept multiple args
  • Loading branch information
postspectacular committed Aug 5, 2023
1 parent fdea6ba commit 3a45c5f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
7 changes: 6 additions & 1 deletion packages/geom/src/api/apc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { IClear } from "@thi.ng/api";
import type { Attribs, PCLike } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";

export abstract class APC implements PCLike {
export abstract class APC implements IClear, PCLike {
constructor(public points: Vec[] = [], public attribs?: Attribs) {}

abstract get type(): number | string;
Expand All @@ -13,4 +14,8 @@ export abstract class APC implements PCLike {
*[Symbol.iterator]() {
yield* this.points;
}

clear() {
this.points.length = 0;
}
}
25 changes: 23 additions & 2 deletions packages/geom/src/api/group.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { Fn } from "@thi.ng/api";
import type { Fn, IClear } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import { __copyAttribs } from "../internal/copy.js";

export class Group implements IHiccupShape {
/**
* Geometry/shape group container for other {@link IHiccupShape}s, incl. nested
* groups.
*/
export class Group implements IClear, IHiccupShape {
constructor(
public attribs?: Attribs,
public children: IHiccupShape[] = []
Expand All @@ -17,6 +21,23 @@ export class Group implements IHiccupShape {
yield* this.children;
}

/**
* Appends given `shapes` to this {@link Group.children} array.
*
* @param shapes
*/
add(...shapes: IHiccupShape[]) {
this.children.push(...shapes);
return this;
}

/**
* Removes all children from the group.
*/
clear() {
this.children.length = 0;
}

copy(): Group {
return this.copyTransformed((c) => <IHiccupShape>c.copy());
}
Expand Down
11 changes: 8 additions & 3 deletions packages/geom/src/api/path.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { IClear } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
import { illegalState } from "@thi.ng/errors/illegal-state";
import type { Attribs, IHiccupShape, PathSegment } from "@thi.ng/geom-api";
import { copy } from "@thi.ng/vectors/copy";
import { __copyAttribs } from "../internal/copy.js";

export class Path implements IHiccupShape {
export class Path implements IClear, IHiccupShape {
closed = false;

constructor(
Expand All @@ -20,6 +21,10 @@ export class Path implements IHiccupShape {
yield* this.segments;
}

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

copy(): Path {
const p = new Path(
this.segments.map((s) => {
Expand All @@ -44,9 +49,9 @@ export class Path implements IHiccupShape {
return o instanceof Path && equiv(this.segments, o.segments);
}

add(s: PathSegment) {
add(...segments: PathSegment[]) {
if (this.closed) illegalState("path already closed");
this.segments.push(s);
this.segments.push(...segments);
}

toHiccup() {
Expand Down
5 changes: 5 additions & 0 deletions packages/geom/src/api/polygon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -7,6 +8,10 @@ export class Polygon extends APC implements IHiccupShape {
return "poly";
}

add(...points: Vec[]) {
this.points.push(...points);
}

copy(): Polygon {
return <Polygon>__copyShape(Polygon, this);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/geom/src/api/polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
IHiccupPathSegment,
IHiccupShape,
} from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +12,10 @@ export class Polyline extends APC implements IHiccupShape, IHiccupPathSegment {
return "polyline";
}

add(...points: Vec[]) {
this.points.push(...points);
}

copy(): Polyline {
return <Polyline>__copyShape(Polyline, this);
}
Expand Down

0 comments on commit 3a45c5f

Please sign in to comment.