原文地址:http://blog.csdn.net/runaying/article/details/16352661
- ///\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 CCPoint
- {
- public:
- float x;
- float y;
- public:
- /**
- * @js NA
- */
- CCPoint();
- /**
- * @js NA
- */
- CCPoint(float x, float y);
- /**
- * @js NA
- * @lua NA
- */
- CCPoint(const CCPoint& other);
- /**
- * @js NA
- * @lua NA
- */
- explicit CCPoint(const CCSize& size);
- /**
- * @js NA
- * @lua NA
- */
- CCPoint& operator= (const CCPoint& other);
- /**
- * @js NA
- * @lua NA
- */
- CCPoint& operator= (const CCSize& size);
- /**
- * @js NA
- * @lua NA
- */
- CCPoint operator+(const CCPoint& right) const;
- /**
- * @js NA
- * @lua NA
- */
- CCPoint operator-(const CCPoint& right) const;
- /**
- * @js NA
- * @lua NA
- */
- CCPoint operator-() const;
- /**
- * @js NA
- * @lua NA
- */
- CC Point operator*(float a) const;
- /**
- * @js NA
- * @lua NA
- */
- CCPoint operator/(float a) const;
- /**
- * @js NA
- * @lua NA
- */
- void setPoint(float x, float y);
- /**
- * @js NA
- */
- bool equals(const CCPoint& target) const;
- /** @returns 如果点 fuzzy equality(模糊平等)表示某种程度的差异相等。.
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- bool fuzzyEquals(const CCPoint& 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 CCPoint& other) const {
- return (*this - other).getLengthSq();
- };
- /** 计算两点之间的距离
- @return float
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- inline float getDistance(const CCPoint& 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 CCPoint& other) const;
- /** 计算两个点之间的乘积.
- @return float
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- inline float dot(const CCPoint& other) const {
- return x*other.x + y*other.y;
- };
- /** 计算两个点之间的交叉乘积
- @return float
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- inline float cross(const CCPoint& other) const {
- return x*other.y - y*other.x;
- };
- /** 计算这个点关于 x 轴的对称点( Point(-y, x))
- @return Point
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- inline CCPoint getPerp() const {
- return CCPoint(-y, x);
- };
- /** 计算两点之间的中点.
- @return Point
- @since v3.0
- * @js NA
- * @lua NA
- */
- inline CCPoint getMidpoint(const CCPoint& other) const
- {
- return CCPoint((x + other.x) / 2.0f, (y + other.y) / 2.0f);
- }
- /** Clamp 的点在 min_inclusive 和 max_inclusive 之间 .
- @since v3.0
- * @js NA
- * @lua NA
- */
- inline CCPoint getClampPoint(const CCPoint& min_inclusive, const CCPoint& max_inclusive) const
- {
- return CCPoint(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 CCPoint compOp(std::function<float(float)> function) const
- {
- return CCPoint(function(x), function(y));
- }
- /** 计算这个点关于 y 轴的对称点(Point(y, -x))
- @return Point
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- inline CCPoint getRPerp() const {
- return CCPoint(y, -x);
- };
- /** 计算这个点与其他点的投影
- @return Point
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- inline CCPoint project(const CCPoint& 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 CCPoint rotate(const CCPoint& other) const {
- return CCPoint(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 CCPoint unrotate(const CCPoint& other) const {
- return CCPoint(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 CCPoint 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 CCPoint lerp(const CCPoint& other, float alpha) const {
- return *this * (1.f - alpha) + other * alpha;
- };
- /** 一个点围绕轴心逆时针旋转的角度
- @param pivot 支点(类似自然界的支点)
- @param angle 逆时针旋转的角度,以弧度为单位
- @returns 旋转后的点
- @since v2.1.4
- * @js NA
- * @lua NA
- */
- CCPoint rotateByAngle(const CCPoint& pivot, float angle) const;
- /**
- * @js NA
- * @lua NA
- */
- static inline CCPoint forAngle(const float a)
- {
- return CCPoint(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 CCPoint& A, const CCPoint& B,
- const CCPoint& C, const CCPoint& D,
- float *S = nullptr, float *T = nullptr);
- /**
- returns true 如果 A-B 线和 C-D 重叠
- @since v3.0
- * @js NA
- * @lua NA
- */
- static bool isLineOverlap(const CCPoint& A, const CCPoint& B,
- const CCPoint& C, const CCPoint& D);
- /**
- returns true 如果 A-B 和 C-D 段平行
- @since v3.0
- * @js NA
- * @lua NA
- */
- static bool isLineParallel(const CCPoint& A, const CCPoint& B,
- const CCPoint& C, const CCPoint& D);
- /**
- returns true 如果 A-B 段和 C-D 段重叠
- @since v3.0
- * @js NA
- * @lua NA
- */
- static bool isSegmentOverlap(const CCPoint& A, const CCPoint& B,
- const CCPoint& C, const CCPoint& D,
- CCPoint* S = nullptr, CCPoint* E = nullptr);
- /**
- returns true 如果 A-B 段和 C-D 段相交
- @since v3.0
- * @js NA
- * @lua NA
- */
- static bool isSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D);
- /**
- returns A-B,C-D线的交点
- @since v3.0
- * @js NA
- * @lua NA
- */
- static CCPoint getIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D);
- static const CCPoint 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 CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D) { return (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); }
- };
- class CC_DLL CCSize
- {
- public:
- float width;
- float height;
- public:
- /**
- * @js NA
- */
- CCSize();
- /**
- * @js NA
- */
- CCSize(float width, float height);
- /**
- * @js NA
- * @lua NA
- */
- CCSize(const CCSize& other);
- /**
- * @js NA
- * @lua NA
- */
- explicit CCSize(const CCPoint& point);
- /**
- * @js NA
- * @lua NA
- */
- CCSize& operator= (const CCSize& other);
- /**
- * @js NA
- * @lua NA
- */
- CCSize& operator= (const CCPoint& point);
- /**
- * @js NA
- * @lua NA
- */
- CCSize operator+(const CCSize& right) const;
- /**
- * @js NA
- * @lua NA
- */
- CCSize operator-(const CCSize& right) const;
- /**
- * @js NA
- * @lua NA
- */
- CCSize operator*(float a) const;
- /**
- * @js NA
- * @lua NA
- */
- CCSize operator/(float a) const;
- /**
- * @js NA
- * @lua NA
- */
- void setSize(float width, float height);
- /**
- * @js NA
- */
- bool equals(const CCSize& target) const;
- static const CCSize ZERO;
- };
- class CC_DLL CCRect
- {
- public:
- CCPoint origin;
- CCSize size;
- public:
- /**
- * @js NA
- */
- CCRect();
- /**
- * @js NA
- */
- CCRect(float x, float y, float width, float height);
- /**
- * @js NA
- * @lua NA
- */
- CCRect(const CCRect& other);
- /**
- * @js NA
- * @lua NA
- */
- CCRect& operator= (const CCRect& 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 CCRect& rect) const;
- /**
- * @js NA
- */
- bool containsPoint(const CCPoint& point) const;
- /**
- * @js NA
- */
- bool intersectsRect(const CCRect& rect) const;
- /**
- * @js NA
- * @lua NA
- */
- Rect unionWithRect(const CCRect & rect) const;
- static const CCRect ZERO;
- };
- // end of data_structure group
- /// @}
- NS_CC_END
- #endif // __CCGEMETRY_H__