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"
|
2013-09-12 00:00:48 +08:00
|
|
|
#include <algorithm>
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Point
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point::Point(void) : x(0), y(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
Point::Point(float xx, float yy) : x(xx), y(yy)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point::Point(const Point& other) : x(other.x), y(other.y)
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point::Point(const Size& size) : x(size.width), y(size.height)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point& Point::operator= (const Point& other)
|
2012-04-23 14:30:38 +08:00
|
|
|
{
|
|
|
|
setPoint(other.x, other.y);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point& Point::operator= (const Size& size)
|
2013-05-03 11:24:52 +08:00
|
|
|
{
|
|
|
|
setPoint(size.width, size.height);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point Point::operator+(const Point& right) const
|
2013-05-02 13:13:03 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point(this->x + right.x, this->y + right.y);
|
2013-05-02 13:13:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point Point::operator-(const Point& right) const
|
2013-05-02 13:13:03 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point(this->x - right.x, this->y - right.y);
|
2013-05-02 13:13:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point Point::operator-() const
|
2013-05-04 15:18:22 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point(-x, -y);
|
2013-05-04 15:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point Point::operator*(float a) const
|
2013-05-02 13:13:03 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point(this->x * a, this->y * a);
|
2013-05-02 13:13:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point Point::operator/(float a) const
|
2013-05-02 13:13:03 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(a!=0, "CCPoint division by 0.");
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point(this->x / a, this->y / a);
|
2013-05-02 13:13:03 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Point::setPoint(float xx, float yy)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
this->x = xx;
|
|
|
|
this->y = yy;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Point::equals(const Point& target) const
|
2012-08-01 15:30:12 +08:00
|
|
|
{
|
2013-04-18 06:16:49 +08:00
|
|
|
return (fabs(this->x - target.x) < FLT_EPSILON)
|
|
|
|
&& (fabs(this->y - target.y) < FLT_EPSILON);
|
2012-08-01 15:30:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Point::fuzzyEquals(const Point& b, float var) const
|
2013-05-04 15:18:22 +08:00
|
|
|
{
|
2013-05-05 00:58:46 +08:00
|
|
|
if(x - var <= b.x && b.x <= x + var)
|
2013-05-04 15:18:22 +08:00
|
|
|
if(y - var <= b.y && b.y <= y + var)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Point::getAngle(const Point& other) const
|
2013-05-04 15:18:22 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point a2 = normalize();
|
|
|
|
Point b2 = other.normalize();
|
2013-05-04 15:18:22 +08:00
|
|
|
float angle = atan2f(a2.cross(b2), a2.dot(b2));
|
|
|
|
if( fabs(angle) < FLT_EPSILON ) return 0.f;
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point Point::rotateByAngle(const Point& pivot, float angle) const
|
2013-05-04 15:18:22 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return pivot + (*this - pivot).rotate(Point::forAngle(angle));
|
2013-05-04 15:18:22 +08:00
|
|
|
}
|
|
|
|
|
2013-08-26 13:45:45 +08:00
|
|
|
bool Point::isOneDemensionSegmentOverlap(float A, float B, float C, float D, float *S, float *E)
|
2013-08-23 14:37:56 +08:00
|
|
|
{
|
|
|
|
float ABmin = MIN(A, B);
|
|
|
|
float ABmax = MAX(A, B);
|
|
|
|
float CDmin = MIN(C, D);
|
|
|
|
float CDmax = MAX(C, D);
|
|
|
|
|
|
|
|
if (ABmax < CDmin || CDmax < ABmin)
|
|
|
|
{
|
|
|
|
// ABmin->ABmax->CDmin->CDmax or CDmin->CDmax->ABmin->ABmax
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ABmin >= CDmin && ABmin <= CDmax)
|
|
|
|
{
|
|
|
|
// CDmin->ABmin->CDmax->ABmax or CDmin->ABmin->ABmax->CDmax
|
2013-08-26 13:45:45 +08:00
|
|
|
if (S != nullptr) *S = ABmin;
|
|
|
|
if (E != nullptr) *E = CDmax < ABmax ? CDmax : ABmax;
|
2013-08-23 14:37:56 +08:00
|
|
|
}
|
|
|
|
else if (ABmax >= CDmin && ABmax <= CDmax)
|
|
|
|
{
|
|
|
|
// ABmin->CDmin->ABmax->CDmax
|
2013-08-26 13:45:45 +08:00
|
|
|
if (S != nullptr) *S = CDmin;
|
|
|
|
if (E != nullptr) *E = ABmax;
|
2013-08-23 14:37:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ABmin->CDmin->CDmax->ABmax
|
2013-08-26 13:45:45 +08:00
|
|
|
if (S != nullptr) *S = CDmin;
|
|
|
|
if (E != nullptr) *E = CDmax;
|
2013-08-23 14:37:56 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 16:38:58 +08:00
|
|
|
bool Point::isLineIntersect(const Point& A, const Point& B,
|
|
|
|
const Point& C, const Point& D,
|
|
|
|
float *S, float *T)
|
|
|
|
{
|
|
|
|
// FAIL: Line undefined
|
|
|
|
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-26 13:45:45 +08:00
|
|
|
const float denom = crossProduct2Vector(A, B, C, D);
|
2013-07-11 16:38:58 +08:00
|
|
|
|
|
|
|
if (denom == 0)
|
|
|
|
{
|
2013-08-26 13:45:45 +08:00
|
|
|
// Lines parallel or overlap
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S != nullptr) *S = crossProduct2Vector(C, D, C, A) / denom;
|
|
|
|
if (T != nullptr) *T = crossProduct2Vector(A, B, C, A) / denom;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Point::isLineParallel(const Point& A, const Point& B,
|
|
|
|
const Point& C, const Point& D)
|
|
|
|
{
|
|
|
|
// FAIL: Line undefined
|
|
|
|
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crossProduct2Vector(A, B, C, D) == 0)
|
|
|
|
{
|
|
|
|
// line overlap
|
|
|
|
if (crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0)
|
2013-07-11 16:38:58 +08:00
|
|
|
{
|
2013-08-26 13:45:45 +08:00
|
|
|
return false;
|
2013-07-11 16:38:58 +08:00
|
|
|
}
|
2013-08-26 13:45:45 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Point::isLineOverlap(const Point& A, const Point& B,
|
|
|
|
const Point& C, const Point& D)
|
|
|
|
{
|
|
|
|
// FAIL: Line undefined
|
|
|
|
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-26 13:45:45 +08:00
|
|
|
if (crossProduct2Vector(A, B, C, D) == 0 &&
|
|
|
|
(crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Point::isSegmentOverlap(const Point& A, const Point& B, const Point& C, const Point& D, Point* S, Point* E)
|
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
|
2013-08-26 13:45:45 +08:00
|
|
|
if (isLineOverlap(A, B, C, D))
|
|
|
|
{
|
|
|
|
return isOneDemensionSegmentOverlap(A.x, B.x, C.x, D.x, &S->x, &E->x) &&
|
|
|
|
isOneDemensionSegmentOverlap(A.y, B.y, C.y, D.y, &S->y, &E->y);
|
|
|
|
}
|
2013-07-11 16:38:58 +08:00
|
|
|
|
2013-08-26 13:45:45 +08:00
|
|
|
return false;
|
2013-07-11 16:38:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Point::isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D)
|
|
|
|
{
|
|
|
|
float S, T;
|
|
|
|
|
|
|
|
if (isLineIntersect(A, B, C, D, &S, &T )&&
|
2013-08-26 13:45:45 +08:00
|
|
|
(S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f))
|
2013-07-11 16:38:58 +08:00
|
|
|
{
|
|
|
|
return true;
|
2013-08-26 13:45:45 +08:00
|
|
|
}
|
2013-07-11 16:38:58 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Point Point::getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D)
|
|
|
|
{
|
|
|
|
float S, T;
|
|
|
|
|
|
|
|
if (isLineIntersect(A, B, C, D, &S, &T))
|
|
|
|
{
|
|
|
|
// Point of intersection
|
|
|
|
Point P;
|
|
|
|
P.x = A.x + S * (B.x - A.x);
|
|
|
|
P.y = A.y + S * (B.y - A.y);
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
2013-07-12 14:47:36 +08:00
|
|
|
return Point::ZERO;
|
2013-07-11 16:38:58 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
const Point Point::ZERO = Point(0, 0);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size::Size(const Point& 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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Size& Size::operator= (const Point& 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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return (float)(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
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return (float)(origin.x + size.width / 2.0);
|
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
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
return (float)(origin.y + size.height / 2.0);
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Rect::containsPoint(const Point& 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());
|
|
|
|
}
|
|
|
|
|
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
|