2010-07-13 13:44:00 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCGeometry.h"
|
2010-07-13 13:44:00 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
// implementation of CCPoint
|
2010-08-04 15:46:12 +08:00
|
|
|
namespace cocos2d {
|
2010-07-13 13:44:00 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint::CCPoint(void)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint::CCPoint(float x, float y)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
bool CCPoint::CCPointEqualToPoint(const CCPoint& point1, const CCPoint& point2)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return ((point1.x == point2.x) && (point1.y == point2.y));
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
// implementation of CCSize
|
2010-07-13 13:44:00 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCSize::CCSize(void)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCSize::CCSize(float width, float height)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
bool CCSize::CCSizeEqualToSize(const CCSize& size1, const CCSize& size2)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return ((size1.width == size2.width) && (size1.height == size2.height));
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
// implementation of CCRect
|
2010-07-13 13:44:00 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect::CCRect(void)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
origin.x = 0;
|
|
|
|
origin.y = 0;
|
|
|
|
|
|
|
|
size.width = 0;
|
|
|
|
size.height = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect::CCRect(float x, float y, float width, float height)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
2011-08-22 14:55:51 +08:00
|
|
|
// Only support that, the width and height > 0
|
2011-09-19 17:53:45 +08:00
|
|
|
CC_ASSERT(width >= 0 && height >= 0);
|
2011-08-22 14:55:51 +08:00
|
|
|
|
2010-07-13 13:44:00 +08:00
|
|
|
origin.x = x;
|
|
|
|
origin.y = y;
|
|
|
|
|
|
|
|
size.width = width;
|
|
|
|
size.height = height;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
bool CCRect::CCRectEqualToRect(const CCRect& rect1, const CCRect& rect2)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
return (CCPoint::CCPointEqualToPoint(rect1.origin, rect2.origin)
|
|
|
|
&& CCSize::CCSizeEqualToSize(rect1.size, rect2.size));
|
2010-07-13 13:44:00 +08:00
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat CCRect::CCRectGetMaxX(const CCRect& rect)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return rect.origin.x + rect.size.width;
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat CCRect::CCRectGetMidX(const CCRect& rect)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return (float)(rect.origin.x + rect.size.width / 2.0);
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat CCRect::CCRectGetMinX(const CCRect& rect)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return rect.origin.x;
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat CCRect::CCRectGetMaxY(const CCRect& rect)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return rect.origin.y + rect.size.height;
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat CCRect::CCRectGetMidY(const CCRect& rect)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return (float)(rect.origin.y + rect.size.height / 2.0);
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat CCRect::CCRectGetMinY(const CCRect& rect)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
return rect.origin.y;
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
bool CCRect::CCRectContainsPoint(const CCRect& rect, const CCPoint& point)
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
if (point.x >= CCRectGetMinX(rect) && point.x <= CCRectGetMaxX(rect)
|
|
|
|
&& point.y >= CCRectGetMinY(rect) && point.y <= CCRectGetMaxY(rect))
|
2010-07-13 13:44:00 +08:00
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
2010-09-29 11:49:38 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
bool CCRect::CCRectIntersectsRect(const CCRect& rectA, const CCRect& rectB)
|
2010-09-29 11:49:38 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
return !(CCRectGetMaxX(rectA) < CCRectGetMinX(rectB)||
|
|
|
|
CCRectGetMaxX(rectB) < CCRectGetMinX(rectA)||
|
|
|
|
CCRectGetMaxY(rectA) < CCRectGetMinY(rectB)||
|
|
|
|
CCRectGetMaxY(rectB) < CCRectGetMinY(rectA));
|
2010-09-29 11:49:38 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 15:46:12 +08:00
|
|
|
}//namespace cocos2d {
|