tags:

views:

65

answers:

3

hello i am using c# and i want to find the if a polygon (mostly a triangle) is given to me and i have to find a given point exists in the given polygon or not i want to know that is there any function in c# that can do it for me or any efficient algo to do so??

polygons are represented in 2D plane by XY points the given point is also represented by XY points

thanx in advance.

A: 

There are a couple of techniques to do this - the same side technique and the barycentric technique. Check out this link which explains the two in sufficient detail.

tathagata
A: 

See http://en.wikipedia.org/wiki/Point_in_polygon for a first reference. As I mentioned in my comment, triangles would be much simpler.

chiccodoro
+1  A: 

You need to use Graphics.IsVisible(Point p). It indicates whether the point specified by a pair of coordinates is contained within the visible clip region of this Graphics object.

Sample from MSDN :

public void IsVisiblePoint(PaintEventArgs e)
{
   // Set clip region.
   Region clipRegion = new Region(new Rectangle(50, 50, 100, 100));
   e.Graphics.SetClip(clipRegion, CombineMode.Replace);
   // Set up coordinates of points.
   int x1 = 100;
   int y1 = 100;
   int x2 = 200;
   int y2 = 200;
   Point point1 = new Point(x1, y1);
   Point point2 = new Point(x2, y2);
   // If point is visible, fill ellipse that represents it.
   if (e.Graphics.IsVisible(point1))
   e.Graphics.FillEllipse(new SolidBrush(Color.Red), x1, y1, 10, 10);
   if (e.Graphics.IsVisible(point2))
   e.Graphics.FillEllipse(new SolidBrush(Color.Blue), x2, y2, 10, 10);
}
Incognito
Is it possible to use GraphicsPath instead of Rectangle? I think those can be used to represent triangles. A rectangle is a bit limiting and probably not what he wants. The proper solution should handle triangulated polygons.
mizipzor
Yes it is possible. The sample is on rectangles, but for any polygon GraphicsPath can be used.
Incognito