cocos2dx 源码学习6 CCGeometry

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

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

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

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