Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
I would implement an algorithm which would check if the point is inside or outside or exactly on border of the polygon.

I have make one function but it will return true only when point is inside of polygon. and return false when point is outside of polygon but not return true when point is on border of the polygon..

This function is given below.
C#
public static Boolean PointInPolygon(PointF p, List poly)
        {
            PointF p1, p2;
            bool inside = false;

            if (poly.Count < 3)
            {
                return inside;
            }
            PointF oldPoint = new PointF(

            poly[poly.Count - 1].X, poly[poly.Count - 1].Y);

            for (int i = 0; i < poly.Count; i++)
            {

                PointF newPoint = new PointF(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;
        }

So please tell me any other way or any changed in my function, So it will solve my problem.
Posted
Updated 27-Nov-11 18:29pm
v3
Comments
RaisKazi 26-Nov-11 2:26am    
Edited: 1) Formatting 2) Added "pre" tag.
BillWoodruff 26-Nov-11 5:11am    
My vote of #1: You are using code taken exactly from: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/95055cdc-60f8-4c22-8270-ab5f9870270a/

But you don't bother to cite it, and, you don't even have the input parameters written correctly: "poly" has got to be a List<Point> in your case.

Get to work.
Amir Mahfoozi 27-Nov-11 0:01am    
As I remember from high school a point is in a polygon if sum of the angles that it make with all of the vertexes are zero otherwise it is out of the polygon.

1 solution

I've read your question multiple times now just to make sure that I understood it correctly.
"I would implement an algorithm which would check if the point is inside or outside or exactly on border of the polygon."
In Euclidean geometry in two dimensional space a point will only have these three options you mentioned so rest assured that regardless of the passed parameters all you have to do is return true;.

Cheers!

—MRB
 
Share this answer
 
Comments
Dharmessh12 27-Nov-11 23:24pm    
ohh Realy realy sorry i have not mention here when return true or false..
.i want return true when point is inside or exactly on border of polygon and return false if point is outside of polygon..
thanks

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