-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3401240
commit 072778f
Showing
5 changed files
with
65 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,8 @@ | ||
import * as _ from 'radashi' | ||
import { bench } from 'vitest' | ||
|
||
describe('lerp', () => { | ||
bench('with valid input', () => { | ||
_.lerp(0, 10, 0.5) | ||
}) | ||
}) |
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,22 @@ | ||
--- | ||
title: lerp | ||
description: Smoothly transitions between two values based on a factor | ||
--- | ||
|
||
## Basic usage | ||
|
||
The `lerp` function is used to linearly interpolate between two numbers based on a specified amount. This function is particularly useful in animations, graphics, and games for smooth transitions. | ||
|
||
```ts | ||
import * as _ from 'radashi' | ||
|
||
_.lerp(0, 10, 0.5) // => 5 | ||
_.lerp(5, 15, 0.2) // => 7 | ||
_.lerp(-10, 10, 0.75) // => 5 | ||
``` | ||
|
||
## Etymology | ||
|
||
The name `lerp` is short for "linear interpolation". It's a term from computer graphics that means "interpolate linearly between two values". | ||
|
||
For more information, check out the [Wikipedia article](https://en.wikipedia.org/wiki/Linear_interpolation) on linear interpolation. |
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
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,12 @@ | ||
/** | ||
* Linearly interpolates between two numbers. | ||
* | ||
* ``` | ||
* lerp(0, 10, 0.5) // => 5 | ||
* lerp(5, 15, 0.2) // => 7 | ||
* lerp(-10, 10, 0.75) // => 5 | ||
* ``` | ||
*/ | ||
export function lerp(from: number, to: number, amount: number): number { | ||
return from + (to - from) * amount | ||
} |
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,22 @@ | ||
import * as _ from 'radashi' | ||
|
||
describe('lerp', () => { | ||
test('linearly interpolate between two numbers', () => { | ||
expect(_.lerp(0, 10, 0.5)).toBe(5) | ||
expect(_.lerp(5, 15, 0.2)).toBe(7) | ||
expect(_.lerp(-10, 10, 0.75)).toBe(5) | ||
}) | ||
test('edge cases', () => { | ||
expect(_.lerp(0, 10, 0)).toBe(0) | ||
expect(_.lerp(0, 10, 1)).toBe(10) | ||
expect(_.lerp(5, 5, 0.5)).toBe(5) | ||
}) | ||
test('negative numbers', () => { | ||
expect(_.lerp(-5, 5, 0.5)).toBe(0) | ||
expect(_.lerp(-10, -5, 0.5)).toBe(-7.5) | ||
}) | ||
test('decimal results', () => { | ||
expect(_.lerp(0, 1, 0.3)).toBeCloseTo(0.3, 5) | ||
expect(_.lerp(1, 2, 0.75)).toBeCloseTo(1.75, 5) | ||
}) | ||
}) |