mirror of https://github.com/axmolengine/axmol.git
fixed #588.
This commit is contained in:
parent
5db1719302
commit
db6a5fce23
|
@ -301,10 +301,22 @@ CCPoint CC_DLL ccpRotateByAngle(const CCPoint& v, const CCPoint& pivot, float an
|
|||
the hit point also is p1 + s * (p2 - p1);
|
||||
@since v0.99.1
|
||||
*/
|
||||
bool CC_DLL ccpLineIntersect(CCPoint p1, CCPoint p2,
|
||||
CCPoint p3, CCPoint p4,
|
||||
bool CC_DLL ccpLineIntersect(const CCPoint& p1, const CCPoint& p2,
|
||||
const CCPoint& p3, const CCPoint& p4,
|
||||
float *s, float *t);
|
||||
|
||||
/*
|
||||
ccpSegmentIntersect returns YES if Segment A-B intersects with segment C-D
|
||||
@since v1.0.0
|
||||
*/
|
||||
bool ccpSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D);
|
||||
|
||||
/*
|
||||
ccpIntersectPoint returns the intersection point of line A-B, C-D
|
||||
@since v1.0.0
|
||||
*/
|
||||
CCPoint ccpIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D);
|
||||
|
||||
}//namespace cocos2d
|
||||
|
||||
#endif // __SUPPORT_CGPOINTEXTENSION_H__
|
||||
|
|
|
@ -124,8 +124,36 @@ CCPoint ccpRotateByAngle(const CCPoint& v, const CCPoint& pivot, float angle)
|
|||
return r;
|
||||
}
|
||||
|
||||
bool ccpLineIntersect(CCPoint A, CCPoint B,
|
||||
CCPoint C, CCPoint D,
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool ccpLineIntersect(const CCPoint& A, const CCPoint& B,
|
||||
const CCPoint& C, const CCPoint& D,
|
||||
float *S, float *T)
|
||||
{
|
||||
// FAIL: Line undefined
|
||||
|
@ -133,51 +161,37 @@ bool ccpLineIntersect(CCPoint A, CCPoint B,
|
|||
{
|
||||
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;
|
||||
|
||||
// Translate system to make A the origin
|
||||
B.x-=A.x; B.y-=A.y;
|
||||
C.x-=A.x; C.y-=A.y;
|
||||
D.x-=A.x; D.y-=A.y;
|
||||
const float denom = DCy*BAx - DCx*BAy;
|
||||
|
||||
// Cache
|
||||
CCPoint C2 = C, D2 = D;
|
||||
*S = DCx*ACy - DCy*ACx;
|
||||
*T = BAx*ACy - BAy*ACx;
|
||||
|
||||
// Length of segment AB
|
||||
float distAB = sqrtf(B.x*B.x+B.y*B.y);
|
||||
|
||||
// Rotate the system so that point B is on the positive X axis.
|
||||
float theCos = B.x/distAB;
|
||||
float theSin = B.y/distAB;
|
||||
float newX = C.x*theCos+C.y*theSin;
|
||||
C.y = C.y*theCos-C.x*theSin; C.x = newX;
|
||||
newX = D.x*theCos+D.y*theSin;
|
||||
D.y = D.y*theCos-D.x*theSin; D.x = newX;
|
||||
|
||||
// FAIL: Lines are parallel.
|
||||
if (C.y == D.y)
|
||||
if (denom == 0)
|
||||
{
|
||||
if (*S == 0 || *T == 0)
|
||||
{
|
||||
// Lines incident
|
||||
return true;
|
||||
}
|
||||
// Lines parallel and not incident
|
||||
return false;
|
||||
}
|
||||
|
||||
// Discover position of the intersection in the line AB
|
||||
float ABpos = D.x+(C.x-D.x)*D.y/(D.y-C.y);
|
||||
*S = *S / denom;
|
||||
*T = *T / denom;
|
||||
|
||||
// Vector CD
|
||||
C.x = D2.x-C2.x;
|
||||
C.y = D2.y-C2.y;
|
||||
// Point of intersection
|
||||
// CGPoint P;
|
||||
// P.x = A.x + *S * (B.x - A.x);
|
||||
// P.y = A.y + *S * (B.y - A.y);
|
||||
|
||||
// Vector between intersection and point C
|
||||
A.x = ABpos*theCos-C2.x;
|
||||
A.y = ABpos*theSin-C2.y;
|
||||
|
||||
newX = sqrtf((A.x*A.x+A.y*A.y)/(C.x*C.x+C.y*C.y));
|
||||
if(((A.y<0) != (C.y<0)) || ((A.x<0) != (C.x<0)))
|
||||
newX *= -1.0f;
|
||||
|
||||
*S = ABpos/distAB;
|
||||
*T = newX;
|
||||
|
||||
// Success.
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue