Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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?


EDIT:
I tried this one, but it never returns true for PointInPolygon
C#
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;
        }
Posted
Updated 5-Jul-11 0:00am
v3

1 solution

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 6:03am    
Did you check it up? Looks to simple to be true...
--SA
Prerak Patel 5-Jul-11 6:05am    
I didn't check that. OP may try.
It is the accepted answer. Moreover, there is a link to article in answer(C++). OP needs to get logic and try.
velvet7 5-Jul-11 14:05pm    
Thanks for the answer, unfortunately it doesn't work:(
Sergey Alexandrovich Kryukov 6-Jul-11 17:40pm    
Thank you for checking it up. Please see the discussion on the similar question:
http://www.codeproject.com/Answers/221054/Behaviour-of-CRgn-class#answer2

Please see "solutions" by C Pallini and mine. Please also see the arguments between us. What do you think?

Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900