WebGIS中一些功能算法实例

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

1、如何判断平面内两条线是否相交并返回交点?

 /// <summary>
        /// 判断平面内两条线是否相交并返回交点
        /// </summary>
        /// <param name="a">线段1起点坐标</param>
        /// <param name="b">线段1终点坐标</param>
        /// <param name="c">线段2起点坐标</param>
        /// <param name="d">线段2终点坐标</param>
        /// <param name="intersection">相交点坐标</param>
        /// <returns>是否相交 0:两线平行  -1:不平行且未相交  1:两线相交</returns>
        public static int GetIntersection(ESRI.ArcGIS.Client.Geometry.MapPoint a, ESRI.ArcGIS.Client.Geometry.MapPoint b, ESRI.ArcGIS.Client.Geometry.MapPoint c, ESRI.ArcGIS.Client.Geometry.MapPoint d, ref ESRI.ArcGIS.Client.Geometry.MapPoint intersection)
        {
            a = new MapPoint(Math.Round(a.X, 3), Math.Round(a.Y, 3));
            b = new MapPoint(Math.Round(b.X, 3), Math.Round(b.Y, 3));
            c = new MapPoint(Math.Round(c.X, 3), Math.Round(c.Y, 3));
            d = new MapPoint(Math.Round(d.X, 3), Math.Round(d.Y, 3));


            //判断异常
            if (Math.Abs(b.X - a.Y) + Math.Abs(b.X - a.X) + Math.Abs(d.Y - c.Y) + Math.Abs(d.X - c.X) == 0)
            {
                /*
                if (c.X - a.X == 0)
                    Debug.Print("ABCD是同一个点!");
                else
                    Debug.Print("AB是一个点,CD是一个点,且AC不同!");
                */
                return 0;
            }


            if (Math.Abs(b.Y - a.Y) + Math.Abs(b.X - a.X) == 0)
            {
                /*
                if ((a.X - d.X) * (c.Y - d.Y) - (a.Y - d.Y) * (c.X - d.X) == 0)
                    Debug.Print("A、B是一个点,且在CD线段上!");
                else
                    Debug.Print("A、B是一个点,且不在CD线段上!");
                */
                return 0;
            }
            if (Math.Abs(d.Y - c.Y) + Math.Abs(d.X - c.X) == 0)
            {
                /*
                if ((d.X - b.X) * (a.Y - b.Y) - (d.Y - b.Y) * (a.X - b.X) == 0)
                    Debug.Print("C、D是一个点,且在AB线段上!");
                else
                    Debug.Print("C、D是一个点,且不在AB线段上!");
                 */
                return 0;
            }


            if ((b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y) == 0)
            {
                //Debug.Print("线段平行,无交点!");
                return 0;
            }


            intersection.X = ((b.X - a.X) * (c.X - d.X) * (c.Y - a.Y) - c.X * (b.X - a.X) * (c.Y - d.Y) + a.X * (b.Y - a.Y) * (c.X - d.X)) / ((b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y));
            intersection.Y = ((b.Y - a.Y) * (c.Y - d.Y) * (c.X - a.X) - c.Y * (b.Y - a.Y) * (c.X - d.X) + a.Y * (b.X - a.X) * (c.Y - d.Y)) / ((b.X - a.X) * (c.Y - d.Y) - (b.Y - a.Y) * (c.X - d.X));


            if ((intersection.X - a.X) * (intersection.X - b.X) <= 0 && (intersection.X - c.X) * (intersection.X - d.X) <= 0 && (intersection.Y - a.Y) * (intersection.Y - b.Y) <= 0 && (intersection.Y - c.Y) * (intersection.Y - d.Y) <= 0)
            {
                // Debug.Print("线段相交于点(" + intersection.X + "," + intersection.Y + ")!");
                return 1; //'相交
            }
            else
            {
                //Debug.Print("线段相交于虚交点(" + intersection.X + "," + intersection.Y + ")!");
                return -1; //'相交但不在线段上
            }
        }

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:验证码PHP类 支持数字,字母,汉字,混合

下一篇:Android连接网络的代码