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