C# Lerping from position to position

2019-08-09 23:00发布

I need to make a picture box to lerp from position to position (like you can do that in unity).
How can I do that , is there a built-in function?
thanks :)

标签: c# position lerp
3条回答
够拽才男人
2楼-- · 2019-08-09 23:23

Try this instead

  float Lerp(float a, float b, float t)
    {
        //return firstFloat * by + secondFloat * (1 - by);
        return (1f - t) * a + t * b;
    }

    PointF Lerp(PointF a, PointF b, float t)
    {
        float retX = Lerp(a.X, b.X, t);
        float retY = Lerp(a.Y, b.Y, t);
        return new PointF(retX, retY);
    }
查看更多
来,给爷笑一个
3楼-- · 2019-08-09 23:27

Linear interpolation (lerp) is actually a pretty easy function to implement. The equation is

float Lerp(float firstFloat, float secondFloat, float by)
{
     return firstFloat * (1 - by) + secondFloat * by;
}

A higher order Lerp just wraps lower order lerps:

Vector2 Lerp(Vector2 firstVector, Vector2 secondVector, float by)
{
    float retX = Lerp(firstVector.x, secondVector.x, by);
    float retY = Lerp(firstVector.y, secondVector.y, by);
    return new Vector2(retX, retY);
}

The DirectX SDK has all manner of math functions like Unity, but that's a lot of overhead to bring in just for Lerp. You're probably best off just implementing your own.

查看更多
倾城 Initia
4楼-- · 2019-08-09 23:38

Greg Bahm wrote inverted lerp equation firstFloat * by + secondFloat * (1 - by), where firstFloat is the secondFloat and secondFloat is the firstFloat.

In fact, corrent lerp equation is:

firstFloat * (1 - by) + secondFloat * by

But the fastest way to linear interpolation is:

firstFloat + (secondFloat - firstFloat) * by

That's 2 additions/subtractions and 1 multiplication instead of 2 addition/subtractions and 2 multiplications. Lerp for Vector2 is correct.

查看更多
登录 后发表回答