cocos2dx Geometry Size和Rect

Size

代码都是基础代码不注释了,写一些特别的
1、赋值时可以接收Size和Vec2类型的值,保证的类型的兼容性
2、对运算符进行了重载,可以按照正常的数学逻辑运算
3.、可以使用equals对比大小

Rect

1、Rect(x, y, width, height) 四个参数分别表示起点的 xy坐标 和 宽高
2、可以直接接受Vec2和Size作为参数,如 Rect(Vec2,Size)

//是否包含在矩形内
bool Rect::containsPoint(const Vec2& point) const
{
    return (point.x >= getMinX() &&
            point.x <= getMaxX() &&
            point.y >= getMinY() &&
            point.y <= getMaxY());
}

//判断是否相交
bool Rect::intersectsRect(const Rect& rect) const
{
    return !(     getMaxX() < rect.getMinX() ||
             rect.getMaxX() <      getMinX() ||
                  getMaxY() < rect.getMinY() ||
             rect.getMaxY() <      getMinY());
}

//与圆是否相交
bool Rect::intersectsCircle(const Vec2& center, float radius) const
{
    //矩形中心
    Vec2 rectangleCenter((origin.x + size.width / 2),
                         (origin.y + size.height / 2));
    
    //矩形宽高的一半
    float w = size.width / 2;
    float h = size.height / 2;
    
    //圆心和矩形中心的距离
    float dx = std::abs(center.x - rectangleCenter.x);
    float dy = std::abs(center.y - rectangleCenter.y);
    
    //保证圆和矩形相交
    //短板效应
    //如果两个中点距离大于圆半径+宽或高的一半就返回false 因为如果这样圆和矩形不会相交 ?
    if (dx > (radius + w) || dy > (radius + h))
    {
        return false;
    }
    
    //获取两心的最小距离
    Vec2 circleDistance(std::abs(center.x - origin.x - w),
                        std::abs(center.y - origin.y - h));
    
    //圆在矩形内
    if (circleDistance.x <= (w))
    {
        return true;
    }
    
    if (circleDistance.y <= (h))
    {
        return true;
    }
    
    //矩形在圆内
    float cornerDistanceSq = powf(circleDistance.x - w, 2) + powf(circleDistance.y - h, 2);
    
    return (cornerDistanceSq <= (powf(radius, 2)));
}

//找到可以容纳两种图形的最小矩形,并将当前rect设置为计算得到的矩形
void Rect::merge(const Rect& rect)
{
    float minX = std::min(getMinX(), rect.getMinX());
    float minY = std::min(getMinY(), rect.getMinY());
    float maxX = std::max(getMaxX(), rect.getMaxX());
    float maxY = std::max(getMaxY(), rect.getMaxY());
    setRect(minX, minY, maxX - minX, maxY - minY);
}

//计算可以容纳两个矩形的最小矩形并返回
Rect Rect::unionWithRect(const Rect & rect) const
{
    //获取四个顶点坐标
    float thisLeftX = origin.x;
    float thisRightX = origin.x + size.width;
    float thisTopY = origin.y + size.height;
    float thisBottomY = origin.y;
    
    //交换大小
    if (thisRightX < thisLeftX)
    {
        std::swap(thisRightX, thisLeftX);   // This rect has negative width
    }
    
    if (thisTopY < thisBottomY)
    {
        std::swap(thisTopY, thisBottomY);   // This rect has negative height
    }
    
    //获取目标四个顶点坐标
    float otherLeftX = rect.origin.x;
    float otherRightX = rect.origin.x + rect.size.width;
    float otherTopY = rect.origin.y + rect.size.height;
    float otherBottomY = rect.origin.y;
    
    //交换大小
    if (otherRightX < otherLeftX)
    {
        std::swap(otherRightX, otherLeftX);   // Other rect has negative width
    }
    
    if (otherTopY < otherBottomY)
    {
        std::swap(otherTopY, otherBottomY);   // Other rect has negative height
    }
    
    //找到可以容纳两个矩形的最小矩形,并返回这个最小矩形
    float combinedLeftX = std::min(thisLeftX, otherLeftX);
    float combinedRightX = std::max(thisRightX, otherRightX);
    float combinedTopY = std::max(thisTopY, otherTopY);
    float combinedBottomY = std::min(thisBottomY, otherBottomY);
    
    return Rect(combinedLeftX, combinedBottomY, combinedRightX - combinedLeftX, combinedTopY - combinedBottomY);
}


const Rect Rect::ZERO = Rect(0, 0, 0, 0);

NS_CC_END

主要用来判断是否相交、包含,可能会用在碰撞检测中。

原文地址:https://www.cnblogs.com/sakuraneo/p/11992051.html