Mathematical explanation of Cocos2d macros
The following text is reproduced from: Success.
/** Returns opposite of point.
@ Return CGPoint
@ Since v0.7.2
*/
Static inline CGPoint
CcpNeg (const CGPoint v) // calculate the symmetric point of the Origin
{
Return ccp (-v. x,-v. y );
}
/** Calculates sum of two points.
@ Return CGPoint
@ Since v0.7.2
*/
Static inline CGPoint
CcpAdd (const CGPoint v1, const CGPoint v2) // calculate the sum of the two vectors
{
Return CCP (v1.x + v2.x, v1.y + v2.y );
}
/** Calculates difference of two points.
@ Return cgpoint
@ Since v0.7.2
*/
Static inline cgpoint
Ccpsub (const cgpoint V1, const cgpoint V2) // calculate the difference between two vectors
{
Return CCP (v1.x-v2.x, v1.y-v2.y );
}
/** Returns point multiplied by given factor.
@ Return cgpoint
@ Since v0.7.2
*/
Static inline cgpoint
Ccpmult (const cgpoint V, const cgfloat s) // specify a factor to calculate a multiple of the vectors.
{
Return CCP (V. x * s, V. y * s );
}
/** Calculates midpoint between two points.
@ Return cgpoint
@ Since v0.7.2
*/
Static inline cgpoint
Ccpmidpoint (const cgpoint V1, const cgpoint V2) // calculate the centers of two points
{
Return ccpmult (ccpadd (V1, V2), 0.5f );
}
/** Calculates dot product of two points.
@ Return cgfloat
@ Since v0.7.2
*/
Static inline cgfloat
Ccpdot (const cgpoint V1, const cgpoint V2) // calculate the dot product of two vectors
{
Return v1.x * v2.x + v1.y * v2.y;
}
/** Calculates cross product of two points.
@ Return cgfloat
@ Since v0.7.2
*/
Static inline cgfloat
Ccpcross (const cgpoint V1, const cgpoint V2) // calculate the cross product of two vectors
{
Return v1.x * v2.y-v1.y * v2.x;
}
/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross (v, perp (v)> = 0
@ Return CGPoint
@ Since v0.7.2
*/
Static inline CGPoint
CcpPerp (const CGPoint v) // coordinate of the point after the vector is rotated counterclockwise
{
Return ccp (-v. y, v. x );
}
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross (v, rperp (v) <= 0
@ Return CGPoint
@ Since v0.7.2
*/
Static inline CGPoint
CcpRPerp (const CGPoint v) // coordinate of the point after the clockwise rotation of the Vector
{
Return ccp (v. y,-v. x );
}
/** Calculates the projection of v1 over v2.
@ Return CGPoint
@ Since v0.7.2
*/
Static inline cgpoint
Ccpproject (const cgpoint V1, const cgpoint V2) // calculate the projection point of vector V1 on vector v2
{
Return ccpmult (V2, ccpdot (V1, V2)/ccpdot (V2, V2 ));
}
/** Rotates two points.
@ Return cgpoint
@ Since v0.7.2
*/
Static inline cgpoint
Ccprotate (const cgpoint V1, const cgpoint V2)
{
Return CCP (v1.x * v2.x-v1.y * v2.y, v1.x * v2.y + v1.y * v2.x );
}
/** Unrotates two points.
@ Return cgpoint
@ Since v0.7.2
*/
Static inline cgpoint
Ccpunrotate (const cgpoint V1, const cgpoint V2)
{
Return CCP (v1.x * v2.x + v1.y * v2.y, v1.y * v2.x-v1.x * v2.y );
}
/** Calculates the square length of a CGPoint (not calling sqrt ())
@ Return CGFloat
@ Since v0.7.2
*/
Static inline CGFloat
CcpLengthSQ (const CGPoint v) // calculates the Square Value of the length of a vector.
{
Return ccpDot (v, v );
}
/** Calculates distance between point an origin
@ Return CGFloat
@ Since v0.7.2
*/
CGFloat ccpLength (const CGPoint v); // calculate the distance between the vertex and the origin, but do not know where the function body is
/** Calculates the distance between two points
@ Return CGFloat
@ Since v0.7.2
*/
CGFloat ccpDistance (const CGPoint v1, const CGPoint v2); // distance between two points
/** Returns point multiplied to a length of 1.
@ Return CGPoint
@ Since v0.7.2
*/
Cgpoint ccpnormalize (const cgpoint V );
/** Converts radians to a normalized vector.
@ Return cgpoint
@ Since v0.7.2
*/
Cgpoint ccpforangle (const cgfloat );
/** Converts a vector to radians.
@ Return cgfloat
@ Since v0.7.2
*/
Cgfloat ccptoangle (const cgpoint V );
/** Clamp a value between from and.
@ Since v0.99.1
*/
Float clampf (float value, float min_inclusive, float max_inclusive );
/** Clamp a point between from and.
@ Since v0.99.1
*/
Cgpoint ccpclamp (cgpoint P, cgpoint from, cgpoint );
/** Quickly convert cgsize to a cgpoint
@ Since v0.99.1
*/
Cgpoint ccpfromsize (cgsize S );
/** Run a math operation function on each point component
* Absf, fllorf, ceilf, roundf
* Any function that has the signature: Float func (float );
* For example: Let's try to take the floor of X, Y
* Ccpcompop (p, flodorf );
@ Since v0.99.1
*/
Cgpoint ccpcompop (cgpoint P, float (* opfunc) (float ));
/** Linear interpolation between two points A and B
@ Returns
Alpha = 0? A
Alpha = 1? B
Otherwise a value between a .. B
@ Since v0.99.1
*/
CGPoint ccpLerp (CGPoint a, CGPoint B, float alpha );
/** @ Returns if points have fuzzy equality ity which means equal with some degree of variance.
@ Since v0.99.1
*/
BOOL ccpFuzzyEqual (CGPoint a, CGPoint B, float variance );
/** Multiplies a nd B components, a. x * B. x, a. y * B. y
@ Returns a component-wise multiplication
@ Since v0.99.1
*/
CGPoint ccpCompMult (CGPoint a, CGPoint B );
/** @ Returns the signed angle in radians between two vector directions ctions
@ Since v0.99.1
*/
Float ccpAngleSigned (CGPoint a, CGPoint B );
/** @ Returns the angle in radians between two vector directions ctions
@ Since v0.99.1
*/
Float ccpAngle (CGPoint a, CGPoint B );
/** Rotates a point counter clockwise by the angle around a counter
@ Param v is the point to rotate
@ Param token is the token, naturally
@ Param angle is the angle of rotation cw in radians
@ Returns the rotated point
@ Since v0.99.1
*/
CGPoint ccpRotateByAngle (CGPoint v, CGPoint vertex, float angle );
/** A general line-line intersection test
@ Param p1
Is the startpoint for the first line P1 = (p1-p2)
@ Param p2
Is the endpoint for the first line P1 = (p1-p2)
@ Param p3
Is the startpoint for the second line P2 = (p3-p4)
@ Param p4
Is the endpoint for the second line P2 = (p3-p4)
@ Param s
Is the range for a hitpoint in P1 (pa = p1 + s * (p2-p1 ))
@ Param t
Is the range for a hitpoint in P3 (pa = p2 + t * (p4-p3 ))
@ Return bool
Indicating successful intersection of a line
Note that to truly test intersection for segments we have to make
Sure that s & t lie within [0 .. 1] and for rays, make sure s & t> 0
The hit point is p3 + t * (p4-p3 );
The hit point also is p1 + s * (p2-p1 );
@ Since v0.99.1
*/
BOOL ccpLineIntersect (CGPoint p1, CGPoint p2,
CGPoint p3, CGPoint p4,
Float * s, float * t );
/*
CcpSegmentIntersect returns YES if Segment A-B intersects with segment C-D
@ Since v1.0.0
*/
BOOL ccpSegmentIntersect (CGPoint A, CGPoint B, CGPoint C, CGPoint D );
/*
CcpIntersectPoint returns the intersection point of line A-B, C-D
@ Since v1.0.0
*/
CGPoint ccpIntersectPoint (CGPoint A, CGPoint B, CGPoint C, CGPoint D );