cocos2dx 源碼學習6 CCGeometry

原文地址:http://blog.csdn.net/runaying/article/details/16352661

  1. ///\cocos2d-x-3.0alpha0\cocos2dx\cocoa\CCGeometry.h  
  2. //這個類主要是講點和點之間的關係、線和線之間的關係、點和座標軸之間的關係,這個類涉及了許多數學的知識,另外有一個類似的類,參考(///cocos2d-x-3.0alpha0/cocos2dx/include/CCDeprecated.h)  
  3.   
  4.   
  5. #ifndef __CCGEMETRY_H__  
  6. #define __CCGEMETRY_H__  
  7.   
  8. #include <math.h>  
  9. #include <functional>  
  10.   
  11. #include "platform/CCPlatformMacros.h"  
  12. #include "CCObject.h"  
  13. #include "ccMacros.h"  
  14.   
  15. NS_CC_BEGIN  
  16.   
  17. /** Clamp 的值在 min_inclusive 和 max_inclusive 之間 . 
  18.  @since v0.99.1 
  19.  */  
  20. inline float clampf(float value, float min_inclusive, float max_inclusive)  
  21. {  
  22.     if (min_inclusive > max_inclusive) {  
  23.         CC_SWAP(min_inclusive, max_inclusive, float);  
  24.     }  
  25.     return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;  
  26. }  
  27.   
  28. /** 
  29.  * @addtogroup data_structures 
  30.  * @{ 
  31.  */  
  32.   
  33. // 點的賦值運算符和拷貝構造函數  
  34. class CC_DLL Size;  
  35.   
  36. class CC_DLL CCPoint  
  37. {  
  38. public:  
  39.     float x;  
  40.     float y;  
  41.       
  42. public:  
  43.     /** 
  44.      * @js NA 
  45.      */  
  46.     CCPoint();  
  47.     /** 
  48.      * @js NA 
  49.      */  
  50.     CCPoint(float x, float y);  
  51.     /** 
  52.      * @js NA 
  53.      * @lua NA 
  54.      */  
  55.     CCPoint(const CCPoint& other);  
  56.     /** 
  57.      * @js NA 
  58.      * @lua NA 
  59.      */  
  60.     explicit CCPoint(const CCSize& size);  
  61.     /** 
  62.      * @js NA 
  63.      * @lua NA 
  64.      */  
  65.     CCPoint& operator= (const CCPoint& other);  
  66.     /** 
  67.      * @js NA 
  68.      * @lua NA 
  69.      */  
  70.     CCPoint& operator= (const CCSize& size);  
  71.     /** 
  72.      * @js NA 
  73.      * @lua NA 
  74.      */  
  75.     CCPoint operator+(const CCPoint& right) const;  
  76.     /** 
  77.      * @js NA 
  78.      * @lua NA 
  79.      */  
  80.     CCPoint operator-(const CCPoint& right) const;  
  81.     /** 
  82.      * @js NA 
  83.      * @lua NA 
  84.      */  
  85.     CCPoint operator-() const;  
  86.     /** 
  87.      * @js NA 
  88.      * @lua NA 
  89.      */  
  90.    CC Point operator*(float a) const;  
  91.     /** 
  92.      * @js NA 
  93.      * @lua NA 
  94.      */  
  95.     CCPoint operator/(float a) const;  
  96.     /** 
  97.      * @js NA 
  98.      * @lua NA 
  99.      */  
  100.     void setPoint(float x, float y);  
  101.     /** 
  102.      * @js NA 
  103.      */  
  104.     bool equals(const CCPoint& target) const;  
  105.       
  106.     /** @returns 如果點 fuzzy equality(模糊平等)表示某種程度的差異相等。. 
  107.      @since v2.1.4 
  108.      * @js NA 
  109.      * @lua NA 
  110.      */  
  111.     bool fuzzyEquals(const CCPoint& target, float variance) const;  
  112.       
  113.     /**計算點和 origin(原點)之間的距離 
  114.      @return float 
  115.      @since v2.1.4 
  116.      * @js NA 
  117.      * @lua NA 
  118.      */  
  119.     inline float getLength() const {  
  120.         return sqrtf(x*x + y*y);  
  121.     };  
  122.       
  123.     /** 計算一個點長度的平方 (不調用 sqrt() ) 
  124.      @return float 
  125.      @since v2.1.4 
  126.      * @js NA 
  127.      * @lua NA 
  128.      */  
  129.     inline float getLengthSq() const {  
  130.         return dot(*this); //x*x + y*y;  
  131.     };  
  132.       
  133.     /** 計算兩點之間的距離的平方 (不調用 sqrt() ) 
  134.      @return float 
  135.      @since v2.1.4 
  136.      * @js NA 
  137.      * @lua NA 
  138.      */  
  139.     inline float getDistanceSq(const CCPoint& other) const {  
  140.         return (*this - other).getLengthSq();  
  141.     };  
  142.       
  143.     /**  計算兩點之間的距離 
  144.      @return float 
  145.      @since v2.1.4 
  146.      * @js NA 
  147.      * @lua NA 
  148.      */  
  149.     inline float getDistance(const CCPoint& other) const {  
  150.         return (*this - other).getLength();  
  151.     };  
  152.       
  153.     /** @returns此向量和x軸之間的角度,單位爲弧度 
  154.      @since v2.1.4 
  155.      * @js NA 
  156.      * @lua NA 
  157.      */  
  158.     inline float getAngle() const {  
  159.         return atan2f(y, x);  
  160.     };  
  161.       
  162.     /** @returns 兩個矢量方向之間的角度,單位爲弧度 
  163.      @since v2.1.4 
  164.      * @js NA 
  165.      * @lua NA 
  166.      */  
  167.     float getAngle(const CCPoint& other) const;  
  168.       
  169.     /** 計算兩個點之間的乘積. 
  170.      @return float 
  171.      @since v2.1.4 
  172.      * @js NA 
  173.      * @lua NA 
  174.      */  
  175.     inline float dot(const CCPoint& other) const {  
  176.         return x*other.x + y*other.y;  
  177.     };  
  178.       
  179.     /** 計算兩個點之間的交叉乘積 
  180.      @return float 
  181.      @since v2.1.4 
  182.      * @js NA 
  183.      * @lua NA 
  184.      */  
  185.     inline float cross(const CCPoint& other) const {  
  186.         return x*other.y - y*other.x;  
  187.     };  
  188.       
  189.     /** 計算這個點關於 x 軸的對稱點( Point(-y, x)) 
  190.      @return Point 
  191.      @since v2.1.4 
  192.      * @js NA 
  193.      * @lua NA 
  194.      */  
  195.     inline CCPoint getPerp() const {  
  196.         return CCPoint(-y, x);  
  197.     };  
  198.       
  199.     /** 計算兩點之間的中點. 
  200.      @return Point 
  201.      @since v3.0 
  202.      * @js NA 
  203.      * @lua NA 
  204.      */  
  205.     inline CCPoint getMidpoint(const CCPoint& other) const  
  206.     {  
  207.         return CCPoint((x + other.x) / 2.0f, (y + other.y) / 2.0f);  
  208.     }  
  209.       
  210.     /** Clamp 的點在 min_inclusive 和 max_inclusive 之間 . 
  211.      @since v3.0 
  212.      * @js NA 
  213.      * @lua NA 
  214.      */  
  215.     inline CCPoint getClampPoint(const CCPoint& min_inclusive, const CCPoint& max_inclusive) const  
  216.     {  
  217.         return CCPoint(clampf(x,min_inclusive.x,max_inclusive.x), clampf(y, min_inclusive.y, max_inclusive.y));  
  218.     }  
  219.       
  220.     /** 運行每個點的數學數學運算功能 
  221.      * absf, fllorf, ceilf, roundf 
  222.      * 任何功能簽名 : float func(float); 
  223.      * For example: 我們嘗試獲取 floor 的 x,y 
  224.      * p.compOp(floorf); 
  225.      @since v3.0 
  226.      * @js NA 
  227.      * @lua NA 
  228.      */  
  229.     inline CCPoint compOp(std::function<float(float)> function) const  
  230.     {  
  231.         return CCPoint(function(x), function(y));  
  232.     }  
  233.       
  234.     /** 計算這個點關於 y 軸的對稱點(Point(y, -x)) 
  235.      @return Point 
  236.      @since v2.1.4 
  237.      * @js NA 
  238.      * @lua NA 
  239.      */  
  240.     inline CCPoint getRPerp() const {  
  241.         return CCPoint(y, -x);  
  242.     };  
  243.       
  244.     /** 計算這個點與其他點的投影 
  245.      @return Point 
  246.      @since v2.1.4 
  247.      * @js NA 
  248.      * @lua NA 
  249.      */  
  250.     inline CCPoint project(const CCPoint& other) const {  
  251.         return other * (dot(other)/other.dot(other));  
  252.     };  
  253.       
  254.     /**兩個點的複合乘法運算 ("rotates" two points).//旋轉 
  255.      @return 點向量與一個角度 this.getAngle() + other.getAngle(), 
  256.      and a length of this.getLength() * other.getLength(). 
  257.      @since v2.1.4 
  258.      * @js NA 
  259.      * @lua NA 
  260.      */  
  261.     inline CCPoint rotate(const CCPoint& other) const {  
  262.         return CCPoint(x*other.x - y*other.y, x*other.y + y*other.x);  
  263.     };  
  264.       
  265.     /** Unrotates(不旋轉) 兩個點.   //rotate(const Point& other) 的逆運算 
  266.      @return 點向量與一個角度 this.getAngle() - other.getAngle(), 
  267.      and a length of this.getLength() * other.getLength(). 
  268.      @since v2.1.4 
  269.      * @js NA 
  270.      * @lua NA 
  271.      */  
  272.     inline CCPoint unrotate(const CCPoint& other) const {  
  273.         return CCPoint(x*other.x + y*other.y, y*other.x - x*other.y);  
  274.     };  
  275.       
  276.     /** Returns 點相乘的長度是 1.(返回該點的倒數) 
  277.      * 如果這個點是 0, 她會返回(1, 0) 
  278.      @return Point 
  279.      @since v2.1.4 
  280.      * @js NA 
  281.      * @lua NA 
  282.      */  
  283.     inline CCPoint normalize() const {  
  284.         float length = getLength();  
  285.         if(length == 0.) return Point(1.f, 0);  
  286.         return *this / getLength();  
  287.     };  
  288.       
  289.     /** a和b兩點之間的線性插值(關於線性差值可以參考 http://zh.wikipedia.org/zh-cn/線性插值‎ ) 
  290.      @returns 
  291.      alpha == 0 ? a 
  292.      alpha == 1 ? b 
  293.      otherwise a value between a..b 
  294.      @since v2.1.4 
  295.      * @js NA 
  296.      * @lua NA 
  297.      */  
  298.     inline CCPoint lerp(const CCPoint& other, float alpha) const {  
  299.         return *this * (1.f - alpha) + other * alpha;  
  300.     };  
  301.       
  302.     /** 一個點圍繞軸心逆時針旋轉的角度 
  303.      @param pivot 支點(類似自然界的支點) 
  304.      @param angle 逆時針旋轉的角度,以弧度爲單位 
  305.      @returns 旋轉後的點 
  306.      @since v2.1.4 
  307.      * @js NA 
  308.      * @lua NA 
  309.      */  
  310.     CCPoint rotateByAngle(const CCPoint& pivot, float angle) const;  
  311.       
  312.     /** 
  313.      * @js NA 
  314.      * @lua NA 
  315.      */  
  316.     static inline CCPoint forAngle(const float a)  
  317.     {  
  318.         return CCPoint(cosf(a), sinf(a));  
  319.     }  
  320.       
  321.     /**一個一般的線線相交測試 
  322.      @param A   the startpoint for the first line L1 = (A - B)  //第一條線的 
  323.      @param B   the endpoint for the first line L1 = (A - B)    //第一條線的 
  324.      @param C   the startpoint for the second line L2 = (C - D) //第二條線的 
  325.      @param D   the endpoint for the second line L2 = (C - D)   //第二條線的 
  326.      @param S   the range for a hitpoint in L1 (p = A + S*(B - A))  //生命值的範圍爲 
  327.      @param T   the range for a hitpoint in L2 (p = C + T*(D - C))  //生命值的範圍爲 
  328.      @returns   whether these two lines interects. 
  329.       
  330.      需要注意的是,真正測試交叉點的片段,我們必須確保 S & T 在射線 [0..1] 內 確保 S & T > 0 
  331.      命中點是        C + T * (D - C); 
  332.      命中點也是   A + S * (B - A); 
  333.      @since 3.0 
  334.      * @js NA 
  335.      * @lua NA 
  336.      */  
  337.     static bool isLineIntersect(const CCPoint& A, const CCPoint& B,  
  338.                                 const CCPoint& C, const CCPoint& D,  
  339.                                 float *S = nullptr, float *T = nullptr);  
  340.       
  341.     /** 
  342.      returns true 如果 A-B 線和 C-D 重疊 
  343.      @since v3.0 
  344.      * @js NA 
  345.      * @lua NA 
  346.      */  
  347.     static bool isLineOverlap(const CCPoint& A, const CCPoint& B,  
  348.                               const CCPoint& C, const CCPoint& D);  
  349.       
  350.     /** 
  351.      returns true 如果 A-B 和 C-D 段平行 
  352.      @since v3.0 
  353.      * @js NA 
  354.      * @lua NA 
  355.      */  
  356.     static bool isLineParallel(const CCPoint& A, const CCPoint& B,  
  357.                                const CCPoint& C, const CCPoint& D);  
  358.       
  359.     /** 
  360.      returns true 如果 A-B 段和 C-D 段重疊 
  361.      @since v3.0 
  362.      * @js NA 
  363.      * @lua NA 
  364.      */  
  365.     static bool isSegmentOverlap(const CCPoint& A, const CCPoint& B,  
  366.                                  const CCPoint& C, const CCPoint& D,  
  367.                                  CCPoint* S = nullptr, CCPoint* E = nullptr);  
  368.       
  369.     /** 
  370.      returns true 如果 A-B 段和 C-D 段相交 
  371.      @since v3.0 
  372.      * @js NA 
  373.      * @lua NA 
  374.      */  
  375.     static bool isSegmentIntersect(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D);  
  376.       
  377.     /** 
  378.      returns A-B,C-D線的交點 
  379.      @since v3.0 
  380.      * @js NA 
  381.      * @lua NA 
  382.      */  
  383.     static CCPoint getIntersectPoint(const CCPoint& A, const CCPoint& B, const CCPoint& C, const CCPoint& D);  
  384.       
  385.     static const CCPoint ZERO;  
  386.       
  387. private:  
  388.     // returns true 如果段A,B與C-D段相交. S->E 是重疊的一部分  
  389.     static bool isOneDemensionSegmentOverlap(float A, float B, float C, float D, float *S, float * E);  
  390.       
  391.     // 兩個向量的交叉替代產品. A->B X C->D  
  392.     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); }  
  393. };  
  394.   
  395. class CC_DLL CCSize  
  396. {  
  397. public:  
  398.     float width;  
  399.     float height;  
  400.       
  401. public:  
  402.     /** 
  403.      * @js NA 
  404.      */  
  405.     CCSize();  
  406.     /** 
  407.      * @js NA 
  408.      */  
  409.     CCSize(float width, float height);  
  410.     /** 
  411.      * @js NA 
  412.      * @lua NA 
  413.      */  
  414.     CCSize(const CCSize& other);  
  415.     /** 
  416.      * @js NA 
  417.      * @lua NA 
  418.      */  
  419.     explicit CCSize(const CCPoint& point);  
  420.     /** 
  421.      * @js NA 
  422.      * @lua NA 
  423.      */  
  424.     CCSize& operator= (const CCSize& other);  
  425.     /** 
  426.      * @js NA 
  427.      * @lua NA 
  428.      */  
  429.     CCSize& operator= (const CCPoint& point);  
  430.     /** 
  431.      * @js NA 
  432.      * @lua NA 
  433.      */  
  434.     CCSize operator+(const CCSize& right) const;  
  435.     /** 
  436.      * @js NA 
  437.      * @lua NA 
  438.      */  
  439.     CCSize operator-(const CCSize& right) const;  
  440.     /** 
  441.      * @js NA 
  442.      * @lua NA 
  443.      */  
  444.     CCSize operator*(float a) const;  
  445.     /** 
  446.      * @js NA 
  447.      * @lua NA 
  448.      */  
  449.     CCSize operator/(float a) const;  
  450.     /** 
  451.      * @js NA 
  452.      * @lua NA 
  453.      */  
  454.     void setSize(float width, float height);  
  455.     /** 
  456.      * @js NA 
  457.      */  
  458.     bool equals(const CCSize& target) const;  
  459.       
  460.     static const CCSize ZERO;  
  461. };  
  462.   
  463. class CC_DLL CCRect  
  464. {  
  465. public:  
  466.     CCPoint origin;  
  467.     CCSize  size;  
  468.       
  469. public:  
  470.     /** 
  471.      * @js NA 
  472.      */  
  473.     CCRect();  
  474.     /** 
  475.      * @js NA 
  476.      */  
  477.     CCRect(float x, float y, float width, float height);  
  478.     /** 
  479.      * @js NA 
  480.      * @lua NA 
  481.      */  
  482.     CCRect(const CCRect& other);  
  483.     /** 
  484.      * @js NA 
  485.      * @lua NA 
  486.      */  
  487.     CCRect& operator= (const CCRect& other);  
  488.     /** 
  489.      * @js NA 
  490.      * @lua NA 
  491.      */  
  492.     void setRect(float x, float y, float width, float height);  
  493.     /** 
  494.      * @js NA 
  495.      */  
  496.     float getMinX() const/// return 當前矩形最左邊的x值  
  497.     /** 
  498.      * @js NA 
  499.      */  
  500.     float getMidX() const/// return 當前矩形最中點的x值  
  501.     /** 
  502.      * @js NA 
  503.      */  
  504.     float getMaxX() const/// return 當前矩形最右邊的x值  
  505.     /** 
  506.      * @js NA 
  507.      */  
  508.     float getMinY() const/// return 當前矩形最下邊的y值  
  509.     /** 
  510.      * @js NA 
  511.      */  
  512.     float getMidY() const/// return 當前矩形最中點的y值  
  513.     /** 
  514.      * @js NA 
  515.      */  
  516.     float getMaxY() const/// return 當前矩形最上邊的y值  
  517.     /** 
  518.      * @js NA 
  519.      */  
  520.     bool equals(const CCRect& rect) const;  
  521.     /** 
  522.      * @js NA 
  523.      */  
  524.     bool containsPoint(const CCPoint& point) const;  
  525.     /** 
  526.      * @js NA 
  527.      */  
  528.     bool intersectsRect(const CCRect& rect) const;  
  529.     /** 
  530.      * @js NA 
  531.      * @lua NA 
  532.      */  
  533.     Rect unionWithRect(const CCRect & rect) const;  
  534.       
  535.     static const CCRect ZERO;  
  536. };  
  537.   
  538. // end of data_structure group  
  539. /// @}  
  540.   
  541. NS_CC_END  
  542.   
  543. #endif // __CCGEMETRY_H__  

發佈了23 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章