原文地址: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__