Skip to content

Commit

Permalink
refactor(geom): add pclike(), simplify shape factory fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 21, 2019
1 parent b3dc83f commit ef0d102
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/geom/src/ctors/cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { cubicFromArc as _arc, cubicFromLine as _line, cubicFromQuadratic as _qu
import { Vec } from "@thi.ng/vectors";
import { Arc } from "../api/arc";
import { Cubic } from "../api/cubic";
import { argAttribs } from "../internal/args";
import { copyAttribs } from "../internal/copy-attribs";
import { pclike } from "../internal/pclike";

export function cubic(a: Vec, b: Vec, c: Vec, d: Vec, attribs?: Attribs): Cubic;
export function cubic(pts: Vec[], attribs?: Attribs): Cubic;
export function cubic(...args: any[]) {
const attr = argAttribs(args);
return new Cubic(args.length === 1 ? args[0] : args, attr);
return pclike(Cubic, args);
}

export const cubicFromArc = (arc: Arc) =>
_arc(arc.pos, arc.r, arc.axis, arc.start, arc.end).map(
(c) => new Cubic(c, { ...arc.attribs })
(c) => new Cubic(c, copyAttribs(arc))
);

export const cubicFromLine = (a: Vec, b: Vec, attribs?: Attribs) =>
Expand Down
5 changes: 2 additions & 3 deletions packages/geom/src/ctors/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { liangBarsky2 } from "@thi.ng/geom-clip";
import { Vec, VecPair } from "@thi.ng/vectors";
import { Line } from "../api/line";
import { Rect } from "../api/rect";
import { argAttribs } from "../internal/args";
import { pclike } from "../internal/pclike";

export function line(a: Vec, b: Vec, attribs?: Attribs): Line;
export function line(pts: Vec[], attribs?: Attribs): Line;
export function line(...args: any[]) {
const attr = argAttribs(args);
return new Line(args.length === 1 ? args[0] : args, attr);
return pclike(Line, args);
}

export const clippedLine = (l: Line, bounds: VecPair | Rect) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/geom/src/ctors/quad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { Plane } from "../api/plane";
import { Quad } from "../api/quad";
import { Quad3 } from "../api/quad3";
import { argAttribs } from "../internal/args";
import { pclike } from "../internal/pclike";

export function quad(a: Vec, b: Vec, c: Vec, d: Vec, attribs?: Attribs): Quad;
export function quad(pts: Vec[], attribs?: Attribs): Quad;
export function quad(...args: any[]) {
const attr = argAttribs(args);
return new Quad(args.length === 1 ? args[0] : args, attr);
return pclike(Quad, args);
}

export function quad3(a: Vec, b: Vec, c: Vec, d: Vec, attribs?: Attribs): Quad;
Expand Down
5 changes: 2 additions & 3 deletions packages/geom/src/ctors/quadratic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Attribs } from "@thi.ng/geom-api";
import { quadraticFromLine as _line } from "@thi.ng/geom-splines";
import { Vec } from "@thi.ng/vectors";
import { Quadratic } from "../api/quadratic";
import { argAttribs } from "../internal/args";
import { pclike } from "../internal/pclike";

export function quadratic(a: Vec, b: Vec, c: Vec, attribs?: Attribs): Quadratic;
export function quadratic(pts: Vec[], attribs?: Attribs): Quadratic;
export function quadratic(...args: any[]) {
const attr = argAttribs(args);
return new Quadratic(args.length === 1 ? args[0] : args, attr);
return pclike(Quadratic, args);
}

export const quadraticFromLine = (a: Vec, b: Vec, attribs?: Attribs) =>
Expand Down
5 changes: 2 additions & 3 deletions packages/geom/src/ctors/triangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Attribs } from "@thi.ng/geom-api";
import { equilateralTriangle2 } from "@thi.ng/geom-poly-utils";
import { Vec } from "@thi.ng/vectors";
import { Triangle } from "../api/triangle";
import { argAttribs } from "../internal/args";
import { pclike } from "../internal/pclike";

export function triangle(a: Vec, b: Vec, c: Vec, attribs?: Attribs): Triangle;
export function triangle(pts: Vec[], attribs?: Attribs): Triangle;
export function triangle(...args: any[]) {
const attr = argAttribs(args);
return new Triangle(args.length === 1 ? args[0] : args, attr);
return pclike(Triangle, args);
}

export const equilateralTriangle = (a: Vec, b: Vec, attribs?: Attribs) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export * from "./ops/warp-points";
export * from "./ops/with-attribs";

export * from "./internal/coll-bounds";
export * from "./internal/copy-attribs";
export * from "./internal/edges";
export * from "./internal/pclike";
export * from "./internal/points-as-shape";
export * from "./internal/split";
export * from "./internal/transform-points";
export * from "./internal/translate-points";
Expand Down
7 changes: 7 additions & 0 deletions packages/geom/src/internal/pclike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PCLikeConstructor } from "@thi.ng/geom-api";
import { argAttribs } from "./args";

export const pclike = (ctor: PCLikeConstructor, args: any[]) => {
const attr = argAttribs(args);
return new ctor(args.length === 1 ? args[0] : args, attr);
};

0 comments on commit ef0d102

Please sign in to comment.