cocos2dx 源码学习6 CCGeometry

前端之家收集整理的这篇文章主要介绍了cocos2dx 源码学习6 CCGeometry前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址:http://www.jb51.cc/article/p-yefdcykj-yd.html

  1. ///\cocos2d-x-3.0alpha0\cocos2dx\cocoa\CCGeometry.h@H_301_10@
  2. //这个类主要是讲点和点之间的关系、线和线之间的关系、点和坐标轴之间的关系,这个类涉及了许多数学的知识,另外有一个类似的类,参考(///cocos2d-x-3.0alpha0/cocos2dx/include/CCDeprecated.h)@H_301_10@
  3. @H_301_10@
  4. @H_301_10@
  5. #ifndef__CCGEMETRY_H__@H_301_10@
  6. #define__CCGEMETRY_H__@H_301_10@
  7. #include<math.h>@H_301_10@
  8. #include<functional>@H_301_10@
  9. #include"platform/CCPlatformMacros.h"@H_301_10@
  10. #include"CCObject.h"@H_301_10@
  11. #include"ccMacros.h"@H_301_10@
  12. NS_CC_BEGIN@H_301_10@
  13. /**Clamp的值在min_inclusive和max_inclusive之间.@H_301_10@
  14. @sincev0.99.1@H_301_10@
  15. */@H_301_10@
  16. @H_502_103@inlinefloatclampf(floatvalue,floatmin_inclusive,87); background-color:inherit; font-weight:bold">floatmax_inclusive)@H_301_10@
  17. {@H_301_10@
  18. if(min_inclusive>max_inclusive){@H_301_10@
  19. CC_SWAP(min_inclusive,max_inclusive,float);@H_301_10@
  20. }@H_301_10@
  21. returnvalue<min_inclusive?min_inclusive:value<max_inclusive?value:max_inclusive;@H_301_10@
  22. /**@H_301_10@
  23. *@addtogroupdata_structures@H_301_10@
  24. *@{@H_301_10@
  25. //点的赋值运算符和拷贝构造函数@H_301_10@
  26. classCC_DLLSize;@H_301_10@
  27. classCC_DLL CCPoint@H_301_10@
  28. public:@H_301_10@
  29. floatx;@H_301_10@
  30. floaty;@H_301_10@
  31. /**@H_301_10@
  32. *@jsNA@H_301_10@
  33. CCPoint();@H_301_10@
  34. CCPoint(floatx,87); background-color:inherit; font-weight:bold">floaty);@H_301_10@
  35. *@luaNA@H_301_10@
  36. */@H_301_10@
  37. CCPoint(constCCPoint&other);@H_301_10@
  38. *@jsNA@H_301_10@
  39. *@luaNA@H_301_10@
  40. explicitCCPoint(constCCSize&size);@H_301_10@
  41. CCPoint&operator=( CCPoint&operator=( CCPointoperator+(constCCPoint&right)const;@H_301_10@
  42. CCPointoperator-(const;@H_301_10@
  43. CCPointoperator-() CC Pointoperator*(floata) CCPointoperator/(voidsetPoint(boolequals(constCCPoint&target)/**@returns如果点fuzzyequality(模糊平等)表示某种程度的差异相等。.@H_301_10@
  44. @sincev2.1.4@H_301_10@
  45. boolfuzzyEquals(constCCPoint&target,87); background-color:inherit; font-weight:bold">floatvariance)/**计算点和origin(原点)之间的距离@H_301_10@
  46. @returnfloat@H_301_10@
  47. floatgetLength()const{@H_301_10@
  48. returnsqrtf(x*x+y*y);@H_301_10@
  49. };@H_301_10@
  50. /**计算一个点长度的平方(不调用sqrt())@H_301_10@
  51. floatgetLengthSq()returndot(*this);//x*x+y*y;@H_301_10@
  52. /**计算两点之间的距离的平方(不调用sqrt())@H_301_10@
  53. floatgetDistanceSq(constCCPoint&other)return(*this-other).getLengthSq();@H_301_10@
  54. /**计算两点之间的距离@H_301_10@
  55. floatgetDistance(this-other).getLength();@H_301_10@
  56. /**@returns此向量和x轴之间的角度,单位为弧度@H_301_10@
  57. @sincev2.1.4@H_301_10@
  58. floatgetAngle()const{@H_301_10@
  59. returnatan2f(y,x);@H_301_10@
  60. };@H_301_10@
  61. /**@returns两个矢量方向之间的角度,单位为弧度@H_301_10@
  62. floatgetAngle(/**计算两个点之间的乘积.@H_301_10@
  63. floatdot(returnx*other.x+y*other.y;@H_301_10@
  64. /**计算两个点之间的交叉乘积@H_301_10@
  65. floatcross(returnx*other.y-y*other.x;@H_301_10@
  66. /**计算这个点关于x轴的对称点(Point(-y,x))@H_301_10@
  67. @returnPoint@H_301_10@
  68. inlineCCPointgetPerp()returnCCPoint(-y,x);@H_301_10@
  69. /**计算两点之间的中点.@H_301_10@
  70. @sincev3.0@H_301_10@
  71. inlineCCPointgetMidpoint(const@H_301_10@
  72. {@H_301_10@
  73. returnCCPoint((x+other.x)/2.0f,(y+other.y)/2.0f);@H_301_10@
  74. /**Clamp的点在min_inclusive和max_inclusive之间.@H_301_10@
  75. inlineCCPointgetClampPoint(constCCPoint&min_inclusive,constCCPoint&max_inclusive)returnCCPoint(clampf(x,min_inclusive.x,max_inclusive.x),clampf(y,min_inclusive.y,max_inclusive.y));@H_301_10@
  76. /**运行每个点的数学数学运算功能@H_301_10@
  77. *absf,fllorf,ceilf,roundf@H_301_10@
  78. *任何功能签名:floatfunc(float);@H_301_10@
  79. *Forexample:我们尝试获取floor的x,y@H_301_10@
  80. *p.compOp(floorf);@H_301_10@
  81. inlineCCPointcompOp(std::function<float(float)>function)returnCCPoint(function(x),function(y));@H_301_10@
  82. /**计算这个点关于y轴的对称点(Point(y,-x))@H_301_10@
  83. @returnPoint@H_301_10@
  84. inlineCCPointgetRPerp()returnCCPoint(y,-x);@H_301_10@
  85. /**计算这个点与其他点的投影@H_301_10@
  86. inlineCCPointproject(returnother*(dot(other)/other.dot(other));@H_301_10@
  87. /**两个点的复合乘法运算("rotates"twopoints).//旋转@H_301_10@
  88. @return点向量与一个角度this.getAngle()+other.getAngle(),@H_301_10@
  89. andalengthofthis.getLength()*other.getLength().@H_301_10@
  90. inlineCCPointrotate(returnCCPoint(x*other.x-y*other.y,x*other.y+y*other.x);@H_301_10@
  91. /**Unrotates(不旋转)两个点.//rotate(constPoint&other)的逆运算@H_301_10@
  92. @return点向量与一个角度this.getAngle()-other.getAngle(),@H_301_10@
  93. andalengthofthis.getLength()*other.getLength().@H_301_10@
  94. inlineCCPointunrotate(returnCCPoint(x*other.x+y*other.y,y*other.x-x*other.y);@H_301_10@
  95. /**Returns点相乘的长度是1.(返回该点的倒数)@H_301_10@
  96. *如果这个点是0,她会返回(1,0)@H_301_10@
  97. inlineCCPointnormalize()floatlength=getLength();@H_301_10@
  98. if(length==0.)returnPoint(1.f,0);@H_301_10@
  99. return*this/getLength();@H_301_10@
  100. /**a和b两点之间的线性插值(关于线性差值可以参考http://zh.wikipedia.org/zh-cn/线性插值‎)@H_301_10@
  101. @returns@H_301_10@
  102. alpha==0?a@H_301_10@
  103. alpha==1?b@H_301_10@
  104. otherwiseavaluebetweena..b@H_301_10@
  105. inlineCCPointlerp(constCCPoint&other,87); background-color:inherit; font-weight:bold">floatalpha)this*(1.f-alpha)+other*alpha;@H_301_10@
  106. /**一个点围绕轴心逆时针旋转的角度@H_301_10@
  107. @parampivot支点(类似自然界的支点)@H_301_10@
  108. @paramangle逆时针旋转的角度,以弧度为单位@H_301_10@
  109. @returns旋转后的点@H_301_10@
  110. CCPointrotateByAngle(constCCPoint&pivot,87); background-color:inherit; font-weight:bold">floatangle)staticinlineCCPointforAngle(constfloata)@H_301_10@
  111. returnCCPoint(cosf(a),sinf(a));@H_301_10@
  112. }@H_301_10@
  113. /**一个一般的线线相交测试@H_301_10@
  114. @paramAthestartpointforthefirstlineL1=(A-B)//第一条线的@H_301_10@
  115. @paramBtheendpointforthefirstlineL1=(A-B)//第一条线的@H_301_10@
  116. @paramCthestartpointforthesecondlineL2=(C-D)//第二条线的@H_301_10@
  117. @paramDtheendpointforthesecondlineL2=(C-D)//第二条线的@H_301_10@
  118. @paramStherangeforahitpointinL1(p=A+S*(B-A))//生命值的范围为@H_301_10@
  119. @paramTtherangeforahitpointinL2(p=C+T*(D-C))//生命值的范围为@H_301_10@
  120. @returnswhetherthesetwolinesinterects.@H_301_10@
  121. @H_301_10@
  122. 需要注意的是,真正测试交叉点的片段,我们必须确保S&T在射线[0..1]内确保S&T>0@H_301_10@
  123. 命中点是C+T*(D-C);@H_301_10@
  124. 命中点也是A+S*(B-A);@H_301_10@
  125. @since3.0@H_301_10@
  126. staticboolisLineIntersect(constCCPoint&A,153); background-color:inherit; font-weight:bold">constCCPoint&B,153); background-color:inherit; font-weight:bold">constCCPoint&C,153); background-color:inherit; font-weight:bold">constCCPoint&D,87); background-color:inherit; font-weight:bold">float*S=nullptr,87); background-color:inherit; font-weight:bold">float*T=nullptr);@H_301_10@
  127. returnstrue如果A-B线和C-D重叠@H_301_10@
  128. boolisLineOverlap(constCCPoint&D);@H_301_10@
  129. returnstrue如果A-B和C-D段平行@H_301_10@
  130. @sincev3.0@H_301_10@
  131. boolisLineParallel(constCCPoint&D);@H_301_10@
  132. returnstrue如果A-B段和C-D段重叠@H_301_10@
  133. boolisSegmentOverlap( CCPoint*S=nullptr,CCPoint*E=nullptr);@H_301_10@
  134. returnstrue如果A-B段和C-D段相交@H_301_10@
  135. boolisSegmentIntersect(returnsA-B,C-D线的交点@H_301_10@
  136. staticCCPointgetIntersectPoint(constCCPointZERO;@H_301_10@
  137. private:@H_301_10@
  138. //returnstrue如果段A,B与C-D段相交.S->E是重叠的一部分@H_301_10@
  139. boolisOneDemensionSegmentOverlap(floatA,87); background-color:inherit; font-weight:bold">floatB,87); background-color:inherit; font-weight:bold">floatC,87); background-color:inherit; font-weight:bold">floatD,87); background-color:inherit; font-weight:bold">float*S,87); background-color:inherit; font-weight:bold">float*E);@H_301_10@
  140. //两个向量的交叉替代产品.A->BXC->D@H_301_10@
  141. floatcrossProduct2Vector(constCCPoint&D){return(D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);}@H_301_10@
  142. classCC_DLL CCSize@H_301_10@
  143. public:@H_301_10@
  144. floatwidth;@H_301_10@
  145. floatheight;@H_301_10@
  146. CCSize();@H_301_10@
  147. CCSize(floatwidth,87); background-color:inherit; font-weight:bold">floatheight);@H_301_10@
  148. CCSize(constCCSize&other);@H_301_10@
  149. explicitCCSize(constCCPoint&point);@H_301_10@
  150. CCSize&operator=( CCSize&operator=( CCSizeoperator+(constCCSize&right) CCSizeoperator-( CCSizeoperator*( CCSizeoperator/(voidsetSize(floatheight);@H_301_10@
  151. constCCSize&target)constCCSizeZERO;@H_301_10@
  152. classCC_DLL CCRect@H_301_10@
  153. CCPointorigin;@H_301_10@
  154. CCSizesize;@H_301_10@
  155. CCRect();@H_301_10@
  156. CCRect(floaty,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCRect(constCCRect&other);@H_301_10@
  157. CCRect&operator=(constCCRect&other);@H_301_10@
  158. voidsetRect(floatgetMinX()const;///return当前矩形最左边的x值@H_301_10@
  159. floatgetMidX()///return当前矩形最中点的x值@H_301_10@
  160. floatgetMaxX()///return当前矩形最右边的x值@H_301_10@
  161. floatgetMinY()///return当前矩形最下边的y值@H_301_10@
  162. floatgetMidY()///return当前矩形最中点的y值@H_301_10@
  163. floatgetMaxY()///return当前矩形最上边的y值@H_301_10@
  164. constCCRect&rect)boolcontainsPoint(constCCPoint&point)boolintersectsRect( RectunionWithRect(constCCRectZERO;@H_301_10@
  165. //endofdata_structuregroup@H_301_10@
  166. ///@}@H_301_10@
  167. NS_CC_END@H_301_10@
  168. #endif//__CCGEMETRY_H__@H_301_10@

猜你在找的Cocos2d-x相关文章