forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
37 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 @@ | ||
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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |