2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "math/CCGeometry.h"
|
|
|
|
|
2013-09-12 00:00:48 +08:00
|
|
|
#include <algorithm>
|
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
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Size
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size::Size(void) : width(0), height(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
Size::Size(float w, float h) : width(w), height(h)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size::Size(const Size& other) : width(other.width), height(other.height)
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Size::Size(const Vec2& point) : width(point.x), height(point.y)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size& Size::operator= (const Size& other)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setSize(other.width, other.height);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Size& Size::operator= (const Vec2& point)
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
|
|
|
setSize(point.x, point.y);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size Size::operator+(const Size& right) const
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Size(this->width + right.width, this->height + right.height);
|
2013-05-03 11:24:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size Size::operator-(const Size& right) const
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Size(this->width - right.width, this->height - right.height);
|
2013-05-03 11:24:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size Size::operator*(float a) const
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Size(this->width * a, this->height * a);
|
2013-05-03 11:24:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size Size::operator/(float a) const
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(a!=0, "CCSize division by 0.");
|
2013-06-20 14:13:12 +08:00
|
|
|
return Size(this->width / a, this->height / a);
|
2013-05-03 11:24:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Size::setSize(float w, float h)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
this->width = w;
|
|
|
|
this->height = h;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Size::equals(const Size& target) const
|
2012-08-01 15:30:12 +08:00
|
|
|
{
|
2013-04-18 06:16:49 +08:00
|
|
|
return (fabs(this->width - target.width) < FLT_EPSILON)
|
|
|
|
&& (fabs(this->height - target.height) < FLT_EPSILON);
|
2012-08-01 15:30:12 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
const Size Size::ZERO = Size(0, 0);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Rect
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect::Rect(void)
|
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);
|
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
Rect::Rect(const Vec2& pos, const Size& dimension)
|
|
|
|
{
|
|
|
|
setRect(pos.x, pos.y, dimension.width, dimension.height);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect& Rect::operator= (const Rect& other)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
|
|
|
|
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
|
2013-07-20 13:01:27 +08:00
|
|
|
// CCASSERT(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;
|
|
|
|
|
|
|
|
size.width = width;
|
|
|
|
size.height = height;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Rect::equals(const Rect& rect) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +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
|
|
|
{
|
2013-09-13 16:37:19 +08:00
|
|
|
return origin.x + size.width;
|
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
|
|
|
{
|
2013-09-13 16:37:19 +08:00
|
|
|
return origin.x + size.width / 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
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return origin.y + size.height;
|
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
|
|
|
{
|
2013-09-13 16:37:19 +08:00
|
|
|
return origin.y + size.height / 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;
|
|
|
|
|
2012-08-01 15:30:12 +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
|
|
|
{
|
|
|
|
return !( getMaxX() < rect.getMinX() ||
|
|
|
|
rect.getMaxX() < getMinX() ||
|
|
|
|
getMaxY() < rect.getMinY() ||
|
|
|
|
rect.getMaxY() < getMinY());
|
|
|
|
}
|
|
|
|
|
2015-04-12 16:32:11 +08:00
|
|
|
bool Rect::intersectsCircle(const cocos2d::Vec2 ¢er, 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 = fabs(center.x - rectangleCenter.x);
|
|
|
|
float dy = fabs(center.y - rectangleCenter.y);
|
|
|
|
|
|
|
|
if (dx > (radius + w) || dy > (radius + h))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 circleDistance(fabs(center.x - origin.x - w),
|
|
|
|
fabs(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)));
|
|
|
|
}
|
|
|
|
|
2014-09-18 13:11:10 +08:00
|
|
|
void Rect::merge(const Rect& rect)
|
|
|
|
{
|
|
|
|
float top1 = getMaxY();
|
|
|
|
float left1 = getMinX();
|
|
|
|
float right1 = getMaxX();
|
|
|
|
float bottom1 = getMinY();
|
|
|
|
|
|
|
|
float top2 = rect.getMaxY();
|
|
|
|
float left2 = rect.getMinX();
|
|
|
|
float right2 = rect.getMaxX();
|
|
|
|
float bottom2 = rect.getMinY();
|
|
|
|
origin.x = std::min(left1, left2);
|
|
|
|
origin.y = std::min(bottom1, bottom2);
|
|
|
|
size.width = std::max(right1, right2) - origin.x;
|
|
|
|
size.height = std::max(top1, top2) - origin.y;
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:39:49 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
const Rect Rect::ZERO = Rect(0, 0, 0, 0);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|