cocos2dx 源码学习6 CCGeometry

cocos2dx 源码学习6 CCGeometry,第1张

概述原文地址:http://www.voidcn.com/article/p-yefdcykj-yd.html ///\cocos2d-x-3.0alpha0\cocos2dx\cocoa\CCGeometry.h   //这个类主要是讲点和点之间的关系、线和线之间的关系、点和坐标轴之间的关系,这个类涉及了许多数学的知识,另外有一个类似的类,参考(///cocos2d-x-3.0alpha0/coco

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

///\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之间. @sincev0.99.1 */ inlinefloatclampf(floatvalue,floatmin_inclusive,87); background-color:inherit; Font-weight:bold">floatmax_inclusive) { if(min_inclusive>max_inclusive){ CC_SWAP(min_inclusive,max_inclusive,float); } returnvalue<min_inclusive?min_inclusive:value<max_inclusive?value:max_inclusive; /** *@addtogroupdata_structures *@{ //点的赋值运算符和拷贝构造函数 classCC_DLLSize; classCC_DLL CCPoint public: floatx; floaty; /** *@JsNA CCPoint(); CCPoint(floatx,87); background-color:inherit; Font-weight:bold">floaty); *@luaNA */ CCPoint(constCCPoint&other); *@JsNA *@luaNA explicitCCPoint(constCCSize&size); CCPoint&operator=( CCPoint&operator=( CCPointoperator+(constCCPoint&right)const; CCPointoperator-(const; CCPointoperator-() CC Pointoperator*(floata) CCPointoperator/(voIDsetPoint(boolequals(constCCPoint&target)/**@returns如果点fuzzyequality(模糊平等)表示某种程度的差异相等。. @sincev2.1.4 boolfuzzyEquals(constCCPoint&target,87); background-color:inherit; Font-weight:bold">floatvariance)/**计算点和origin(原点)之间的距离 @returnfloat floatgetLength()const{ returnsqrtf(x*x+y*y); }; /**计算一个点长度的平方(不调用sqrt()) floatgetLengthSq()returndot(*this);//x*x+y*y; /**计算两点之间的距离的平方(不调用sqrt()) floatgetdistanceSq(constCCPoint&other)return(*this-other).getLengthSq(); /**计算两点之间的距离 floatgetdistance(this-other).getLength(); /**@returns此向量和x轴之间的角度,单位为弧度 @sincev2.1.4 floatgetAngle()const{ returnatan2f(y,x); }; /**@returns两个矢量方向之间的角度,单位为弧度 floatgetAngle(/**计算两个点之间的乘积. floatdot(returnx*other.x+y*other.y; /**计算两个点之间的交叉乘积 floatcross(returnx*other.y-y*other.x; /**计算这个点关于x轴的对称点(Point(-y,x)) @returnPoint inlineCCPointgetPerp()returnCCPoint(-y,x); /**计算两点之间的中点. @sincev3.0 inlineCCPointgetMIDpoint(const { returnCCPoint((x+other.x)/2.0f,(y+other.y)/2.0f); /**Clamp的点在min_inclusive和max_inclusive之间. inlineCCPointgetClampPoint(constCCPoint&min_inclusive,constCCPoint&max_inclusive)returnCCPoint(clampf(x,min_inclusive.x,max_inclusive.x),clampf(y,min_inclusive.y,max_inclusive.y)); /**运行每个点的数学数学运算功能 *absf,fllorf,ceilf,roundf *任何功能签名:floatfunc(float); *Forexample:我们尝试获取floor的x,y *p.compOp(floorf); inlineCCPointcompOp(std::function<float(float)>function)returnCCPoint(function(x),function(y)); /**计算这个点关于y轴的对称点(Point(y,-x)) @returnPoint inlineCCPointgetRPerp()returnCCPoint(y,-x); /**计算这个点与其他点的投影 inlineCCPointproject(returnother*(dot(other)/other.dot(other)); /**两个点的复合乘法运算("rotates"twopoints).//旋转 @return点向量与一个角度this.getAngle()+other.getAngle(), anDalengthofthis.getLength()*other.getLength(). inlineCCPointrotate(returnCCPoint(x*other.x-y*other.y,x*other.y+y*other.x); /**Unrotates(不旋转)两个点.//rotate(constPoint&other)的逆运算 @return点向量与一个角度this.getAngle()-other.getAngle(), anDalengthofthis.getLength()*other.getLength(). inlineCCPointunrotate(returnCCPoint(x*other.x+y*other.y,y*other.x-x*other.y); /**Returns点相乘的长度是1.(返回该点的倒数) *如果这个点是0,她会返回(1,0) inlineCCPointnormalize()floatlength=getLength(); if(length==0.)returnPoint(1.f,0); return*this/getLength(); /**a和b两点之间的线性插值(关于线性差值可以参考http://zh.wikipedia.org/zh-cn/线性插值‎) @returns Alpha==0?a Alpha==1?b otherwiseavaluebetweena..b inlineCCPointlerp(constCCPoint&other,87); background-color:inherit; Font-weight:bold">floatAlpha)this*(1.f-Alpha)+other*Alpha; /**一个点围绕轴心逆时针旋转的角度 @parampivot支点(类似自然界的支点) @paramangle逆时针旋转的角度,以弧度为单位 @returns旋转后的点 CCPointrotateByAngle(constCCPoint&pivot,87); background-color:inherit; Font-weight:bold">floatangle)staticinlineCCPointforAngle(constfloata) returnCCPoint(cosf(a),sinf(a)); } /**一个一般的线线相交测试 @paramAthestartpointforthefirstlineL1=(A-B)//第一条线的 @paramBtheendpointforthefirstlineL1=(A-B)//第一条线的 @paramCthestartpointforthesecondlineL2=(C-D)//第二条线的 @paramDtheendpointforthesecondlineL2=(C-D)//第二条线的 @paramStherangeforahitpointinL1(p=A+S*(B-A))//生命值的范围为 @paramTtherangeforahitpointinL2(p=C+T*(D-C))//生命值的范围为 @returnswhetherthesetwolinesinterects. 需要注意的是,真正测试交叉点的片段,我们必须确保S&T在射线[0..1]内确保S&T>0 命中点是C+T*(D-C); 命中点也是A+S*(B-A); @since3.0 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); returnstrue如果A-B线和C-D重叠 boolislineOverlap(constCCPoint&D); returnstrue如果A-B和C-D段平行 @sincev3.0 boolislineParallel(constCCPoint&D); returnstrue如果A-B段和C-D段重叠 boolisSegmentOverlap( CCPoint*S=nullptr,CCPoint*E=nullptr); returnstrue如果A-B段和C-D段相交 boolisSegmentIntersect(returnsA-B,C-D线的交点 staticCCPointgetIntersectPoint(constCCPointZERO; private: //returnstrue如果段A,B与C-D段相交.S->E是重叠的一部分 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); //两个向量的交叉替代产品.A->BXC->D floatcrossproduct2Vector(constCCPoint&D){return(D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);} classCC_DLL CCSize public: floatwIDth; floatheight; CCSize(); CCSize(floatwIDth,87); background-color:inherit; Font-weight:bold">floatheight); CCSize(constCCSize&other); explicitCCSize(constCCPoint&point); CCSize&operator=( CCSize&operator=( CCSizeoperator+(constCCSize&right) CCSizeoperator-( CCSizeoperator*( CCSizeoperator/(voIDsetSize(floatheight); constCCSize&target)constCCSizeZERO; classCC_DLL CCRect CCPointorigin; CCSizesize; CCRect(); CCRect(floaty,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCRect(constCCRect&other); CCRect&operator=(constCCRect&other); voIDsetRect(floatgetMinX()const;///return当前矩形最左边的x值 floatgetMIDX()///return当前矩形最中点的x值 floatgetMaxX()///return当前矩形最右边的x值 floatgetMinY()///return当前矩形最下边的y值 floatgetMIDY()///return当前矩形最中点的y值 floatgetMaxY()///return当前矩形最上边的y值 constCCRect&rect)boolcontainsPoint(constCCPoint&point)boolintersectsRect( RectunionWithRect(constCCRectZERO; //endofdata_structuregroup ///@} NS_CC_END #endif//__CCGEMETRY_H__ 总结

以上是内存溢出为你收集整理的cocos2dx 源码学习6 CCGeometry全部内容,希望文章能够帮你解决cocos2dx 源码学习6 CCGeometry所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1012829.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存