Moved most CCPointExtension functions to CCPoint OO-equivalents

This commit is contained in:
Adrien Béraud 2013-05-04 17:18:22 +10:00
parent b34dc25346
commit b9fe35901a
5 changed files with 116 additions and 39 deletions

View File

@ -66,6 +66,11 @@ CCPoint CCPoint::operator-(const CCPoint& right) const
return CCPoint(this->x - right.x, this->y - right.y);
}
CCPoint CCPoint::operator-() const
{
return CCPoint(-x, -y);
}
CCPoint CCPoint::operator*(float a) const
{
return CCPoint(this->x * a, this->y * a);
@ -88,6 +93,30 @@ bool CCPoint::equals(const CCPoint& target) const
&& (fabs(this->y - target.y) < FLT_EPSILON);
}
bool CCPoint::fuzzyEquals(const CCPoint& b, float var) const
{
if(x - var <= x && b.x <= x + var)
if(y - var <= b.y && b.y <= y + var)
return true;
return false;
}
float CCPoint::angle(const CCPoint& other) const
{
CCPoint a2 = normalize();
CCPoint b2 = other.normalize();
float angle = atan2f(a2.cross(b2), a2.dot(b2));
if( fabs(angle) < FLT_EPSILON ) return 0.f;
return angle;
}
CCPoint CCPoint::rotateByAngle(const CCPoint& pivot, float angle) const
{
CCPoint r = *this - pivot;
CCPoint a = CCPoint::forAngle(angle);
return pivot + CCPoint(r.x*a.x - r.y*a.y, r.x*a.y + r.y*a.x);
}
// implementation of CCSize
CCSize::CCSize(void) : width(0), height(0)

View File

@ -27,6 +27,7 @@ THE SOFTWARE.
#include "platform/CCPlatformMacros.h"
#include "CCObject.h"
#include <math.h>
NS_CC_BEGIN
@ -35,7 +36,7 @@ NS_CC_BEGIN
* @{
*/
// for CCPoint assignement operator
// for CCPoint assignement operator and copy constructor
class CC_DLL CCSize;
class CC_DLL CCPoint
@ -53,10 +54,69 @@ public:
CCPoint& operator= (const CCSize& size);
CCPoint operator+(const CCPoint& right) const;
CCPoint operator-(const CCPoint& right) const;
CCPoint operator-() const;
CCPoint operator*(float a) const;
CCPoint operator/(float a) const;
void setPoint(float x, float y);
bool equals(const CCPoint& target) const;
bool fuzzyEquals(const CCPoint& target, float variance) const;
inline float length() const {
return sqrtf(x*x + y*y);
};
inline float lengthSq() const {
return dot(*this); //x*x + y*y;
};
inline float angle() const {
return atan2f(y, x);
};
float angle(const CCPoint& other) const;
inline float dot(const CCPoint& other) const {
return x*other.x + y*other.y;
};
inline float cross(const CCPoint& other) const {
return x*other.y - y*other.x;
};
inline CCPoint perp() const {
return CCPoint(-y, x);
};
inline CCPoint rPerp() const {
return CCPoint(y, -x);
};
inline CCPoint project(const CCPoint& other) const {
return other * dot(other)/other.dot(other);
};
inline CCPoint rotate(const CCPoint& other) const {
return CCPoint(x*other.x - y*other.y, x*other.y + y*other.x);
};
inline CCPoint unrotate(const CCPoint& other) const {
return CCPoint(x*other.x + y*other.y, y*other.x - x*other.y);
};
inline CCPoint normalize() const {
return *this / length();
};
inline CCPoint lerp(const CCPoint& other, float alpha) const {
return *this * (1.f - alpha) + other * alpha;
};
CCPoint rotateByAngle(const CCPoint& pivot, float angle) const;
static inline CCPoint forAngle(const float a)
{
return CCPoint(cosf(a), sinf(a));
}
};
class CC_DLL CCSize

View File

@ -638,9 +638,9 @@ void CCSpriteBatchNode::removeSpriteFromAtlas(CCSprite *pobSprite)
m_pobDescendants->removeObjectAtIndex(uIndex);
// update all sprites beyond this one
unsigned int count = m_pobDescendants->count();
unsigned int count = uIndex;
for(; uIndex < count; ++uIndex)
for(uIndex = 0; uIndex < count; ++uIndex)
{
CCSprite* s = (CCSprite*)(m_pobDescendants->objectAtIndex(uIndex));
s->setAtlasIndex( s->getAtlasIndex() - 1 );

View File

@ -35,36 +35,36 @@ NS_CC_BEGIN
float
ccpLength(const CCPoint& v)
{
return sqrtf(ccpLengthSQ(v));
return v.length();
}
float
ccpDistance(const CCPoint& v1, const CCPoint& v2)
{
return ccpLength(ccpSub(v1, v2));
return (v1 - v2).length();
}
CCPoint
ccpNormalize(const CCPoint& v)
{
return ccpMult(v, 1.0f/ccpLength(v));
return v.normalize();
}
CCPoint
ccpForAngle(const float a)
{
return ccp(cosf(a), sinf(a));
return CCPoint::forAngle(a);
}
float
ccpToAngle(const CCPoint& v)
{
return atan2f(v.y, v.x);
return v.angle();
}
CCPoint ccpLerp(const CCPoint& a, const CCPoint& b, float alpha)
{
return ccpAdd(ccpMult(a, 1.f - alpha), ccpMult(b, alpha));
return a.lerp(b, alpha);
}
float clampf(float value, float min_inclusive, float max_inclusive)
@ -82,7 +82,7 @@ CCPoint ccpClamp(const CCPoint& p, const CCPoint& min_inclusive, const CCPoint&
CCPoint ccpFromSize(const CCSize& s)
{
return ccp(s.width, s.height);
return CCPoint(s);
}
CCPoint ccpCompOp(const CCPoint& p, float (*opFunc)(float))
@ -92,10 +92,7 @@ CCPoint ccpCompOp(const CCPoint& p, float (*opFunc)(float))
bool ccpFuzzyEqual(const CCPoint& a, const CCPoint& b, float var)
{
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;
return a.fuzzyEquals(b, var);
}
CCPoint ccpCompMult(const CCPoint& a, const CCPoint& b)
@ -105,21 +102,12 @@ CCPoint ccpCompMult(const CCPoint& a, const CCPoint& b)
float ccpAngleSigned(const CCPoint& a, const CCPoint& b)
{
CCPoint a2 = ccpNormalize(a);
CCPoint b2 = ccpNormalize(b);
float angle = atan2f(a2.x * b2.y - a2.y * b2.x, ccpDot(a2, b2));
if( fabs(angle) < kCCPointEpsilon ) return 0.f;
return angle;
return a.angle(b);
}
CCPoint ccpRotateByAngle(const CCPoint& v, const CCPoint& pivot, float angle)
{
CCPoint r = ccpSub(v, pivot);
float cosa = cosf(angle), sina = sinf(angle);
float t = r.x;
r.x = t*cosa - r.y*sina + pivot.x;
r.y = t*sina + r.y*cosa + pivot.y;
return r;
return v.rotateByAngle(pivot, angle);
}

View File

@ -66,7 +66,7 @@ NS_CC_BEGIN
static inline CCPoint
ccpNeg(const CCPoint& v)
{
return ccp(-v.x, -v.y);
return -v;
}
/** Calculates sum of two points.
@ -76,7 +76,7 @@ ccpNeg(const CCPoint& v)
static inline CCPoint
ccpAdd(const CCPoint& v1, const CCPoint& v2)
{
return ccp(v1.x + v2.x, v1.y + v2.y);
return v1 + v2;
}
/** Calculates difference of two points.
@ -86,7 +86,7 @@ ccpAdd(const CCPoint& v1, const CCPoint& v2)
static inline CCPoint
ccpSub(const CCPoint& v1, const CCPoint& v2)
{
return ccp(v1.x - v2.x, v1.y - v2.y);
return v1 - v2;
}
/** Returns point multiplied by given factor.
@ -96,7 +96,7 @@ ccpSub(const CCPoint& v1, const CCPoint& v2)
static inline CCPoint
ccpMult(const CCPoint& v, const float s)
{
return ccp(v.x*s, v.y*s);
return v * s;
}
/** Calculates midpoint between two points.
@ -106,7 +106,7 @@ ccpMult(const CCPoint& v, const float s)
static inline CCPoint
ccpMidpoint(const CCPoint& v1, const CCPoint& v2)
{
return ccpMult(ccpAdd(v1, v2), 0.5f);
return (v1 + v2) / 2.f;
}
/** Calculates dot product of two points.
@ -116,7 +116,7 @@ ccpMidpoint(const CCPoint& v1, const CCPoint& v2)
static inline float
ccpDot(const CCPoint& v1, const CCPoint& v2)
{
return v1.x*v2.x + v1.y*v2.y;
return v1.dot(v2);
}
/** Calculates cross product of two points.
@ -126,7 +126,7 @@ ccpDot(const CCPoint& v1, const CCPoint& v2)
static inline float
ccpCross(const CCPoint& v1, const CCPoint& v2)
{
return v1.x*v2.y - v1.y*v2.x;
return v1.cross(v2);
}
/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0
@ -136,7 +136,7 @@ ccpCross(const CCPoint& v1, const CCPoint& v2)
static inline CCPoint
ccpPerp(const CCPoint& v)
{
return ccp(-v.y, v.x);
return v.perp();
}
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0
@ -146,7 +146,7 @@ ccpPerp(const CCPoint& v)
static inline CCPoint
ccpRPerp(const CCPoint& v)
{
return ccp(v.y, -v.x);
return v.rPerp();
}
/** Calculates the projection of v1 over v2.
@ -156,7 +156,7 @@ ccpRPerp(const CCPoint& v)
static inline CCPoint
ccpProject(const CCPoint& v1, const CCPoint& v2)
{
return ccpMult(v2, ccpDot(v1, v2)/ccpDot(v2, v2));
return v1.project(v2);
}
/** Rotates two points.
@ -166,7 +166,7 @@ ccpProject(const CCPoint& v1, const CCPoint& v2)
static inline CCPoint
ccpRotate(const CCPoint& v1, const CCPoint& v2)
{
return ccp(v1.x*v2.x - v1.y*v2.y, v1.x*v2.y + v1.y*v2.x);
return v1.rotate(v2);
}
/** Unrotates two points.
@ -176,7 +176,7 @@ ccpRotate(const CCPoint& v1, const CCPoint& v2)
static inline CCPoint
ccpUnrotate(const CCPoint& v1, const CCPoint& v2)
{
return ccp(v1.x*v2.x + v1.y*v2.y, v1.y*v2.x - v1.x*v2.y);
return v1.unrotate(v2);
}
/** Calculates the square length of a CCPoint (not calling sqrt() )
@ -186,7 +186,7 @@ ccpUnrotate(const CCPoint& v1, const CCPoint& v2)
static inline float
ccpLengthSQ(const CCPoint& v)
{
return ccpDot(v, v);
return v.lengthSq();
}
@ -197,7 +197,7 @@ ccpLengthSQ(const CCPoint& v)
static inline float
ccpDistanceSQ(const CCPoint p1, const CCPoint p2)
{
return ccpLengthSQ(ccpSub(p1, p2));
return (p1 - p2).lengthSq();
}