C# Lerping from position to position

梦想的初衷 提交于 2019-12-24 11:26:01

问题


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 :)


回答1:


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.




回答2:


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.




回答3:


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);
    }


来源:https://stackoverflow.com/questions/33044848/c-sharp-lerping-from-position-to-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!