Description
The existing interpolation functions are between two values of Double
, Vector2
or Point
, but for some cases it's useful to interpolate between a larger group of values, which apparently is called piecewise linear interpolation. Non linear versions exist but this one should be fairly simple to implement.
A generic signature would look something like this:
case class PiecewiseLerpResult(value: Vector2, derivative: Vector2)
// at should be a value between 0.0 and 1.0
def piecewiseLerp(vectors: List[Vector2], at: Double): PiecewiseLerpResult
The two values it returns are the interpolated value and it's derivative which for a use case where the Vector2 represents position, the value would be the current position and the derivative its direction and speed.
And to integrate with Indigo's signals it would look like this:
// Over a time period
def piecewiseLerp(vectors: Vector2, over: Seconds): SignalFunction[Seconds, PiecewiseLerpResult]
// From 0.0 to 1.0
def piecewiseLerp(vectors: Vector2): SignalFunction[Double, PiecewiseLerpResult]
I created a type for the result because I find tuples like (Vector2, Vector2)
can get confusing.
An assumption of this function is that the waypoints would be spaced relative to their distance, rather than equaly distributed over time. This would maintain constant traveling speed over the whole path which is something we usually want. But if we would also create a picewiseLerp for Double values instead of Vector2, it's not clear that this would be the desired behaviour.