chjh_721 发表于 2006-9-2 12:27:00

[ARX] 初学者求救大虾帮忙,急!!谢谢!!

<P>#include "math.h"<BR>//#include"gept2dar.h"<BR>#include"geline2d.h"<BR>//#include"acarray.h"<BR>#include"gepnt2d.h"<BR>#include"gelnsg2d.h"</P>
<P><BR>//以下为计算凸多边形面积方法&nbsp;&nbsp; <BR>double areaofchimb(AcGePoint2dArray&amp;ptarr)<BR>{<BR>AcGePoint2d *p=&amp;ptarr.at(0);<BR>&nbsp;&nbsp; int i;<BR>&nbsp; int n;//点的个数<BR>&nbsp;<BR>&nbsp; n=ptarr.length();<BR>&nbsp; AcGePoint2d startpoint=ptarr.first();<BR>&nbsp; AcGePoint2d endpoint=ptarr.last();</P>
<P>//求顶点到其余点的对角线长<BR>&nbsp;double*arry=new double;</P>
<P>&nbsp;for(i=0;i&lt;n-3;i++)<BR>//&nbsp;&nbsp;&nbsp; arry=(ptarr.at(0)).distanceTo(ptarr.at(i+2));<BR>arry=ptarr.distanceTo(ptarr);<BR>//求面积 <BR>&nbsp;&nbsp;&nbsp; double area=0;<BR>&nbsp;int j=0;<BR>&nbsp; for(j=0;j&lt;n-2;j++)<BR>&nbsp; { double a;<BR>&nbsp;a=startpoint.distanceTo(ptarr);<BR>&nbsp;// a=ads_distance(ptarr,ptarr);<BR>&nbsp;double b;<BR>&nbsp;&nbsp;&nbsp; b=(ptarr).distanceTo(ptarr);<BR>//&nbsp;b=ads_distance(ptarr,ptarr);<BR>&nbsp;&nbsp;&nbsp; double d;<BR>&nbsp;&nbsp;&nbsp; if (j&lt;n-3)<BR>&nbsp; d=arry;<BR>&nbsp; else<BR>&nbsp; &nbsp; d=startpoint.distanceTo(endpoint);<BR>//&nbsp;&nbsp; d=ads_distance(ptarr,ptarr);<BR>&nbsp;&nbsp;&nbsp; double c=(a+b+d)/2;<BR>&nbsp;&nbsp;&nbsp; double s=sqrt(c*(c-a)*(c-b)*(c-d));<BR>&nbsp;&nbsp;&nbsp; area+=s;<BR>&nbsp; }<BR>&nbsp;return area;&nbsp; <BR>}</P>
<P><BR>//计算多边形面积&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; !!需要修改,补凸的时候可能使临近两点变凹&nbsp;&nbsp; <BR>double area(AcGePoint2dArray &amp;ptarr)<BR>{&nbsp; <BR>&nbsp;bool&nbsp; PointInPolygon(AcGePoint2d&amp; pt,AcGePoint2dArray &amp;ptarr);<BR>&nbsp;&nbsp;&nbsp; double areaoftriangle(AcGePoint2d ,AcGePoint2d,AcGePoint2d );<BR>&nbsp;&nbsp;&nbsp; int n;//点的个数<BR>&nbsp;n=4;<BR>&nbsp;&nbsp;&nbsp; ptarr.at(0).set(10,10);<BR>&nbsp;&nbsp;&nbsp; ptarr.at(1).set(10,20);<BR>&nbsp;&nbsp;&nbsp; ptarr.at(2).set(20,20);<BR>&nbsp;&nbsp;&nbsp; ptarr.at(3).set(20,10);</P>
<P><BR>&nbsp;double area=0;<BR>&nbsp;&nbsp;&nbsp; double s=0;<BR>&nbsp;&nbsp;&nbsp; int i;<BR>&nbsp;for(i=0;i&lt;n;i++)<BR>&nbsp;{double s=0;<BR>&nbsp;AcGePoint2d*ppt=new AcGePoint2d(ptarr.at(i));<BR>&nbsp;&nbsp;&nbsp; ptarr.removeAt(i);<BR>&nbsp;&nbsp;&nbsp; bool flag;<BR>&nbsp;flag=PointInPolygon(*ppt,ptarr);<BR>&nbsp; if(flag==true) ptarr.insertAt(i,*ppt);<BR>&nbsp; else<BR>&nbsp;&nbsp; {AcGeLine2d*line=new AcGeLine2d(ptarr.at((i-1+n-1)%(n-1)),ptarr.at((i+n-1)%(n-1)));<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; AcGePoint2d newpoint=(*ppt).mirror(*line);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptarr.insertAt(i,newpoint);<BR>&nbsp;&nbsp;&nbsp; s=s-2*(areaoftriangle(ptarr.at((n+i-1)%n),ptarr.at(i),ptarr.at((n+i+1)%n)));<BR>&nbsp;&nbsp; }<BR>&nbsp;}<BR>&nbsp;area=areaofchimb(ptarr);<BR>&nbsp;area=area-s;<BR>&nbsp;return area;</P>
<P>}</P>
<P><BR>//以下判断点是否在多边形中,在返回false ,不在返回true&nbsp;&nbsp;&nbsp;&nbsp; <BR>bool&nbsp; PointInPolygon(AcGePoint2d&amp; pt,AcGePoint2dArray &amp;ptarr)<BR>{<BR>&nbsp;int n = ptarr.length();<BR>&nbsp;&nbsp;&nbsp; int&nbsp;i, count=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;double yup, xup, ydown, xdown;<BR>//&nbsp;double y0, x0;<BR>&nbsp;double t;<BR>&nbsp;for( i=0; i&lt; n; i++ )<BR>&nbsp;{<BR>&nbsp; if((pt.x ==(ptarr.at(i).x)&amp;&amp;(pt.y==ptarr.at(i).y)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //!!检查括号匹配不??<BR>&nbsp;&nbsp; || (pt.x==(ptarr.at((i+1)%n).x))&amp;&amp;(pt.y==(ptarr.at((i+1)%n).y))))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<BR>&nbsp;xup=(ptarr.at(i)).x&gt;(ptarr.at((i+1)%n).x)?(ptarr.at(i)).x:(ptarr.at((i+1)%n).x);<BR>&nbsp;yup=(ptarr.at(i)).y&gt;(ptarr.at((i+1)%n).y)?(ptarr.at(i)).y:(ptarr.at((i+1)%n).y);<BR>&nbsp;xdown=(ptarr.at(i)).x&lt;(ptarr.at((i+1)%n).x)?(ptarr.at(i)).x:(ptarr.at((i+1)%n).x);<BR>&nbsp;ydown=yup=(ptarr.at(i)).y&lt;(ptarr.at((i+1)%n).y)?(ptarr.at(i)).y:(ptarr.at((i+1)%n).y);<BR>&nbsp;&nbsp; if(pt.y&gt;yup)<BR>&nbsp;&nbsp;&nbsp; continue;<BR>&nbsp;&nbsp; if(pt.y&lt;ydown)<BR>&nbsp;&nbsp;&nbsp; continue;<BR>&nbsp;&nbsp; if(pt.y==ydown)<BR>&nbsp;&nbsp;&nbsp;&nbsp; if(pt.y&lt;yup&amp;&amp;pt.x&lt;xdown)&nbsp; count++;<BR>&nbsp; else continue;<BR>&nbsp;&nbsp; if(pt.y&lt;yup&amp;&amp;pt.y&gt;ydown)<BR>&nbsp;&nbsp;&nbsp; double&nbsp; t=xdown+(pt.y-ydown)*(xup-xdown)/(yup-ydown);<BR>&nbsp;if(t&gt;pt.x)&nbsp; count++;<BR>&nbsp;if(t=pt.x)&nbsp; return true;<BR>&nbsp;}<BR>&nbsp; if(count%2==0)&nbsp; return false;<BR>&nbsp; else <BR>&nbsp;&nbsp; return true;<BR>}</P>
<P>//计算三角形面积&nbsp; <BR>&nbsp;double areaoftriangle(AcGePoint2d pt1,AcGePoint2d pt2,AcGePoint2d pt3)<BR>&nbsp;{<BR>&nbsp; double a,b,c,l,s;<BR>&nbsp;&nbsp;&nbsp;&nbsp; a=pt1.distanceTo(pt2);<BR>&nbsp;&nbsp;&nbsp;&nbsp; b=pt2.distanceTo(pt3);<BR>&nbsp;&nbsp;&nbsp;&nbsp; c=pt3.distanceTo(pt1);<BR>&nbsp;&nbsp;&nbsp;&nbsp; l=(a+b+c)/2;<BR>&nbsp;&nbsp;&nbsp;&nbsp; s=sqrt(l*(l-a)*(l-b)*(l-c));<BR>&nbsp;&nbsp;&nbsp;&nbsp; return s;<BR>&nbsp;}<BR></P>
页: [1]
查看完整版本: [ARX] 初学者求救大虾帮忙,急!!谢谢!!