Skip to content

Commit

Permalink
feat(vectors): migrate direction(), normalLeft/Right2() from geom pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 21, 2019
1 parent 862ed08 commit 07d5f8f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/vectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,10 @@ Functions for memory mapped, strided vectors (without requiring wrappers):
- `angleBetween2` / `angleBetween3`
- `angleRatio`
- `bisect2`
- `direction`
- `faceForward`
- `heading` / `headingXY` / `headingXZ` / `headingYZ`
- `normalLeft2` / `normalRight2`
- `perpendicularLeft2` / `perpendicularRight2`
- `project`
- `reflect`
Expand Down
15 changes: 15 additions & 0 deletions packages/vectors/src/direction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ReadonlyVec, Vec } from "./api";
import { normalize } from "./normalize";
import { sub } from "./sub";

/**
* Computes direction vector `a` -> `b`, normalized to length `n`
* (default 1).
*
* @param a
* @param b
* @param n
*/
export const direction =
(out: Vec, a: ReadonlyVec, b: ReadonlyVec, n = 1) =>
normalize(null, sub(out || a, b, a), n);
2 changes: 2 additions & 0 deletions packages/vectors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from "./copy";
export * from "./cos";
export * from "./cosh";
export * from "./cross";
export * from "./direction";
export * from "./dist";
export * from "./dist-chebyshev";
export * from "./dist-manhattan";
Expand Down Expand Up @@ -74,6 +75,7 @@ export * from "./mul";
export * from "./muln";
export * from "./muls";
export * from "./neg";
export * from "./normal";
export * from "./normalize";
export * from "./ortho-normal";
export * from "./perpendicular";
Expand Down
31 changes: 31 additions & 0 deletions packages/vectors/src/normal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ReadonlyVec, Vec } from "./api";
import { direction } from "./direction";
import { perpendicularLeft2, perpendicularRight2 } from "./perpendicular";

/**
* Computes 2D normal by rotating direction vector `a` -> `b`, 90 deg
* counterclockwise, normalized to length `n` (default: 1). If `out` is
* null, creates new vector.
*
* @param out
* @param a
* @param b
* @param n
*/
export const normalLeft2 =
(out: Vec, a: ReadonlyVec, b: ReadonlyVec, n = 1) =>
perpendicularLeft2(null, direction(out || [], a, b, n));

/**
* Computes 2D normal by rotating direction vector `a` -> `b`, 90 deg
* clockwise, normalized to length `n` (default: 1). If `out` is null,
* creates new vector.
*
* @param out
* @param a
* @param b
* @param n
*/
export const normalRight2 =
(out: Vec, a: ReadonlyVec, b: ReadonlyVec, n = 1) =>
perpendicularRight2(null, direction(out || [], a, b, n));

0 comments on commit 07d5f8f

Please sign in to comment.