Skip to content

Commit

Permalink
feat: add lerp function (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson authored Jul 8, 2024
1 parent 3401240 commit 072778f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions benchmarks/number/lerp.bench.ts
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)
})
})
22 changes: 22 additions & 0 deletions docs/number/lerp.mdx
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.
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export * from './curry/proxied.ts'
export * from './curry/throttle.ts'

export * from './number/inRange.ts'
export * from './number/lerp.ts'
export * from './number/round.ts'
export * from './number/toFloat.ts'
export * from './number/toInt.ts'
Expand Down
12 changes: 12 additions & 0 deletions src/number/lerp.ts
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
}
22 changes: 22 additions & 0 deletions tests/number/lerp.test.ts
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)
})
})

0 comments on commit 072778f

Please sign in to comment.