c# 判断两条线段是否相交(判断地图多边形是否相交)

  private void button1_Click(object sender, EventArgs e)
        {
            //var result = intersect3(point1, point2, point3, point4);
            var strPoints = this.txtPoints.Text.Trim(); //数据库复制出来的经纬度字符串
            strPoints = strPoints.Substring(0, strPoints.Length - 1);
            var pointArr = strPoints.Split(';');
            var lineList = new List<MapLine>();
            for (int i = 0; i < pointArr.Length; i++)
            {
                //y 经度 //x 纬度
                var line = new MapLine();
                string[] tempPoint1 = pointArr[i].Split(',');
                line.Point1 = new MapPoint() {y = Convert.ToDouble(tempPoint1[0]), x = Convert.ToDouble(tempPoint1[1])};

                string[] tempPoint2;
                if (i == pointArr.Length - 1)
                {
                    tempPoint2 = pointArr[0].Split(',');
                }
                else
                {
                    tempPoint2 = pointArr[i + 1].Split(',');
                }
                line.Point2 = new MapPoint() {y = Convert.ToDouble(tempPoint2[0]), x = Convert.ToDouble(tempPoint2[1])};
                lineList.Add(line);
            }
            bool xj = false;
            for (var i = 0; i < lineList.Count; i++)
            {
                var count = 0;
                for (var j = 0; j < lineList.Count; j++)
                {
                    if (i == j)//如果等于自己,不比较
                        continue;
                    var result = intersect(lineList[i].Point1, lineList[i].Point2, lineList[j].Point1,
                        lineList[j].Point2);
                    if (result)
                    {
                        count++;
                    }
                }
                if (count > 2)
                {
                    xj = true;
                }
            }
            MessageBox.Show(xj ? "相交" : "不相交");
        }


        //叉积  
        double mult(MapPoint a, MapPoint b, MapPoint c)
        {
            return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
        }

        //aa, bb为一条线段两端点 cc, dd为另一条线段的两端点 相交返回true, 不相交返回false  
        bool intersect(MapPoint aa, MapPoint bb, MapPoint cc, MapPoint dd)
        {
            if (Math.Max(aa.x, bb.x) < Math.Min(cc.x, dd.x))
            {
                return false;
            }
            if (Math.Max(aa.y, bb.y) < Math.Min(cc.y, dd.y))
            {
                return false;
            }
            if (Math.Max(cc.x, dd.x) < Math.Min(aa.x, bb.x))
            {
                return false;
            }
            if (Math.Max(cc.y, dd.y) < Math.Min(aa.y, bb.y))
            {
                return false;
            }
            if (mult(cc, bb, aa) * mult(bb, dd, aa) < 0)
            {
                return false;
            }
            if (mult(aa, dd, cc) * mult(dd, bb, cc) < 0)
            {
                return false;
            }
            return true;
        }  

  

strPoints 
例如 106.549238056551,29.5682761629536;106.544257083204,29.5651964736277;106.550028675985,29.5651166254344;106.544602513089,29.5681623774632;
posted @ 2017-04-20 16:49  dotNet修行之路  阅读(1297)  评论(0编辑  收藏  举报