Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 估计两点距离的Taylor-McLaughlin级数_C++_Algorithm_Graphic_Taylor Series - Fatal编程技术网

C++ 估计两点距离的Taylor-McLaughlin级数

C++ 估计两点距离的Taylor-McLaughlin级数,c++,algorithm,graphic,taylor-series,C++,Algorithm,Graphic,Taylor Series,点到点的距离:dist=sqrt(dx*dx+dy*dy); 但是sqrt太慢了,我不能接受。我在书中找到了一种叫做泰勒-麦克劳克林级数的方法来估计两点之间的距离。但是我不能理解下面的代码。谢谢所有帮助我的人 #define MIN(a, b) ((a < b) ? a : b) int FastDistance2D(int x, int y) { // This function computes the distance from 0,0 to x,y with 3.5% er

点到点的距离:dist=sqrt(dx*dx+dy*dy); 但是sqrt太慢了,我不能接受。我在书中找到了一种叫做泰勒-麦克劳克林级数的方法来估计两点之间的距离。但是我不能理解下面的代码。谢谢所有帮助我的人

#define MIN(a, b) ((a < b) ? a : b)
int FastDistance2D(int x, int y)
{
    // This function computes the distance from 0,0 to x,y with 3.5% error
    // First compute the absolute value of x, y
    x = abs(x);
    y = abs(y);

    // Compute the minimum of x, y
    int mn = MIN(x, y);

    // Return the distance
    return x + y - (mn >> 1) - (mn >> 2) + (mn >> 4);
}
#定义最小值(a,b)((a>1)-(mn>>2)+(mn>>4);
}

我查阅了有关McLaughlin系列的相关数据,但我仍然无法理解返回值如何使用McLaughlin系列来估计值。谢谢大家~

你说得对
sqrt
是一个相当慢的函数。但是你真的需要计算距离吗

在许多情况下,您可以使用distance²来代替。
例如

  • 如果你想知道什么距离更短,你可以比较距离的平方和实际距离
  • 如果要检查距离是否为
    100>,
    也可以检查
    10000>距离平方
  • 在程序中使用偏差平方而不是距离,通常可以避免计算
    sqrt


    这取决于您的应用程序,但始终值得考虑。

    此任务几乎与另一个任务重复:

    这是一篇伟大文章的链接:

    在这篇文章中,你可以找到不同的近似根的方法。例如,这一款可能适合您:

    int isqrt (long r) {
        float tempf, x, y, rr;
        int is;
    
        rr = (long) r;
        y = rr*0.5;
        *(unsigned long *) &tempf = (0xbe6f0000 - *(unsigned long *) &rr) >> 1;
        x = tempf;
        x = (1.5*x) - (x*x)*(x*y);
        if (r > 101123) x = (1.5*x) - (x*x)*(x*y);
        is = (int) (x*rr + 0.5);
        return is + ((signed int) (r - is*is)) >> 31;
    }
    
    如果可以快速计算根操作,则可以按常规方式计算距离:

    return isqrt(a*a+b*b)
    
    还有一个链接:

    u32的近似距离(s32 dx、s32 dy)
    {
    u32最小值,最大值;
    如果(dx<0)dx=-dx;
    如果(dy<0)dy=-dy;
    if(dxreturn(((max)我不明白为什么你的输入和方法的输入由两个整数组成,因为如果你想计算2D中两个点的距离,你需要为每个点计算4个整数(x,y)。注释是从(,0,0)到(x,y)。@MapleWan当你说“在书上”时哪本书?
    dx
    通常代表
    delta x
    ,即2个
    x
    值之间的差异。@m谢谢,你是对的,但有时,我需要距离值来做其他数学计算。@doctor喜欢一本关于游戏编程的书,书名为²:平方(我实在受不了缺少脚注;)谢谢你的建议,当我没有复杂的计算时,这是个好主意。这不是平方根吗?此外,在现代CPU上,这不是个好主意。太棒了,代码比我想象的要复杂得多,非常感谢。我会研究代码并阅读你提供的文章。
    u32 approx_distance( s32 dx, s32 dy )
    {
       u32 min, max;
    
       if ( dx < 0 ) dx = -dx;
       if ( dy < 0 ) dy = -dy;
    
       if ( dx < dy )
       {
          min = dx;
          max = dy;
       } else {
          min = dy;
          max = dx;
       }
    
       // coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min )
       return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) +
                ( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 );
    }