问题描述
判断线段与多边形、多边形与多边形是否相交(C#),包括线段、多边形包含在多边形内,线段与多边形某一条边重合的情况判断。求教各位大神!我有写一个判断线段与多边形的边时候相交的算法,但是貌似没有判断成功,请各位大神指导!谢谢!publicboolGetLineIntersection(GraphicGraphic,GraphicDrawPolygonGraphic){doubledistAB,theCos,theSin,newX,ABpos;doubleX=0;doubleY=0;DrawPolygon=DrawPolygonGraphic.GeometryasPolygon;if(Graphic.Geometry.GetType().ToString()=="ESRI.ArcGIS.Client.Geometry.Polyline"){polyline=Graphic.GeometryasPolyline;foreach(varpointCollectioninDrawPolygon.Rings){PointCollectiondrawlist=pointCollection;foreach(varpcinpolyline.Paths){PointCollectionlinelist=pc;for(inti=0;i<drawlist.Count-1;i++){for(intj=0;j<linelist.Count-1;j++){if((drawlist[i+1].Y-drawlist[i].Y)*(linelist[j].X-linelist[j+1].X)-(drawlist[i+1].X-drawlist[i].X)*(linelist[j].Y-linelist[j+1].Y)==0){returnfalse;}X=((drawlist[i+1].X-drawlist[i].X)*(linelist[i].X-linelist[i+1].X)*(linelist[i].Y-drawlist[i].Y)-linelist[i].X*(drawlist[i+1].X-drawlist[i].X)*(linelist[i].Y-linelist[i+1].Y)+drawlist[i].X*(drawlist[i+1].Y-drawlist[i].Y)*(linelist[i].X-linelist[i+1].X))/((drawlist[i+1].Y-drawlist[i].Y)*(linelist[i].X-linelist[i+1].X)-(drawlist[i+1].X-drawlist[i].X)*(linelist[i].Y-linelist[i+1].Y));Y=((drawlist[i+1].Y-drawlist[i].Y)*(linelist[i].Y-linelist[i+1].Y)*(linelist[i].X-drawlist[i].X)-linelist[i].Y*(drawlist[i+1].Y-drawlist[i].Y)*(linelist[i].X-linelist[i+1].X)+drawlist[i].Y*(drawlist[i+1].X-drawlist[i].X)*(linelist[i].Y-linelist[i+1].Y))/((drawlist[i+1].X-drawlist[i].X)*(linelist[i].Y-linelist[i+1].Y)-(drawlist[i+1].Y-drawlist[i].Y)*(linelist[i].X-linelist[i+1].X));if((X-drawlist[i].X)*(X-drawlist[i+1].X)<=0&&(X-linelist[i].X)*(X-linelist[i+1].X)<=0&&(Y-drawlist[i].Y)*(Y-drawlist[i+1].Y)<=0&&(Y-linelist[i].Y)*(Y-linelist[i+1].Y)<=0){returntrue;}}}}}}returnfalse;}
解决方案
解决方案二:
点是否在多边形内(含边界上)privateboolpointInPolygon(floatx,floaty,PointF[]polygon){inti;intj=polygon.Length-1;booloddNodes=false;for(i=0;i<polygon.Length;i++){if((polygon[i].Y<y&&polygon[j].Y>=y||polygon[j].Y<y&&polygon[i].Y>=y)&&(polygon[i].X<=x||polygon[j].X<=x)){if(polygon[i].X+(y-polygon[i].Y)/(polygon[j].Y-polygon[i].Y)*(polygon[j].X-polygon[i].X)<x){oddNodes=!oddNodes;}}j=i;}returnoddNodes;}
判断多边形、线段是否和另一多边形相交,可先判断他们的控制矩形是否相交RectangleF.IntersectsWith方法更可以用RectangleF.IntersectsWith取回控制举行的交集然后再做点是否在多边形中的判断用GraphicsPath.IsVisible可能更快
解决方案三:
你好!请问RectangleF.IntersectsWith方法怎么写?
解决方案四:
给你一个演示usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace多边形相交{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}protectedoverridevoidOnPaint(PaintEventArgse){base.OnPaint(e);varg=e.Graphics;g.SmoothingMode=SmoothingMode.AntiAlias;g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.CompositingQuality=CompositingQuality.HighQuality;using(varp1=newGraphicsPath()){p1.AddPolygon(newPointF[]{newPointF(10,20),newPointF(80,160),newPointF(200,25)});g.DrawPath(Pens.Green,p1);varp2=newGraphicsPath();p2.AddPolygon(newPointF[]{newPointF(210,200),newPointF(100,100),newPointF(220,30)});g.DrawPath(Pens.Red,p2);varbox1=p1.GetBounds();varbox2=p2.GetBounds();if(box1.IntersectsWith(box2)){varpen1=newPen(Color.Green,1);pen1.DashStyle=System.Drawing.Drawing2D.DashStyle.Custom;pen1.DashPattern=newfloat[]{5,5};g.DrawRectangle(pen1,box1.X,box1.Y,box1.Width,box1.Height);varpen2=newPen(Color.Red,1);pen2.DashStyle=System.Drawing.Drawing2D.DashStyle.Custom;pen2.DashPattern=newfloat[]{5,5};g.DrawRectangle(pen2,box2.X,box2.Y,box2.Width,box2.Height);varbox3=RectangleF.Intersect(box1,box2);varpen3=newPen(Color.Black,3);pen3.DashStyle=System.Drawing.Drawing2D.DashStyle.Custom;pen3.DashPattern=newfloat[]{1,1};g.DrawRectangle(pen3,box3.X,box3.Y,box3.Width,box3.Height);}}}}}
红、绿实线为多边形,红、绿虚线框分别是他们的控制矩形黑虚线框是两个控制矩形的相交矩形你可据此写出分析算法,而不是一味的循环