判断点是否在矩形框、多边形内

采用下面两个方法可以简单判断矩形框、多边形是否包含某个点。

     /// <summary>
        /// 判断点是否在矩形框内
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static bool PtInRect(Point pt, RectangleF rect)
        {
            GraphicsPath path = new GraphicsPath();
            Region region = new Region();
            path.Reset();
            //构建多边形
            path.AddRectangle(rect);
            region.MakeEmpty();
            region.Union(path);
            //判断点是否在多边形里
            bool rlt = region.IsVisible(pt);
            region.Dispose();
            path.Dispose();
            return rlt;
        }
        /// <summary>
        /// 判断点是否在多边形内
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="pts"></param>
        /// <returns></returns>
        public static bool PtInPolygon(Point pt, Point[] pts)
        {
            GraphicsPath path = new GraphicsPath();
            Region region = new Region();
            path.Reset();
            //构建多边形
            path.AddPolygon(pts);
            region.MakeEmpty();
            region.Union(path);
            //判断点是否在多边形里
            bool rlt = region.IsVisible(pt);
            region.Dispose();
            path.Dispose();
            return rlt;
        }

 

posted @ 2020-08-12 10:14  胖达没有海  阅读(908)  评论(0编辑  收藏  举报