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

14 篇文章 1 订阅
2 篇文章 0 订阅

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

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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_陈陆亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值