Skip to content

Commit

Permalink
feat: add lerp (TheAlgorithms#545)
Browse files Browse the repository at this point in the history
* add lerp

* fix: add description
  • Loading branch information
b5710546232 authored Oct 7, 2022
1 parent 4d9b1e4 commit 51f0485
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions math/lerp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package math

// Lerp or Linear interpolation
// This function will return new value in 't' percentage between 'v0' and 'v1'
func Lerp(v0, v1, t float64) float64 {
// see: https://en.wikipedia.org/wiki/Linear_interpolation
return (1-t)*v0 + t*v1
}
29 changes: 29 additions & 0 deletions math/lerp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package math_test

import (
"testing"

algmath "github.com/TheAlgorithms/Go/math"
)

func TestLerp(t *testing.T) {
tests := []struct {
name string
testValues []float64
answer float64
}{
{"Lerp(1,1,1)", []float64{1, 1, 1}, 1},
{"Lerp(0,1,1)", []float64{0, 1, 1}, 1},
{"Lerp(0,1,0.5)", []float64{0, 1, 0.5}, 0.5},
{"Lerp(0,1,0.1)", []float64{0, 1, 0.1}, 0.1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := algmath.Lerp(test.testValues[0], test.testValues[1], test.testValues[2])
if got != test.answer {
t.Errorf("Lerp(%f,%f,%f) = %v, want %v", got, test.testValues[0],
test.testValues[1], test.testValues[2], test.answer)
}
})
}
}

0 comments on commit 51f0485

Please sign in to comment.