diff --git a/geometry.go b/geometry.go index 9651d3cf871d796e41efe00581f32df6472e98da..c6e1c69801d1091474b8944d170e5f556fb08854 100644 --- a/geometry.go +++ b/geometry.go @@ -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. // diff --git a/util.go b/util.go index 45f5ba28cd8a408a6eec0df77a4be34468bf8381..ce46294408f766d7be735bfd8d5ceeee78eb0a4f 100644 --- a/util.go +++ b/util.go @@ -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) -}