2011-11-26 17:07:36 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 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
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
#define kCCPointEpsilon FLT_EPSILON
|
2010-07-16 14:54:26 +08:00
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat
|
2011-04-16 14:55:02 +08:00
|
|
|
ccpLength(const CCPoint& v)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return sqrtf(ccpLengthSQ(v));
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat
|
2011-04-16 14:55:02 +08:00
|
|
|
ccpDistance(const CCPoint& v1, const CCPoint& v2)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccpLength(ccpSub(v1, v2));
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint
|
2011-04-16 14:55:02 +08:00
|
|
|
ccpNormalize(const CCPoint& v)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccpMult(v, 1.0f/ccpLength(v));
|
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint
|
2012-04-13 14:09:02 +08:00
|
|
|
ccpForAngle(const CCFloat a)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccp(cosf(a), sinf(a));
|
|
|
|
}
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
CCFloat
|
2011-04-16 14:55:02 +08:00
|
|
|
ccpToAngle(const CCPoint& v)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return atan2f(v.y, v.x);
|
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
CCPoint ccpLerp(const CCPoint& a, const CCPoint& b, float alpha)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccpAdd(ccpMult(a, 1.f - alpha), ccpMult(b, alpha));
|
|
|
|
}
|
|
|
|
|
|
|
|
float clampf(float value, float min_inclusive, float max_inclusive)
|
|
|
|
{
|
|
|
|
if (min_inclusive > max_inclusive) {
|
2011-11-26 17:07:36 +08:00
|
|
|
CC_SWAP(min_inclusive, max_inclusive, float);
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
|
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
CCPoint ccpClamp(const CCPoint& p, const CCPoint& min_inclusive, const CCPoint& max_inclusive)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccp(clampf(p.x,min_inclusive.x,max_inclusive.x), clampf(p.y, min_inclusive.y, max_inclusive.y));
|
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
CCPoint ccpFromSize(const CCSize& s)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccp(s.width, s.height);
|
|
|
|
}
|
|
|
|
|
2011-07-05 12:05:19 +08:00
|
|
|
CCPoint ccpCompOp(const CCPoint& p, float (*opFunc)(float))
|
|
|
|
{
|
2010-07-16 14:54:26 +08:00
|
|
|
return ccp(opFunc(p.x), opFunc(p.y));
|
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
bool ccpFuzzyEqual(const CCPoint& a, const CCPoint& b, float var)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
if(a.x - var <= b.x && b.x <= a.x + var)
|
|
|
|
if(a.y - var <= b.y && b.y <= a.y + var)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
CCPoint ccpCompMult(const CCPoint& a, const CCPoint& b)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
return ccp(a.x * b.x, a.y * b.y);
|
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
float ccpAngleSigned(const CCPoint& a, const CCPoint& b)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
2011-07-05 12:05:19 +08:00
|
|
|
CCPoint a2 = ccpNormalize(a);
|
|
|
|
CCPoint b2 = ccpNormalize(b);
|
2010-07-16 14:54:26 +08:00
|
|
|
float angle = atan2f(a2.x * b2.y - a2.y * b2.x, ccpDot(a2, b2));
|
2011-03-07 17:11:57 +08:00
|
|
|
if( fabs(angle) < kCCPointEpsilon ) return 0.f;
|
2010-07-16 14:54:26 +08:00
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2011-07-05 12:05:19 +08:00
|
|
|
CCPoint ccpRotateByAngle(const CCPoint& v, const CCPoint& pivot, float angle)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint r = ccpSub(v, pivot);
|
2011-07-05 12:05:19 +08:00
|
|
|
float cosa = cosf(angle), sina = sinf(angle);
|
2010-07-16 14:54:26 +08:00
|
|
|
float t = r.x;
|
2011-11-26 17:07:36 +08:00
|
|
|
r.x = t*cosa - r.y*sina + pivot.x;
|
2011-07-05 12:05:19 +08:00
|
|
|
r.y = t*sina + r.y*cosa + pivot.y;
|
2010-07-16 14:54:26 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-11-26 17:07:36 +08:00
|
|
|
|
|
|
|
bool ccpSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D)
|
|
|
|
{
|
|
|
|
float S, T;
|
|
|
|
|
|
|
|
if( ccpLineIntersect(A, B, C, D, &S, &T )
|
|
|
|
&& (S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPoint ccpIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D)
|
|
|
|
{
|
|
|
|
float S, T;
|
|
|
|
|
|
|
|
if( ccpLineIntersect(A, B, C, D, &S, &T) )
|
|
|
|
{
|
|
|
|
// Point of intersection
|
|
|
|
CCPoint P;
|
|
|
|
P.x = A.x + S * (B.x - A.x);
|
|
|
|
P.y = A.y + S * (B.y - A.y);
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CCPointZero;
|
2011-07-08 14:16:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ccpLineIntersect(const CCPoint& A, const CCPoint& B,
|
|
|
|
const CCPoint& C, const CCPoint& D,
|
2011-07-05 12:05:19 +08:00
|
|
|
float *S, float *T)
|
|
|
|
{
|
2011-11-26 17:07:36 +08:00
|
|
|
// FAIL: Line undefined
|
|
|
|
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const float BAx = B.x - A.x;
|
|
|
|
const float BAy = B.y - A.y;
|
|
|
|
const float DCx = D.x - C.x;
|
|
|
|
const float DCy = D.y - C.y;
|
|
|
|
const float ACx = A.x - C.x;
|
|
|
|
const float ACy = A.y - C.y;
|
|
|
|
|
|
|
|
const float denom = DCy*BAx - DCx*BAy;
|
|
|
|
|
|
|
|
*S = DCx*ACy - DCy*ACx;
|
|
|
|
*T = BAx*ACy - BAy*ACx;
|
|
|
|
|
|
|
|
if (denom == 0)
|
|
|
|
{
|
|
|
|
if (*S == 0 || *T == 0)
|
|
|
|
{
|
|
|
|
// Lines incident
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Lines parallel and not incident
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*S = *S / denom;
|
|
|
|
*T = *T / denom;
|
|
|
|
|
|
|
|
// Point of intersection
|
|
|
|
// CGPoint P;
|
|
|
|
// P.x = A.x + *S * (B.x - A.x);
|
|
|
|
// P.y = A.y + *S * (B.y - A.y);
|
|
|
|
|
2011-07-05 12:05:19 +08:00
|
|
|
return true;
|
2010-07-16 14:54:26 +08:00
|
|
|
}
|
|
|
|
|
2011-04-16 14:55:02 +08:00
|
|
|
float ccpAngle(const CCPoint& a, const CCPoint& b)
|
2010-07-16 14:54:26 +08:00
|
|
|
{
|
|
|
|
float angle = acosf(ccpDot(ccpNormalize(a), ccpNormalize(b)));
|
2011-03-07 17:11:57 +08:00
|
|
|
if( fabs(angle) < kCCPointEpsilon ) return 0.f;
|
2010-07-16 14:54:26 +08:00
|
|
|
return angle;
|
2010-08-04 15:46:12 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_END
|