原文地址:http://www.jb51.cc/article/p-yefdcykj-yd.html
- ///\cocos2d-x-3.0alpha0\cocos2dx\cocoa\CCGeometry.h@H_301_10@
- //这个类主要是讲点和点之间的关系、线和线之间的关系、点和坐标轴之间的关系,这个类涉及了许多数学的知识,另外有一个类似的类,参考(///cocos2d-x-3.0alpha0/cocos2dx/include/CCDeprecated.h)@H_301_10@
- @H_301_10@
- @H_301_10@
- #ifndef__CCGEMETRY_H__@H_301_10@
- #define__CCGEMETRY_H__@H_301_10@
- #include<math.h>@H_301_10@
- #include<functional>@H_301_10@
- #include"platform/CCPlatformMacros.h"@H_301_10@
- #include"CCObject.h"@H_301_10@
- #include"ccMacros.h"@H_301_10@
- NS_CC_BEGIN@H_301_10@
- /**Clamp的值在min_inclusive和max_inclusive之间.@H_301_10@
- @sincev0.99.1@H_301_10@
- */@H_301_10@
- @H_502_103@inlinefloatclampf(floatvalue,floatmin_inclusive,87); background-color:inherit; font-weight:bold">floatmax_inclusive)@H_301_10@
- {@H_301_10@
- if(min_inclusive>max_inclusive){@H_301_10@
- CC_SWAP(min_inclusive,max_inclusive,float);@H_301_10@
- }@H_301_10@
- returnvalue<min_inclusive?min_inclusive:value<max_inclusive?value:max_inclusive;@H_301_10@
- /**@H_301_10@
- *@addtogroupdata_structures@H_301_10@
- *@{@H_301_10@
- //点的赋值运算符和拷贝构造函数@H_301_10@
- classCC_DLLSize;@H_301_10@
- classCC_DLL CCPoint@H_301_10@
- public:@H_301_10@
- floatx;@H_301_10@
- floaty;@H_301_10@
- /**@H_301_10@
- *@jsNA@H_301_10@
- CCPoint();@H_301_10@
- CCPoint(floatx,87); background-color:inherit; font-weight:bold">floaty);@H_301_10@
- *@luaNA@H_301_10@
- */@H_301_10@
- CCPoint(constCCPoint&other);@H_301_10@
- *@jsNA@H_301_10@
- *@luaNA@H_301_10@
- explicitCCPoint(constCCSize&size);@H_301_10@
- CCPoint&operator=( CCPoint&operator=( CCPointoperator+(constCCPoint&right)const;@H_301_10@
- CCPointoperator-(const;@H_301_10@
- CCPointoperator-() CC Pointoperator*(floata) CCPointoperator/(voidsetPoint(boolequals(constCCPoint&target)/**@returns如果点fuzzyequality(模糊平等)表示某种程度的差异相等。.@H_301_10@
- @sincev2.1.4
@H_301_10@- bool
fuzzyEquals(constCCPoint&target,87); background-color:inherit; font-weight:bold">floatvariance)/**计算点和origin(原点)之间的距离@H_301_10@- @returnfloat
@H_301_10@- float
getLength()const{@H_301_10@ - returnsqrtf(x*x+y*y);@H_301_10@
- };@H_301_10@
- /**计算一个点长度的平方(不调用sqrt())@H_301_10@
- floatgetLengthSq()returndot(*this);//x*x+y*y;@H_301_10@
- /**计算两点之间的距离的平方(不调用sqrt())@H_301_10@
- floatgetDistanceSq(constCCPoint&other)return(*this-other).getLengthSq();@H_301_10@
- /**计算两点之间的距离@H_301_10@
- floatgetDistance(this-other).getLength();@H_301_10@
- /**@returns此向量和x轴之间的角度,单位为弧度@H_301_10@
- @sincev2.1.4@H_301_10@
- floatgetAngle()const{@H_301_10@
- returnatan2f(y,x);@H_301_10@
- };@H_301_10@
- /**@returns两个矢量方向之间的角度,单位为弧度@H_301_10@
- floatgetAngle(/**计算两个点之间的乘积.@H_301_10@
- floatdot(returnx*other.x+y*other.y;@H_301_10@
- /**计算两个点之间的交叉乘积@H_301_10@
- floatcross(returnx*other.y-y*other.x;@H_301_10@
- /**计算这个点关于x轴的对称点(Point(-y,x))@H_301_10@
- @returnPoint@H_301_10@
- inlineCCPointgetPerp()returnCCPoint(-y,x);@H_301_10@
- /**计算两点之间的中点.@H_301_10@
- @sincev3.0@H_301_10@
- inlineCCPointgetMidpoint(const@H_301_10@
- {@H_301_10@
- returnCCPoint((x+other.x)/2.0f,(y+other.y)/2.0f);@H_301_10@
- /**Clamp的点在min_inclusive和max_inclusive之间.@H_301_10@
- 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@
- /**运行每个点的数学数学运算功能@H_301_10@
- *absf,fllorf,ceilf,roundf@H_301_10@
- *任何功能签名:floatfunc(float);@H_301_10@
- *Forexample:我们尝试获取floor的x,y@H_301_10@
- *p.compOp(floorf);@H_301_10@
- inlineCCPointcompOp(std::function<float(float)>function)returnCCPoint(function(x),function(y));@H_301_10@
- /**计算这个点关于y轴的对称点(Point(y,-x))@H_301_10@
- @returnPoint@H_301_10@
- inlineCCPointgetRPerp()returnCCPoint(y,-x);@H_301_10@
- /**计算这个点与其他点的投影@H_301_10@
- inlineCCPointproject(returnother*(dot(other)/other.dot(other));@H_301_10@
- /**两个点的复合乘法运算("rotates"twopoints).//旋转@H_301_10@
- @return点向量与一个角度this.getAngle()+other.getAngle(),@H_301_10@
- andalengthofthis.getLength()*other.getLength().@H_301_10@
- inlineCCPointrotate(returnCCPoint(x*other.x-y*other.y,x*other.y+y*other.x);@H_301_10@
- /**Unrotates(不旋转)两个点.//rotate(constPoint&other)的逆运算@H_301_10@
- @return点向量与一个角度this.getAngle()-other.getAngle(),@H_301_10@
- andalengthofthis.getLength()*other.getLength().@H_301_10@
- inlineCCPointunrotate(returnCCPoint(x*other.x+y*other.y,y*other.x-x*other.y);@H_301_10@
- /**Returns点相乘的长度是1.(返回该点的倒数)@H_301_10@
- *如果这个点是0,她会返回(1,0)@H_301_10@
- inlineCCPointnormalize()floatlength=getLength();@H_301_10@
- if(length==0.)returnPoint(1.f,0);@H_301_10@
- return*this/getLength();@H_301_10@
- /**a和b两点之间的线性插值(关于线性差值可以参考http://zh.wikipedia.org/zh-cn/线性插值)@H_301_10@
- @returns@H_301_10@
- alpha==0?a@H_301_10@
- alpha==1?b@H_301_10@
- otherwiseavaluebetweena..b@H_301_10@
- inlineCCPointlerp(constCCPoint&other,87); background-color:inherit; font-weight:bold">floatalpha)this*(1.f-alpha)+other*alpha;@H_301_10@
- /**一个点围绕轴心逆时针旋转的角度@H_301_10@
- @parampivot支点(类似自然界的支点)@H_301_10@
- @paramangle逆时针旋转的角度,以弧度为单位@H_301_10@
- @returns旋转后的点@H_301_10@
- CCPointrotateByAngle(constCCPoint&pivot,87); background-color:inherit; font-weight:bold">floatangle)staticinlineCCPointforAngle(constfloata)@H_301_10@
- returnCCPoint(cosf(a),sinf(a));@H_301_10@
- }@H_301_10@
- /**一个一般的线线相交测试@H_301_10@
- @paramAthestartpointforthefirstlineL1=(A-B)//第一条线的@H_301_10@
- @paramBtheendpointforthefirstlineL1=(A-B)//第一条线的@H_301_10@
- @paramCthestartpointforthesecondlineL2=(C-D)//第二条线的@H_301_10@
- @paramDtheendpointforthesecondlineL2=(C-D)//第二条线的@H_301_10@
- @paramStherangeforahitpointinL1(p=A+S*(B-A))//生命值的范围为@H_301_10@
- @paramTtherangeforahitpointinL2(p=C+T*(D-C))//生命值的范围为@H_301_10@
- @returnswhetherthesetwolinesinterects.@H_301_10@
- @H_301_10@
- 需要注意的是,真正测试交叉点的片段,我们必须确保S&T在射线[0..1]内确保S&T>0@H_301_10@
- 命中点是C+T*(D-C);@H_301_10@
- 命中点也是A+S*(B-A);@H_301_10@
- @since3.0@H_301_10@
- 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@
- returnstrue如果A-B线和C-D重叠@H_301_10@
- boolisLineOverlap(constCCPoint&D);@H_301_10@
- returnstrue如果A-B和C-D段平行@H_301_10@
- @sincev3.0@H_301_10@
- boolisLineParallel(constCCPoint&D);@H_301_10@
- returnstrue如果A-B段和C-D段重叠@H_301_10@
- boolisSegmentOverlap( CCPoint*S=nullptr,CCPoint*E=nullptr);@H_301_10@
- returnstrue如果A-B段和C-D段相交
@H_301_10@ - boolisSegmentIntersect(returnsA-B,C-D线的交点@H_301_10@
- staticCCPointgetIntersectPoint(constCCPointZERO;@H_301_10@
- private:@H_301_10@
- //returnstrue如果段A,B与C-D段相交.S->E是重叠的一部分@H_301_10@
- 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@
- //两个向量的交叉替代产品.A->BXC->D@H_301_10@
- floatcrossProduct2Vector(constCCPoint&D){return(D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);}@H_301_10@
- classCC_DLL CCSize@H_301_10@
- public:@H_301_10@
- floatwidth;@H_301_10@
- floatheight;@H_301_10@
- CCSize();@H_301_10@
- CCSize(floatwidth,87); background-color:inherit; font-weight:bold">floatheight);@H_301_10@
- CCSize(constCCSize&other);@H_301_10@
- explicitCCSize(constCCPoint&point);@H_301_10@
- CCSize&operator=( CCSize&operator=( CCSizeoperator+(constCCSize&right) CCSizeoperator-( CCSizeoperator*( CCSizeoperator/(voidsetSize(floatheight);@H_301_10@
- const
CCSize&target)constCCSizeZERO;@H_301_10@- class
CC_DLL CCRect@H_301_10@- CCPointorigin;@H_301_10@
- CCSizesize;@H_301_10@
- CCRect();@H_301_10@
- CCRect(floaty,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCRect(constCCRect&other);@H_301_10@
- CCRect&operator=(constCCRect&other);@H_301_10@
- void
setRect(floatgetMinX()const;///return当前矩形最左边的x值@H_301_10@- float
getMidX()///return当前矩形最中点的x值@H_301_10@- float
getMaxX()///return当前矩形最右边的x值@H_301_10@- float
getMinY()///return当前矩形最下边的y值@H_301_10@ - floatgetMidY()///return当前矩形最中点的y值@H_301_10@
- floatgetMaxY()///return当前矩形最上边的y值@H_301_10@
- constCCRect&rect)boolcontainsPoint(constCCPoint&point)boolintersectsRect( RectunionWithRect(constCCRectZERO;@H_301_10@
- //endofdata_structuregroup
@H_301_10@ - ///@}@H_301_10@
- NS_CC_END@H_301_10@
- #endif//__CCGEMETRY_H__@H_301_10@