• 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;
  • 相关阅读:
    连接数据库修改篇
    连接数据库
    get和post
    php连接mysql
    AJAX基础!
    JAVAsrcipt总结!
    使用AJAX如何得到数据库当中的值!
    AJAX如何做出输入框提示!
    PHP数据连接主键与外键!
    JAVA当中内存管理与垃圾回收!
  • 原文地址:https://www.cnblogs.com/fjzhang/p/6739609.html
Copyright © 2020-2023  润新知