2011-11-26 17:07:36 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2007 Scott Lembcke
|
2011-11-26 17:07:36 +08:00
|
|
|
Copyright (c) 2010 Lam Pham
|
|
|
|
|
|
|
|
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 "CCPointExtension.h"
|
|
|
|
#include "ccMacros.h" // FLT_EPSILON
|
|
|
|
#include <stdio.h>
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2011-11-26 17:07:36 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#define kPointEpsilon FLT_EPSILON
|
2010-07-16 14:54:26 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float
|
2013-06-20 14:13:12 +08:00
|
|
|
ccpLength(const Point& v)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-05-05 00:58:46 +08:00
|
|
|
return v.getLength();
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float
|
2013-06-20 14:13:12 +08:00
|
|
|
ccpDistance(const Point& v1, const Point& v2)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return v1.getDistance(v2);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point
|
|
|
|
ccpNormalize(const Point& v)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-05-04 15:18:22 +08:00
|
|
|
return v.normalize();
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point
|
2012-08-01 15:30:12 +08:00
|
|
|
ccpForAngle(const float a)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point::forAngle(a);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
float
|
2013-06-20 14:13:12 +08:00
|
|
|
ccpToAngle(const Point& v)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-05-05 00:58:46 +08:00
|
|
|
return v.getAngle();
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpLerp(const Point& a, const Point& b, float alpha)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-05-04 15:18:22 +08:00
|
|
|
return a.lerp(b, alpha);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpClamp(const Point& p, const Point& min_inclusive, const Point& max_inclusive)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return p.getClampPoint(min_inclusive, max_inclusive);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpFromSize(const Size& s)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Point(s);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpCompOp(const Point& p, float (*opFunc)(float))
|
2011-07-05 12:05:19 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return p.compOp(opFunc);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ccpFuzzyEqual(const Point& a, const Point& b, float var)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-05-04 15:18:22 +08:00
|
|
|
return a.fuzzyEquals(b, var);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpCompMult(const Point& a, const Point& b)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return ccp(a.x * b.x, a.y * b.y);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float ccpAngleSigned(const Point& a, const Point& b)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-05-05 00:58:46 +08:00
|
|
|
return a.getAngle(b);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpRotateByAngle(const Point& v, const Point& pivot, float angle)
|
2011-07-05 12:05:19 +08:00
|
|
|
{
|
2013-05-04 15:18:22 +08:00
|
|
|
return v.rotateByAngle(pivot, angle);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2011-11-26 17:07:36 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ccpSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D)
|
2011-11-26 17:07:36 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return Point::isSegmentIntersect(A, B, C, D);
|
2011-11-26 17:07:36 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ccpIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D)
|
2011-11-26 17:07:36 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return Point::getIntersectPoint(A, B, C, D);
|
2011-07-08 14:16:10 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ccpLineIntersect(const Point& A, const Point& B,
|
|
|
|
const Point& C, const Point& D,
|
2012-04-19 14:35:52 +08:00
|
|
|
float *S, float *T)
|
2011-07-05 12:05:19 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return Point::isLineIntersect(A, B, C, D, S, T);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float ccpAngle(const Point& a, const Point& b)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
return a.getAngle(b);
|
2010-08-04 15:46:12 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_END
|