add Lerp function
This commit is contained in:
parent
c1c1bae358
commit
28f0f01173
|
@ -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
|
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It has a position
|
||||||
// and a size.
|
// and a size.
|
||||||
//
|
//
|
||||||
|
|
19
util.go
19
util.go
|
@ -12,17 +12,6 @@ func clamp(x, low, high float64) float64 {
|
||||||
return x
|
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 {
|
func transformToMat(t ...Transform) mgl32.Mat3 {
|
||||||
mat := mgl32.Ident3()
|
mat := mgl32.Ident3()
|
||||||
for i := range t {
|
for i := range t {
|
||||||
|
@ -30,11 +19,3 @@ func transformToMat(t ...Transform) mgl32.Mat3 {
|
||||||
}
|
}
|
||||||
return mat
|
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)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue