Skip to content

Commit

Permalink
fix(vectors): arg types limit(), add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 1, 2021
1 parent 56dba9a commit d9e8404
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/vectors/src/limit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { ReadonlyVec, Vec } from "./api.js";
import type { VecOpVN } from "./api.js";
import { mag } from "./mag.js";
import { mulN } from "./muln.js";
import { set } from "./set.js";

export const limit = (out: Vec, v: ReadonlyVec, n: number) => {
/**
* Limits `v` to max length `n` and writes result to `out` (or to itself if
* `out` is null).
*
* @param out
* @param v
* @param n
*/
export const limit: VecOpVN = (out, v, n) => {
!out && (out = v);
const m = mag(v);
return m > n ? mulN(out, v, n / m) : out !== v ? set(out, v) : out;
Expand Down

0 comments on commit d9e8404

Please sign in to comment.