原文地址: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__