add Lerp function

This commit is contained in:
faiface 2017-03-05 11:47:18 +01:00
parent c1c1bae358
commit 28f0f01173
2 changed files with 9 additions and 19 deletions

View File

@ -106,6 +106,15 @@ func (u Vec) Map(f func(float64) float64) Vec {
)
}
// Lerp returns a linear interpolation between vectors a and b.
//
// This function basically returns a point along the line between a and b and t chooses which point.
// If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will
// return the appropriate point between a and b and so on.
func Lerp(a, b Vec, t float64) Vec {
return a.Scaled(1-t) + b.Scaled(t)
}
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It has a position
// and a size.
//

19
util.go
View File

@ -12,17 +12,6 @@ func clamp(x, low, high float64) float64 {
return x
}
func lerp(x float64, a, b Vec) Vec {
return a.Scaled(1-x) + b.Scaled(x)
}
func lerp2d(x, a, b Vec) Vec {
return V(
lerp(x.X(), a, b).X(),
lerp(x.Y(), a, b).Y(),
)
}
func transformToMat(t ...Transform) mgl32.Mat3 {
mat := mgl32.Ident3()
for i := range t {
@ -30,11 +19,3 @@ func transformToMat(t ...Transform) mgl32.Mat3 {
}
return mat
}
func pictureBounds(p Picture, v Vec) Vec {
w, h := p.Bounds().Size.XY()
a := p.Bounds().Pos
b := p.Bounds().Pos + p.Bounds().Size
u := lerp2d(v, a, b)
return V(u.X()/w, u.Y()/h)
}