获取多边形内的所有点(在C#中) [英] Get all Points within a Polygon (in C#)

查看:881
本文介绍了获取多边形内的所有点(在C#中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我想获取多边形内的所有点,所以假设我们有一个正方形:
(0,0)
(2,0)
(2,2)
(0,2)

现在我想得到这个正方形内的所有点,它们是:
(0,0)
(0,1)
(0,2)
(1,0)
(2,0)
(1,1)
(1,2)
(2,1)
(2,2)

但是有更多复杂的多边形,是否有一种方法可以使更复杂的多边形得到类似的东西?



我尝试了这个,但对于PointInPolygon它从未返回true

Hello!

I would like to get all the points within a polygon, so lets assume we have a square:
(0,0)
(2,0)
(2,2)
(0,2)

Now I would like to get all the points within this square, these are:
(0,0)
(0,1)
(0,2)
(1,0)
(2,0)
(1,1)
(1,2)
(2,1)
(2,2)

But there are more complex polygons, so is there a method to get something like this for more complex polygons?



I tried this one, but it never returns true for PointInPolygon

public static void GetPoints(List<Point> points)
        {
            if (points.Count == 0)
                return;
            int highestx = points[0].X;
            int highesty = points[0].Y;
            int lowestx = points[0].X;
            int lowesty = points[0].Y;
            for (int i = 0; i < points.Count; i++)
            {
                if (points[i].X > highestx)
                    highestx = points[i].X;
                if (points[i].Y > highesty)
                    highesty = points[i].Y;
                if (points[i].X < lowestx)
                    lowestx = points[i].X;
                if (points[i].Y < lowesty)
                    lowesty = points[i].Y;
            }
            for (int x = lowestx; x < highestx; x++)
            {
                for (int y = lowesty; y < highesty; y++)
                {
                    if (PointInPolygon(new Point(x, y), points))
                    {
                        obstacles[x, y] = 1;
                    }
                }
            }
        }
static bool PointInPolygon(Point p, List<Point> poly)
        {
            Point p1, p2;

            bool inside = false;

            if (poly.Count < 3)
            {
                return inside;
            }

            Point oldPoint = new Point(
            poly[poly.Count - 1].X, poly[poly.Count - 1].Y);

            for (int i = 0; i < poly.Count; i++)
            {
                Point newPoint = new Point(poly[i].X, poly[i].Y);

                if (newPoint.X > oldPoint.X)
                {
                    p1 = oldPoint;
                    p2 = newPoint;
                }
                else
                {
                    p1 = newPoint;
                    p2 = oldPoint;
                }

                if ((newPoint.X < p.X) == (p.X <= oldPoint.X)
                && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X)
                 < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))
                {
                    inside = !inside;
                }

                oldPoint = newPoint;
            }

            return inside;
        }

推荐答案

http: //stackstackflow.com/questions/4243042/c-point-in-polygon [

这篇关于获取多边形内的所有点(在C#中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆