2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCGeometry.h"
|
2012-04-23 14:30:38 +08:00
|
|
|
#include "ccMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// implementation of CCPoint
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CCPoint::CCPoint(void)
|
|
|
|
{
|
2012-04-23 14:30:38 +08:00
|
|
|
setPoint(0.0f, 0.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint::CCPoint(float x, float y)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setPoint(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint::CCPoint(const CCPoint& other)
|
|
|
|
{
|
|
|
|
setPoint(other.x, other.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint& CCPoint::operator= (const CCPoint& other)
|
|
|
|
{
|
|
|
|
setPoint(other.x, other.y);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCPoint::setPoint(float x, float y)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCObject* CCPoint::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCPoint* pRet = new CCPoint();
|
|
|
|
pRet->setPoint(this->x, this->y);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
bool CCPoint::equals(const CCPoint& target) const
|
|
|
|
{
|
|
|
|
return ((x == target.x) && (y == target.y));
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCPoint::CCPointEqualToPoint(const CCPoint& point1, const CCPoint& point2)
|
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return point1.equals(point2);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCSize
|
|
|
|
|
|
|
|
CCSize::CCSize(void)
|
|
|
|
{
|
2012-04-23 14:30:38 +08:00
|
|
|
setSize(0.0f, 0.0f);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSize::CCSize(float width, float height)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setSize(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSize::CCSize(const CCSize& other)
|
|
|
|
{
|
|
|
|
setSize(other.width, other.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSize& CCSize::operator= (const CCSize& other)
|
|
|
|
{
|
|
|
|
setSize(other.width, other.height);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCSize::setSize(float width, float height)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCObject* CCSize::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCSize* pRet = new CCSize();
|
|
|
|
pRet->setSize(this->width, this->width);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
bool CCSize::equals(const CCSize& target) const
|
|
|
|
{
|
|
|
|
return ((width == target.width) && (height == target.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCSize::CCSizeEqualToSize(const CCSize& size1, const CCSize& size2)
|
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return size1.equals(size2);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCRect
|
|
|
|
|
|
|
|
CCRect::CCRect(void)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
CCRect::CCRect(float x, float y, float width, float height)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setRect(x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCRect::CCRect(const CCRect& other)
|
|
|
|
{
|
|
|
|
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCRect& CCRect::operator= (const CCRect& other)
|
|
|
|
{
|
|
|
|
setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCRect::setRect(float x, float y, float width, float height)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Only support that, the width and height > 0
|
2012-04-23 14:30:38 +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;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCObject* CCRect::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCRect* pRet = new CCRect();
|
|
|
|
pRet->setRect(this->origin.x, this->origin.y, this->size.width, this->size.height);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
bool CCRect::equals(const CCRect& 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
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCRect::getMaxX() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return (float)(origin.x + size.width);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCRect::getMidX() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return (float)(origin.x + size.width / 2.0);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCRect::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
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCRect::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
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCRect::getMidY() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return (float)(origin.y + size.height / 2.0);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float CCRect::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
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
bool CCRect::containsPoint(const CCPoint& 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;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
bool CCRect::intersectsRect(const CCRect& rect) const
|
|
|
|
{
|
|
|
|
return !( getMaxX() < rect.getMinX() ||
|
|
|
|
rect.getMaxX() < getMinX() ||
|
|
|
|
getMaxY() < rect.getMinY() ||
|
|
|
|
rect.getMaxY() < getMinY());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCRect::CCRectEqualToRect(const CCRect& rect1, const CCRect& rect2)
|
|
|
|
{
|
|
|
|
return rect1.equals(rect2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCRect::CCRectContainsPoint(const CCRect& rect, const CCPoint& point)
|
|
|
|
{
|
|
|
|
return rect.containsPoint(point);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool CCRect::CCRectIntersectsRect(const CCRect& rectA, const CCRect& rectB)
|
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
/*
|
2012-04-19 14:35:52 +08:00
|
|
|
return !(CCRectGetMaxX(rectA) < CCRectGetMinX(rectB)||
|
|
|
|
CCRectGetMaxX(rectB) < CCRectGetMinX(rectA)||
|
|
|
|
CCRectGetMaxY(rectA) < CCRectGetMinY(rectB)||
|
|
|
|
CCRectGetMaxY(rectB) < CCRectGetMinY(rectA));
|
2012-08-01 15:30:12 +08:00
|
|
|
*/
|
|
|
|
return rectA.intersectsRect(rectB);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|