Skip to content

Commit

Permalink
feat(geom): add offset() & initial impls
Browse files Browse the repository at this point in the history
- add impls for circle, line, rect
  • Loading branch information
postspectacular committed Jun 19, 2020
1 parent 7837961 commit 819afd1
Show file tree
Hide file tree
Showing 2 changed files with 48 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 @@ -58,6 +58,7 @@ export * from "./ops/fit-into-bounds";
export * from "./ops/flip";
export * from "./ops/intersects";
export * from "./ops/map-point";
export * from "./ops/offset";
export * from "./ops/point-at";
export * from "./ops/point-inside";
export * from "./ops/resample";
Expand Down
47 changes: 47 additions & 0 deletions packages/geom/src/ops/offset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation2 } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import {
addN2,
max2,
set2,
ZERO2,
normalCCW,
add2,
sub2,
} from "@thi.ng/vectors";
import { Circle } from "../api/circle";
import { Rect } from "../api/rect";
import { rectFromCentroid } from "../ctors/rect";
import { copyAttribs } from "../internal/copy-attribs";
import { dispatch } from "../internal/dispatch";
import { centroid } from "./centroid";
import { Line } from "../api/line";
import { Quad } from "../api/quad";

export const offset = defmulti<IShape, number, IShape>(dispatch);

offset.addAll(<IObjectOf<Implementation2<unknown, number, IShape>>>{
[Type.CIRCLE]: ($: Circle, n) =>
new Circle(set2([], $.pos), Math.max($.r + n, 0)),

[Type.LINE]: ({ points: [a, b], attribs }: Line, n) => {
const norm = normalCCW([], a, b, n);
return new Quad(
[
add2([], a, norm),
add2([], b, norm),
sub2([], b, norm),
sub2([], a, norm),
],
{ ...attribs }
);
},

[Type.RECT]: ($: Rect, n) =>
rectFromCentroid(
centroid($)!,
max2(null, addN2([], $.size, n), ZERO2),
copyAttribs($)
),
});

0 comments on commit 819afd1

Please sign in to comment.