我正在检查矩形中的点。我不明白如何添加(检查下面代码中的 2 号 if 语句)导致代码导致比没有它更多的错误返回。有人可以帮助我理解此代码行为。
我原本希望添加这个 if 语句来获得相同数量或更少的错误返回值。
if (p.x <= center.x + halfWidth && p.x >= center.x - halfWidth
&& p.y <= center.y + halfHeight && p.y >= center.y - halfHeight)
{
return true;
}
else
{
//Check number # 2
if ( ( (p.x <= center.x + halfWidth && p.x >= center.x - halfWidth)
|| (p.x - center.x + halfWidth < 0.0001)||(p.x - center.x - halfWidth) < 0.0001)
&& ((p.y <= center.y + halfHeight && p.y >= center.y - halfHeight)
|| p.y - center.y + halfHeight < 0.0001 || p.y - center.y - halfHeight < 0.0001))
{
return true;
}
return false;
}
请忽略我使用的是 (0.0001) 而不是 epsilon
最佳答案
使用边缘而不是中心点会不会更容易?
float left = center.x - halfWidth; // halfWidth??
float right = center.x + halfWith; // having rect defined this way
float top = center.y + halfHeight; // adds 1 * FLT_EPSILOM error
float bottom = center.y - halfHeight; // to all calculus ops.
float error_x = fabs(p.x * 2 * FLT_EPSILOM);
float error_y = fabs(p.y * 2 * FLT_EPSILOM);
return (left <= (p.x + error_x) && (p.x - error_x) <= right)
&& (bottom <= (p.y + error_y) && (p.y - error_y) <= top);
关于c++ - 如果结果为假,如何再次检查会导致更多错误结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45226871/