-
Notifications
You must be signed in to change notification settings - Fork 684
Expand file tree
/
Copy pathlerp.ts
More file actions
32 lines (31 loc) · 1013 Bytes
/
Copy pathlerp.ts
File metadata and controls
32 lines (31 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.
/**
* Performs linear interpolation between two values.
*
* `t` outside the range `[0, 1]` extrapolates beyond the endpoints. A `t` of
* `0` returns `a` exactly and a `t` of `1` returns `b` exactly, even when
* floating point rounding would otherwise cause `a + (b - a)` to differ from
* `b`.
*
* @param a The start value
* @param b The end value
* @param t The interpolation parameter (`0` returns `a`, `1` returns `b`)
* @returns The interpolated value
*
* @example Usage
* ```ts
* import { lerp } from "@std/math/lerp";
* import { assertEquals } from "@std/assert";
*
* assertEquals(lerp(0, 10, 0.5), 5);
* assertEquals(lerp(0, 10, 0), 0);
* assertEquals(lerp(0, 10, 1), 10);
* ```
*/
// deno-lint-ignore deno-style-guide/exported-function-args-maximum
export function lerp(a: number, b: number, t: number): number {
if (t === 0) return a;
if (t === 1) return b;
return a + (b - a) * t;
}