Skip to content

Commit

Permalink
perf(vectors): add direction2/3, update callsites
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 6, 2022
1 parent acd4a14 commit f3dcda8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/vectors/src/bisect.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HALF_PI, PI } from "@thi.ng/math/api";
import { addmN } from "./addmn.js";
import type { ReadonlyVec, Vec, VecOpRoVV } from "./api.js";
import { direction } from "./direction.js";
import { direction2 } from "./direction.js";
import { headingXY } from "./heading.js";
import { mixN2 } from "./mixn.js";
import { normalize } from "./normalize.js";
import { normalize, normalize2 } from "./normalize.js";
import { perpendicularCCW } from "./perpendicular.js";
import { sub } from "./sub.js";

Expand Down Expand Up @@ -72,9 +72,9 @@ export const cornerBisector2 = (
!out && (out = []),
perpendicularCCW(
out,
normalize(
normalize2(
out,
mixN2(out, direction(out, a, b), direction([], b, c), 0.5),
mixN2(out, direction2(out, a, b), direction2([], b, c), 0.5),
n
)
)
Expand Down
18 changes: 16 additions & 2 deletions packages/vectors/src/direction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReadonlyVec, Vec } from "./api.js";
import { normalize } from "./normalize.js";
import { sub } from "./sub.js";
import { normalize, normalize2, normalize3 } from "./normalize.js";
import { sub, sub2, sub3 } from "./sub.js";

/**
* Computes direction vector `a` -> `b`, normalized to length `n`
Expand All @@ -16,3 +16,17 @@ export const direction = (
b: ReadonlyVec,
n = 1
) => normalize(null, sub(out || a, b, a), n);

export const direction2 = (
out: Vec | null,
a: ReadonlyVec,
b: ReadonlyVec,
n = 1
) => normalize2(null, sub2(out || a, b, a), n);

export const direction3 = (
out: Vec | null,
a: ReadonlyVec,
b: ReadonlyVec,
n = 1
) => normalize3(null, sub3(out || a, b, a), n);
6 changes: 3 additions & 3 deletions packages/vectors/src/normal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReadonlyVec, Vec } from "./api.js";
import { direction } from "./direction.js";
import { direction2 } from "./direction.js";
import { perpendicularCCW, perpendicularCW } from "./perpendicular.js";

/**
Expand All @@ -13,7 +13,7 @@ import { perpendicularCCW, perpendicularCW } from "./perpendicular.js";
* @param n -
*/
export const normalCCW = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, n = 1) =>
perpendicularCCW(null, direction(out || [], a, b, n));
perpendicularCCW(null, direction2(out || [], a, b, n));

/**
* Computes 2D normal by rotating direction vector `a` -> `b`, 90 deg
Expand All @@ -26,4 +26,4 @@ export const normalCCW = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, n = 1) =>
* @param n -
*/
export const normalCW = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, n = 1) =>
perpendicularCW(null, direction(out || [], a, b, n));
perpendicularCW(null, direction2(out || [], a, b, n));
4 changes: 2 additions & 2 deletions packages/vectors/src/ortho-normal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReadonlyVec, Vec } from "./api.js";
import { cross3 } from "./cross.js";
import { normalize } from "./normalize.js";
import { normalize3 } from "./normalize.js";
import { sub3 } from "./sub.js";

/**
Expand Down Expand Up @@ -28,5 +28,5 @@ export const orthoNormal3 = (
doNormalize = true
) => {
out = cross3(null, sub3(out || a, b, a), sub3([], c, a));
return doNormalize ? normalize(out, out) : out;
return doNormalize ? normalize3(out, out) : out;
};

0 comments on commit f3dcda8

Please sign in to comment.