C#百度地图判断一个点是否在几何图形上Demo

来源:互联网 发布:windows pe有什么用 编辑:程序博客网 时间:2024/06/01 11:41

项目需要查找指定范围里面是否存在某点的需求

1、首先是几何坐标集

   point[] polygon = new point[] {            new point(112.579325, 26.915291),            new point(112.584967,26.899086),            new point(112.608287,26.898023),            new point(112.602825,26.914356),            new point(112.588254,26.909862)            };

哎呀 写好了 没什么说的了....

直接贴代码吧


using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication1{    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            double lng=Convert.ToDouble( textBox1.Text);            double lat = Convert.ToDouble(textBox2.Text);            point point = new point(lng, lat);            point ne=new point(0,0),sw = new point(0, 0);            FindAppointDirection(polygon,ref ne, ref sw);//找东北 西南坐标            if (isPointInRect(ne, sw, point)) {                bool fuck = CalculationAlgorithm(polygon, point);                if (fuck) {                    label1.Text ="目标点在几何图形中";                }                else {                    label1.Text = "目标点不在几何图形中";                }                         }        }       public bool CalculationAlgorithm(point[] polygon, point p) {            point[] pts = polygon;//获取多边形点            //112.58464,26.909432            int N = pts.Length;            var boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true            var intersectCount = 0;//cross points count of x             var precision = 2e-10; //浮点类型计算时候与0比较时候的容差            point p1, p2;//neighbour bound vertices            p1 = pts[0];//left vertex                    for (var i = 1; i <= N; ++i)            {//check all rays                            if (p.Equals(p1))                {                    return boundOrVertex;//p is an vertex                }                p2 = pts[i % N];//right vertex                            if (p.lat < Math.Min(p1.lat, p2.lat) || p.lat > Math.Max(p1.lat, p2.lat))                {//ray is outside of our interests                                    p1 = p2;                    continue;//next ray left point                }                if (p.lat > Math.Min(p1.lat, p2.lat) && p.lat < Math.Max(p1.lat, p2.lat))                {//ray is crossing over by the algorithm (common part of)                    if (p.lng <= Math.Max(p1.lng, p2.lng))                    {//x is before of ray                                            if (p1.lat == p2.lat && p.lng >= Math.Min(p1.lng, p2.lng))                        {//overlies on a horizontal ray                            return boundOrVertex;                        }                        if (p1.lng == p2.lng)                        {//ray is vertical                                                    if (p1.lng == p.lng)                            {//overlies on a vertical ray                                return boundOrVertex;                            }                            else                            {//before ray                                ++intersectCount;                            }                        }                        else                        {//cross point on the left side                                                    var xinters = (p.lat - p1.lat) * (p2.lng - p1.lng) / (p2.lat - p1.lat) + p1.lng;//cross point of lng                                                    if (Math.Abs(p.lng - xinters) < precision)                            {//overlies on a ray                                return boundOrVertex;                            }                            if (p.lng < xinters)                            {//before ray                                ++intersectCount;                            }                        }                    }                }                else                {//special case when ray is crossing through the vertex                                    if (p.lat == p2.lat && p.lng <= p2.lng)                    {//p crossing over p2                                            var p3 = pts[(i + 1) % N]; //next vertex                                            if (p.lat >= Math.Min(p1.lat, p3.lat) && p.lat <= Math.Max(p1.lat, p3.lat))                        {//p.lat lies between p1.lat & p3.lat                            ++intersectCount;                        }                        else                        {                            intersectCount += 2;                        }                    }                }                p1 = p2;//next ray left point            }            if (intersectCount % 2 == 0)            {//偶数在多边形外                return false;            }            else            { //奇数在多边形内                return true;            }        }       point[] polygon = new point[] {            new point(112.579325, 26.915291),            new point(112.584967,26.899086),            new point(112.608287,26.898023),            new point(112.602825,26.914356),            new point(112.588254,26.909862)            };        /// <summary>        /// 判断点是否在矩形内        /// </summary>        /// <param name="ne"></param>        /// <param name="sw"></param>        /// <param name="Tagrtpoint"></param>        /// <returns></returns>        bool isPointInRect(point ne, point sw,point Tagrtpoint) {            //西南脚点 point sw = new point(112.579325, 26.898023);            //东北  point ne = new point(26.915291, 112.608287);            return (Tagrtpoint.lng >= sw.lng && Tagrtpoint.lng <= ne.lng && Tagrtpoint.lat >= sw.lat && Tagrtpoint.lat <= ne.lat);        }        /// <summary>        /// 找东西南北经方向 简单说找东北 西南 坐标做一个矩形判断        /// </summary>        void FindAppointDirection(point[] polygon,ref point ne,ref point sw) {                      double[] iArrarylat = new double[polygon.Length];            double[] iArrarylng = new double[polygon.Length];            for (int i=0;i< polygon.Length;i++) {                iArrarylat[i] = polygon[i].lat;                iArrarylng[i] = polygon[i].lng;            }            Array.Sort(iArrarylat);            Array.Sort(iArrarylng);            ne.lat = iArrarylat[polygon.Length - 1];            ne.lng = iArrarylng[polygon.Length - 1];            sw.lat = iArrarylat[0];            sw.lng = iArrarylng[0] ;        }        public class point {            public  double lng;            public  double lat;            public point(double lng, double lat)            {                this.lat = lat;                this.lng = lng;            }        }    }}



运行图





源码Demo下载地址:点我跳转

阅读全文
0 0
原创粉丝点击