c++ - 如果结果为假,如何再次检查会导致更多错误结果?

标签 c++

我正在检查矩形中的点。我不明白如何添加(检查下面代码中的 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/

相关文章:

c++ - Visual Studio 和 Xcode 中的 C++ 运算符重载错误

c++ - kd-tree 构建非常慢

c++ - 将特定数组元素的地址传递给函数

C++ 奇怪的函数行为

c++ - 理解指针的使用

c++ - 从Pytorch C++中的c10::Dict <c10::IValue,c10::IValue>获取值

C++ 代码不在同一行中显示输出

c++ - 使用以编程方式创建的 Windows 媒体播放器进行基本播放

c++ - 我怎样才能让 GCC 在 ROM 中放置一个 C++ constexpr?

c++ - GCC 4.7 从初始化器列表初始化 unique_ptrs 容器失败