-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geom-poly-utils): move barycentric fns from main geom pkg
- Loading branch information
1 parent
74aa954
commit 68a26f4
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters