首页 新闻 会员 周边 捐助

判断线段与多边形、多边形与多边形是否相交的算法(C#)

0
悬赏园豆:15 [已关闭问题] 关闭于 2015-08-28 15:48

判断线段与多边形、多边形与多边形是否相交(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;
          }
C#
CynthiaTT的主页 CynthiaTT | 菜鸟二级 | 园豆:204
提问于:2015-07-10 16:22
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册