2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2012-04-19 14:35:52 +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"
|
2014-04-30 08:37:36 +08:00
|
|
|
|
2013-09-12 00:00:48 +08:00
|
|
|
#include <algorithm>
|
2016-07-07 00:38:27 +08:00
|
|
|
#include <cmath>
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
// implementation of Vec2
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Rect
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
Rect::Rect()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-23 14:30:38 +08:00
|
|
|
setRect(0.0f, 0.0f, 0.0f, 0.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect::Rect(float x, float y, float width, float height)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setRect(x, y, width, height);
|
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
Rect::Rect(const Vec2& pos, const Vec2& dimension)
|
2015-06-04 15:43:31 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setRect(pos.x, pos.y, dimension.x, dimension.y);
|
2015-06-04 15:43:31 +08:00
|
|
|
}
|
2012-04-23 14:30:38 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect::Rect(const Rect& other)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setRect(other.origin.x, other.origin.y, other.size.x, other.size.y);
|
2012-04-23 14:30:38 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect& Rect::operator=(const Rect& other)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setRect(other.origin.x, other.origin.y, other.size.x, other.size.y);
|
2012-04-23 14:30:38 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Rect::setRect(float x, float y, float width, float height)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-01-29 14:03:03 +08:00
|
|
|
// CGRect can support width<0 or height<0
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXASSERT(width >= 0.0f && height >= 0.0f, "width and height of Rect must not less than 0.");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
origin.x = x;
|
|
|
|
origin.y = y;
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
size.x = width;
|
|
|
|
size.y = height;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Rect::equals(const Rect& rect) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (origin.equals(rect.origin) && size.equals(rect.size));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Rect::getMaxX() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.x + size.x;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Rect::getMidX() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.x + size.x / 2.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Rect::getMinX() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return origin.x;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Rect::getMaxY() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.y + size.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Rect::getMidY() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
return origin.y + size.y / 2.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Rect::getMinY() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return origin.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Rect::containsPoint(const Vec2& point) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (point.x >= getMinX() && point.x <= getMaxX() && point.y >= getMinY() && point.y <= getMaxY())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Rect::intersectsRect(const Rect& rect) const
|
2012-08-01 15:30:12 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return !(getMaxX() < rect.getMinX() || rect.getMaxX() < getMinX() || getMaxY() < rect.getMinY() ||
|
|
|
|
rect.getMaxY() < getMinY());
|
2012-08-01 15:30:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-14 17:33:58 +08:00
|
|
|
bool Rect::intersectsCircle(const Vec2& center, float radius) const
|
2015-04-12 16:32:11 +08:00
|
|
|
{
|
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
|
|
|
|
2016-07-07 00:38:27 +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
|
|
|
|
2015-04-12 16:32:11 +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));
|
|
|
|
|
2015-04-12 16:32:11 +08:00
|
|
|
if (circleDistance.x <= (w))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-04-12 16:32:11 +08:00
|
|
|
if (circleDistance.y <= (h))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-04-12 16:32:11 +08:00
|
|
|
float cornerDistanceSq = powf(circleDistance.x - w, 2) + powf(circleDistance.y - h, 2);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-04-12 16:32:11 +08:00
|
|
|
return (cornerDistanceSq <= (powf(radius, 2)));
|
|
|
|
}
|
|
|
|
|
2014-09-18 13:11:10 +08:00
|
|
|
void Rect::merge(const Rect& rect)
|
|
|
|
{
|
2015-10-17 03:59:07 +08:00
|
|
|
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);
|
2014-09-18 13:11:10 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect Rect::unionWithRect(const Rect& rect) const
|
2013-07-03 02:39:49 +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;
|
2013-07-03 02:39:49 +08:00
|
|
|
float thisBottomY = origin.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-07-03 02:39:49 +08:00
|
|
|
if (thisRightX < thisLeftX)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(thisRightX, thisLeftX); // This rect has negative width
|
2013-07-03 02:39:49 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-07-03 02:39:49 +08:00
|
|
|
if (thisTopY < thisBottomY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(thisTopY, thisBottomY); // This rect has negative height
|
2013-07-03 02:39:49 +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;
|
2013-07-03 02:39:49 +08:00
|
|
|
float otherBottomY = rect.origin.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-07-03 02:39:49 +08:00
|
|
|
if (otherRightX < otherLeftX)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(otherRightX, otherLeftX); // Other rect has negative width
|
2013-07-03 02:39:49 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-07-03 02:39:49 +08:00
|
|
|
if (otherTopY < otherBottomY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(otherTopY, otherBottomY); // Other rect has negative height
|
2013-07-03 02:39:49 +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);
|
2013-07-03 02:39:49 +08:00
|
|
|
float combinedBottomY = std::min(thisBottomY, otherBottomY);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-07-03 02:39:49 +08:00
|
|
|
return Rect(combinedLeftX, combinedBottomY, combinedRightX - combinedLeftX, combinedTopY - combinedBottomY);
|
|
|
|
}
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
const Rect Rect::ZERO = Rect(0, 0, 0, 0);
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|