Skip to content

Commit

Permalink
feat(geom): add polygon impl for asCubic(), add pathFromCubics()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 11, 2019
1 parent 81ac728 commit 2faec7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packages/geom/src/ctors/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ import { arcFrom2Points } from "./arc";
export const path = (segments: PathSegment[], attribs?: Attribs) =>
new Path(segments, attribs);

export const pathFromCubics = (cubics: Cubic[]) => {
const path = new Path([], cubics[0].attribs);
path.segments.push({ type: SegmentType.MOVE, point: cubics[0].points[0] });
for (let c of cubics) {
path.segments.push({ type: SegmentType.CUBIC, geo: c });
}
return path;
};

export const normalizedPath = (path: Path) =>
new Path(
[
Expand Down
20 changes: 16 additions & 4 deletions packages/geom/src/ops/as-cubic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1 } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { defmulti, Implementation1, MultiFn1O } from "@thi.ng/defmulti";
import { CubicOpts, IShape, Type } from "@thi.ng/geom-api";
import { closedCubicFromBreakPoints, closedCubicFromControlPoints } from "@thi.ng/geom-splines";
import {
EPS,
HALF_PI,
Expand All @@ -14,12 +15,15 @@ import {
Cubic,
Line,
Path,
Polygon,
Quadratic
} from "../api";
import { cubicFromLine, cubicFromQuadratic } from "../ctors/cubic";
import { cubic, cubicFromLine, cubicFromQuadratic } from "../ctors/cubic";
import { dispatch } from "../internal/dispatch";

export const asCubic = defmulti<IShape, Cubic[]>(dispatch);
export const asCubic: MultiFn1O<IShape, Partial<CubicOpts>, Cubic[]> = defmulti(
dispatch
);

asCubic.addAll(<IObjectOf<Implementation1<unknown, Cubic[]>>>{
[Type.ARC]: ($: Arc) => {
Expand Down Expand Up @@ -79,6 +83,14 @@ asCubic.addAll(<IObjectOf<Implementation1<unknown, Cubic[]>>>{
...mapcat((s) => (s.geo ? asCubic(s.geo) : null), $.segments)
],

[Type.POLYGON]: ($: Polygon, opts: Partial<CubicOpts> = {}) => {
opts = { breakPoints: false, scale: 1 / 3, uniform: false, ...opts };
return (opts.breakPoints
? closedCubicFromBreakPoints($.points, opts.scale, opts.uniform)
: closedCubicFromControlPoints($.points, opts.scale, opts.uniform)
).map((pts) => cubic(pts, { ...$.attribs }));
},

[Type.QUADRATIC]: ({ attribs, points }: Quadratic) => [
cubicFromQuadratic(points[0], points[1], points[2], { ...attribs })
]
Expand Down

0 comments on commit 2faec7f

Please sign in to comment.