Skip to content

Commit

Permalink
feat(geom): add asPolyline() multi-fn
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 22, 2019
1 parent a017b10 commit c602379
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from "./ops/arc-length";
export * from "./ops/area";
export * from "./ops/as-cubic";
export * from "./ops/as-polygon";
export * from "./ops/as-polyline";
export * from "./ops/as-svg";
export * from "./ops/bounds";
export * from "./ops/center";
Expand Down
44 changes: 44 additions & 0 deletions packages/geom/src/ops/as-polyline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { defmulti, MultiFn1O } from "@thi.ng/defmulti";
import {
IShape,
Polyline,
SamplingOpts,
Type,
Path
} from "../api";
import { dispatch } from "../internal/dispatch";
import { vertices } from "./vertices";

export const asPolyline: MultiFn1O<IShape, number | Partial<SamplingOpts>, Polyline> = defmulti(dispatch);

asPolyline.addAll({

[Type.POINTS]:
($, opts) => new Polyline(vertices($, opts), { ...$.attribs }),

[Type.PATH]:
($: Path, opts) => {
const pts = vertices($, opts);
return new Polyline(
$.closed ?
pts.concat([pts[0]]) :
pts,
{ ...$.attribs }
);
},

[Type.POLYGON]:
($, opts) => {
const pts = vertices($, opts);
return new Polyline(pts.concat([pts[0]]), { ...$.attribs });
},

});

asPolyline.isa(Type.CIRCLE, Type.POLYGON);
asPolyline.isa(Type.ELLIPSE, Type.POLYGON);
asPolyline.isa(Type.LINE, Type.POINTS);
asPolyline.isa(Type.POLYLINE, Type.POINTS);
asPolyline.isa(Type.QUAD, Type.POLYGON);
asPolyline.isa(Type.RECT, Type.POLYGON);
asPolyline.isa(Type.TRIANGLE, Type.POLYGON);

0 comments on commit c602379

Please sign in to comment.