您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x节点(CCGeometry.h)API

2013-12-06 20:52 357 查看
本文来自http://blog.csdn.net/runaying ,引用必须注明出处!


cocos2d-x节点(CCGeometry.h)API

温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记

这个类主要是讲点和点之间的关系、线和线之间的关系、点和坐标轴之间的关系,这个类涉及了许多数学的知识,另外有一个类似的类,参考(///cocos2d-x-3.0alpha0/cocos2dx/include/CCDeprecated.h)
///\cocos2d-x-3.0alpha0\cocos2dx\cocoa\CCGeometry.h
//这个类主要是讲点和点之间的关系、线和线之间的关系、点和坐标轴之间的关系,这个类涉及了许多数学的知识,另外有一个类似的类,参考(///cocos2d-x-3.0alpha0/cocos2dx/include/CCDeprecated.h)

#ifndef __CCGEMETRY_H__
#define __CCGEMETRY_H__

#include <math.h>
#include <functional>

#include "platform/CCPlatformMacros.h"
#include "CCObject.h"
#include "ccMacros.h"

NS_CC_BEGIN

/** Clamp 的值在 min_inclusive 和 max_inclusive 之间 .
@since v0.99.1
*/
inline float clampf(float value, float min_inclusive, float max_inclusive)
{
if (min_inclusive > max_inclusive) {
CC_SWAP(min_inclusive, max_inclusive, float);
}
return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}

/**
* @addtogroup data_structures
* @{
*/

// 点的赋值运算符和拷贝构造函数
class CC_DLL Size;

class CC_DLL Point
{
public:
float x;
float y;

public:
/**
* @js NA
*/
Point();
/**
* @js NA
*/
Point(float x, float y);
/**
* @js NA
* @lua NA
*/
Point(const Point& other);
/**
* @js NA
* @lua NA
*/
explicit Point(const Size& size);
/**
* @js NA
* @lua NA
*/
Point& operator= (const Point& other);
/**
* @js NA
* @lua NA
*/
Point& operator= (const Size& size);
/**
* @js NA
* @lua NA
*/
Point operator+(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point operator-(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point operator-() const;
/**
* @js NA
* @lua NA
*/
Point operator*(float a) const;
/**
* @js NA
* @lua NA
*/
Point operator/(float a) const;
/**
* @js NA
* @lua NA
*/
void setPoint(float x, float y);
/**
* @js NA
*/
bool equals(const Point& target) const;

/** @returns 如果点 fuzzy equality(模糊平等)表示某种程度的差异相等。.
@since v2.1.4
* @js NA
* @lua NA
*/
bool fuzzyEquals(const Point& target, float variance) const;

/**计算点和 origin(原点)之间的距离
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getLength() const {
return sqrtf(x*x + y*y);
};

/** 计算一个点长度的平方 (不调用 sqrt() )
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getLengthSq() const {
return dot(*this); //x*x + y*y;
};

/** 计算两点之间的距离的平方 (不调用 sqrt() )
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getDistanceSq(const Point& other) const {
return (*this - other).getLengthSq();
};

/**  计算两点之间的距离
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getDistance(const Point& other) const {
return (*this - other).getLength();
};

/** @returns此向量和x轴之间的角度,单位为弧度
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getAngle() const {
return atan2f(y, x);
};

/** @returns 两个矢量方向之间的角度,单位为弧度
@since v2.1.4
* @js NA
* @lua NA
*/
float getAngle(const Point& other) const;

/** 计算两个点之间的乘积.
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float dot(const Point& other) const {
return x*other.x + y*other.y;
};

/** 计算两个点之间的交叉乘积
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float cross(const Point& other) const {
return x*other.y - y*other.x;
};

/** 计算这个点关于 x 轴的对称点( Point(-y, x))
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point getPerp() const {
return Point(-y, x);
};

/** 计算两点之间的中点.
@return Point
@since v3.0
* @js NA
* @lua NA
*/
inline Point getMidpoint(const Point& other) const
{
return Point((x + other.x) / 2.0f, (y + other.y) / 2.0f);
}

/** Clamp 的点在 min_inclusive 和 max_inclusive 之间 .
@since v3.0
* @js NA
* @lua NA
*/
inline Point getClampPoint(const Point& min_inclusive, const Point& max_inclusive) const
{
return Point(clampf(x,min_inclusive.x,max_inclusive.x), clampf(y, min_inclusive.y, max_inclusive.y));
}

/** 运行每个点的数学数学运算功能
* absf, fllorf, ceilf, roundf
* 任何功能签名 : float func(float);
* For example: 我们尝试获取 floor 的 x,y
* p.compOp(floorf);
@since v3.0
* @js NA
* @lua NA
*/
inline Point compOp(std::function<float(float)> function) const
{
return Point(function(x), function(y));
}

/** 计算这个点关于 y 轴的对称点(Point(y, -x))
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point getRPerp() const {
return Point(y, -x);
};

/** 计算这个点与其他点的投影
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point project(const Point& other) const {
return other * (dot(other)/other.dot(other));
};

/**两个点的复合乘法运算 ("rotates" two points).//旋转
@return 点向量与一个角度 this.getAngle() + other.getAngle(),
and a length of this.getLength() * other.getLength().
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point rotate(const Point& other) const {
return Point(x*other.x - y*other.y, x*other.y + y*other.x);
};

/** Unrotates(不旋转) 两个点.   //rotate(const Point& other) 的逆运算
@return 点向量与一个角度 this.getAngle() - other.getAngle(),
and a length of this.getLength() * other.getLength().
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point unrotate(const Point& other) const {
return Point(x*other.x + y*other.y, y*other.x - x*other.y);
};

/** Returns 点相乘的长度是 1.(返回该点的倒数)
* 如果这个点是 0, 她会返回(1, 0)
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point normalize() const {
float length = getLength();
if(length == 0.) return Point(1.f, 0);
return *this / getLength();
};

/** a和b两点之间的线性插值(关于线性差值可以参考 http://zh.wikipedia.org/zh-cn/线性插值‎ )
@returns
alpha == 0 ? a
alpha == 1 ? b
otherwise a value between a..b
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point lerp(const Point& other, float alpha) const {
return *this * (1.f - alpha) + other * alpha;
};

/** 一个点围绕轴心逆时针旋转的角度
@param pivot 支点(类似自然界的支点)
@param angle 逆时针旋转的角度,以弧度为单位
@returns 旋转后的点
@since v2.1.4
* @js NA
* @lua NA
*/
Point rotateByAngle(const Point& pivot, float angle) const;

/**
* @js NA
* @lua NA
*/
static inline Point forAngle(const float a)
{
return Point(cosf(a), sinf(a));
}

/**一个一般的线线相交测试
@param A   the startpoint for the first line L1 = (A - B)  //第一条线的
@param B   the endpoint for the first line L1 = (A - B)    //第一条线的
@param C   the startpoint for the second line L2 = (C - D) //第二条线的
@param D   the endpoint for the second line L2 = (C - D)   //第二条线的
@param S   the range for a hitpoint in L1 (p = A + S*(B - A))  //生命值的范围为
@param T   the range for a hitpoint in L2 (p = C + T*(D - C))  //生命值的范围为
@returns   whether these two lines interects.

需要注意的是,真正测试交叉点的片段,我们必须确保 S & T 在射线 [0..1] 内 确保 S & T > 0
命中点是        C + T * (D - C);
命中点也是   A + S * (B - A);
@since 3.0
* @js NA
* @lua NA
*/
static bool isLineIntersect(const Point& A, const Point& B,
const Point& C, const Point& D,
float *S = nullptr, float *T = nullptr);

/**
returns true 如果 A-B 线和 C-D 重叠
@since v3.0
* @js NA
* @lua NA
*/
static bool isLineOverlap(const Point& A, const Point& B,
const Point& C, const Point& D);

/**
returns true 如果 A-B 和 C-D 段平行
@since v3.0
* @js NA
* @lua NA
*/
static bool isLineParallel(const Point& A, const Point& B,
const Point& C, const Point& D);

/**
returns true 如果 A-B 段和 C-D 段重叠
@since v3.0
* @js NA
* @lua NA
*/
static bool isSegmentOverlap(const Point& A, const Point& B,
const Point& C, const Point& D,
Point* S = nullptr, Point* E = nullptr);

/**
returns true 如果 A-B 段和 C-D 段相交
@since v3.0
* @js NA
* @lua NA
*/
static bool isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D);

/**
returns A-B,C-D线的交点
@since v3.0
* @js NA
* @lua NA
*/
static Point getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D);

static const Point ZERO;

private:
// returns true 如果段A,B与C-D段相交. S->E 是重叠的一部分
static bool isOneDemensionSegmentOverlap(float A, float B, float C, float D, float *S, float * E);

// 两个向量的交叉替代产品. A->B X C->D
static float crossProduct2Vector(const Point& A, const Point& B, const Point& C, const Point& D) { return (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); }
};

class CC_DLL Size
{
public:
float width;
float height;

public:
/**
* @js NA
*/
Size();
/**
* @js NA
*/
Size(float width, float height);
/**
* @js NA
* @lua NA
*/
Size(const Size& other);
/**
* @js NA
* @lua NA
*/
explicit Size(const Point& point);
/**
* @js NA
* @lua NA
*/
Size& operator= (const Size& other);
/**
* @js NA
* @lua NA
*/
Size& operator= (const Point& point);
/**
* @js NA
* @lua NA
*/
Size operator+(const Size& right) const;
/**
* @js NA
* @lua NA
*/
Size operator-(const Size& right) const;
/**
* @js NA
* @lua NA
*/
Size operator*(float a) const;
/**
* @js NA
* @lua NA
*/
Size operator/(float a) const;
/**
* @js NA
* @lua NA
*/
void setSize(float width, float height);
/**
* @js NA
*/
bool equals(const Size& target) const;

static const Size ZERO;
};

class CC_DLL Rect
{
public:
Point origin;
Size  size;

public:
/**
* @js NA
*/
Rect();
/**
* @js NA
*/
Rect(float x, float y, float width, float height);
/**
* @js NA
* @lua NA
*/
Rect(const Rect& other);
/**
* @js NA
* @lua NA
*/
Rect& operator= (const Rect& other);
/**
* @js NA
* @lua NA
*/
void setRect(float x, float y, float width, float height);
/**
* @js NA
*/
float getMinX() const; /// return 当前矩形最左边的x值
/**
* @js NA
*/
float getMidX() const; /// return 当前矩形最中点的x值
/**
* @js NA
*/
float getMaxX() const; /// return 当前矩形最右边的x值
/**
* @js NA
*/
float getMinY() const; /// return 当前矩形最下边的y值
/**
* @js NA
*/
float getMidY() const; /// return 当前矩形最中点的y值
/**
* @js NA
*/
float getMaxY() const; /// return 当前矩形最上边的y值
/**
* @js NA
*/
bool equals(const Rect& rect) const;
/**
* @js NA
*/
bool containsPoint(const Point& point) const;
/**
* @js NA
*/
bool intersectsRect(const Rect& rect) const;
/**
* @js NA
* @lua NA
*/
Rect unionWithRect(const Rect & rect) const;

static const Rect ZERO;
};

// end of data_structure group
/// @}

NS_CC_END

#endif // __CCGEMETRY_H__
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: