c# 判断点与圆、矩形、多边形的关系

本文介绍了C#中如何使用自定义类实现对点、圆、矩形和多边形的空间结构封装,并提供了判断点与这些空间结构位置关系的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c#中并未提供类型GIS的空间结构,把点、圆、矩形、多边形等封装在一起,但是基本的空间位置关系还是可以判断的:

       //判断点与矩形、圆及多边形的位置关系
    public class PointHelper
    {
        public static Boolean PointInRect(Point p,Rectangle rect)
        {
            return rect.Contains(p);
        }
        public static Boolean PointInCircle(Point p, Point circleStart, Point circleEnd)
        {
            try
            {
                double radius = Math.Sqrt(Math.Pow(Math.Abs(circleStart.X - circleEnd.X), 2) + Math.Pow(Math.Abs(circleStart.Y - circleEnd.Y), 2));
                double distance = Math.Sqrt(Math.Pow(Math.Abs(circleStart.X - p.X), 2) + Math.Pow(Math.Abs(circleStart.Y - p.Y), 2));
                return distance <= radius;
            }
            catch (Exception ex)
            {
               
                return false;
            }
        }
        //str的格式:121.436372,31.3397;121.463393,31.3397;121.463393,31.327979;121.436372,31.32797
        public static Boolean PointInPolygon(Point p, String str)
        {
            try
            {
                List<Point> pList = getPointList(str);
                System.Drawing.Drawing2D.GraphicsPath myGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
                Region myRegion = new Region();

                myGraphicsPath.Reset();
                myGraphicsPath.AddPolygon(pList.ToArray());
                myRegion.MakeEmpty();
                myRegion.Union(myGraphicsPath);
                //返回判断点是否在多边形里
                return myRegion.IsVisible(p);
            }
            catch (Exception ex)
            {
               
                return false;
            }
        }
        public static Point getPoint(string s)
        {
            try
            {
                Point p = new Point();
                string[] pArr = s.Split(',');
                p.X = (int)(double.Parse(pArr[0]) * 1000000);
                p.Y = (int)(double.Parse(pArr[1]) * 1000000);
                return p;
            }
            catch (Exception ex)
            {
                
                return new Point(); ;
            }
        }
        public static List<Point> getPointList(string str)
        {
            try
            {
                List<Point> pList = new List<Point>();
                if (str.Contains(";"))
                {
                    string[] pointArr = str.Split(';');
                    foreach (string s in pointArr)
                    {
                        if (s.Contains(","))
                        {
                            Point point = getPoint(s);
                            pList.Add(point);
                        }
                    }
                }
                return pList;
            }
            catch (Exception ex)
            {
               
                return null;
            }
        }
    }

 

 

转载于:https://www.cnblogs.com/nygfcn1234/p/3165065.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值