2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2017 Chukong Technologies
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
#include "math/Rect.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
|
|
|
|
// implementation of Vec2
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// implementation of Rect
|
|
|
|
|
|
|
|
Rect::Rect()
|
|
|
|
{
|
|
|
|
setRect(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect::Rect(float x, float y, float width, float height)
|
|
|
|
{
|
|
|
|
setRect(x, y, width, height);
|
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
Rect::Rect(const Vec2& pos, const Vec2& dimension)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setRect(pos.x, pos.y, dimension.x, dimension.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Rect::Rect(const Rect& other)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setRect(other.origin.x, other.origin.y, other.size.x, other.size.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect& Rect::operator=(const Rect& other)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setRect(other.origin.x, other.origin.y, other.size.x, other.size.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rect::setRect(float x, float y, float width, float height)
|
|
|
|
{
|
|
|
|
// CGRect can support width<0 or height<0
|
|
|
|
// CCASSERT(width >= 0.0f && height >= 0.0f, "width and height of Rect must not less than 0.");
|
|
|
|
|
|
|
|
origin.x = x;
|
|
|
|
origin.y = y;
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
size.x = width;
|
|
|
|
size.y = height;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Rect::equals(const Rect& rect) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (origin.equals(rect.origin) && size.equals(rect.size));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Rect::getMaxX() const
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.x + size.x;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Rect::getMidX() const
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.x + size.x / 2.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Rect::getMinX() const
|
|
|
|
{
|
|
|
|
return origin.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Rect::getMaxY() const
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.y + size.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Rect::getMidY() const
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.y + size.y / 2.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Rect::getMinY() const
|
|
|
|
{
|
|
|
|
return origin.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Rect::containsPoint(const Vec2& point) const
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (point.x >= getMinX() && point.x <= getMaxX() && point.y >= getMinY() && point.y <= getMaxY())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Rect::intersectsRect(const Rect& rect) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return !(getMaxX() < rect.getMinX() || rect.getMaxX() < getMinX() || getMaxY() < rect.getMinY() ||
|
|
|
|
rect.getMaxY() < getMinY());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Rect::intersectsCircle(const Vec2& center, float radius) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 rectangleCenter((origin.x + size.x / 2), (origin.y + size.y / 2));
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
float w = size.x / 2;
|
|
|
|
float h = size.y / 2;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float dx = std::abs(center.x - rectangleCenter.x);
|
|
|
|
float dy = std::abs(center.y - rectangleCenter.y);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (dx > (radius + w) || dy > (radius + h))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Vec2 circleDistance(std::abs(center.x - origin.x - w), std::abs(center.y - origin.y - h));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (circleDistance.x <= (w))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (circleDistance.y <= (h))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float cornerDistanceSq = powf(circleDistance.x - w, 2) + powf(circleDistance.y - h, 2);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return (cornerDistanceSq <= (powf(radius, 2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect Rect::unionWithRect(const Rect& rect) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float thisLeftX = origin.x;
|
|
|
|
float thisRightX = origin.x + size.x;
|
|
|
|
float thisTopY = origin.y + size.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
float thisBottomY = origin.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (thisRightX < thisLeftX)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(thisRightX, thisLeftX); // This rect has negative width
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (thisTopY < thisBottomY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(thisTopY, thisBottomY); // This rect has negative height
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
float otherLeftX = rect.origin.x;
|
|
|
|
float otherRightX = rect.origin.x + rect.size.x;
|
|
|
|
float otherTopY = rect.origin.y + rect.size.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
float otherBottomY = rect.origin.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (otherRightX < otherLeftX)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(otherRightX, otherLeftX); // Other rect has negative width
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (otherTopY < otherBottomY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(otherTopY, otherBottomY); // Other rect has negative height
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
float combinedLeftX = std::min(thisLeftX, otherLeftX);
|
|
|
|
float combinedRightX = std::max(thisRightX, otherRightX);
|
|
|
|
float combinedTopY = std::max(thisTopY, otherTopY);
|
2019-11-23 20:27:39 +08:00
|
|
|
float combinedBottomY = std::min(thisBottomY, otherBottomY);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return Rect(combinedLeftX, combinedBottomY, combinedRightX - combinedLeftX, combinedTopY - combinedBottomY);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Rect Rect::ZERO = Rect(0, 0, 0, 0);
|
|
|
|
|
|
|
|
NS_CC_END
|