判断线段与多边形、多边形与多边形是否相交(C#),包括线段、多边形包含在多边形内,线段与多边形某一条边重合的情况判断。求教各位大神!
我有写一个判断线段与多边形的边时候相交的算法,但是貌似没有判断成功,请各位大神指导。
public bool GetLineIntersection(Graphic Graphic, Graphic DrawPolygonGraphic) { double distAB, theCos, theSin, newX, ABpos; double X = 0; double Y = 0; DrawPolygon = DrawPolygonGraphic.Geometry as Polygon; if (Graphic.Geometry.GetType().ToString() == "ESRI.ArcGIS.Client.Geometry.Polyline") { polyline = Graphic.Geometry as Polyline; foreach (var pointCollection in DrawPolygon.Rings) { PointCollection drawlist = pointCollection; foreach (var pc in polyline.Paths) { PointCollection linelist = pc; for (int i = 0; i < drawlist.Count - 1; i++) { for (int j = 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) { return false; } 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) { return true; } } } } } } return false; }