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.
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;
                    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.
Updated 27-Nov-11 18:29pm
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:

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;.


Share this answer
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..

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