Skip to content

Commit

Permalink
feat(geom-poly-utils): move barycentric fns from main geom pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 25, 2019
1 parent 74aa954 commit 68a26f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/geom-poly-utils/src/barycentric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
addW3,
dot,
magSq,
ReadonlyVec,
setC3,
sub,
Vec
} from "@thi.ng/vectors";

export const toBarycentric =
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec, p: ReadonlyVec, out: Vec = []) => {
const u = sub([], b, a);
const v = sub([], c, a);
const w = sub([], p, a);
const uu = magSq(u);
const vv = magSq(v);
const uv = dot(u, v);
const uw = dot(u, w);
const vw = dot(v, w);
const d = 1 / (uv * uv - uu * vv);
const s = d * (uv * vw - vv * uw);
const t = d * (uv * uw - uu * vw);
return setC3(out, 1 - (s + t), s, t);
};

export const fromBarycentric =
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec, p: ReadonlyVec, out?: Vec) =>
addW3(out, a, b, c, p[0], p[1], p[2]);
1 change: 1 addition & 0 deletions packages/geom-poly-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./area";
export * from "./barycentric";
export * from "./bounds";
export * from "./center-of-weight";
export * from "./centroid";
Expand Down

0 comments on commit 68a26f4

Please sign in to comment.