【发布时间】:2013-09-12 16:02:07
【问题描述】:
点到点的距离:dist = sqrt(dx * dx + dy * dy); 但是 sqrt 太慢了,我不能接受。我找到了一种叫做 Taylor McLaughlin Series 的方法来估计书上两点的距离。但我无法理解以下代码。感谢任何帮助我的人。
#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);
}
我查阅了有关 McLaughlin Series 的相关数据,但我仍然无法理解返回值是如何使用 McLaughlin Series 来估算值的。谢谢大家~
【问题讨论】:
-
我不明白为什么你的输入和方法的输入由两个整数组成,因为如果你想计算二维中两点的距离,你需要 4 个整数,每个 (x,y)点。
-
评论确实说从 (,0,0) 到 (x,y)。 @MapleWan 当你说“在书上”时是哪本书?
-
dx通常代表delta x,即两个x值之间的差异。 -
@MSalters 谢谢,你是对的,但有时,我需要距离值来进行其他数学计算。
-
@doctorlove 一本关于游戏编程的书,叫做
标签: c++ algorithm graphic taylor-series