0

I want the user to define a polygon by using his mouse and clicks to define the points. The second task is once i have a set of defined polygons i want to check if some randomly generated points lie within any of those polygons.

I am working on C# .net platform. Thanks.

olive
  • 2,983
  • 5
  • 21
  • 20
  • Sounds like a good goal. What have you tried so far? – Andrew Cooper Apr 07 '11 at 04:35
  • Your question makes it look like you haven't even tried to do it yourself - I'm sure you have but you should give us some detail about what you've tried and we'll try to get you on the right path. – Greg Sansom Apr 07 '11 at 04:49
  • I have thought about the approach though not exactly tried it yet because am not sure of the methods that can be used. Well am thinking of storing the points in a list. Then using the list elements to define a polygon. Then checking if a particular point is within the various polygons. Isnt there any inbuilt function that checks if a point lies in a polygon,VC++ had one such.Convex and concave is not a issue because the polygon would be defined by the order in which user defines them. Thanks.. – olive Apr 07 '11 at 05:04
  • If VC++ had one then you could easily PInvoke to the VC++ function. – rein Apr 07 '11 at 05:53

2 Answers2

2

This is not a simple task. For instance, you should analyze and sort the set of points as Convex or Concave Hull. You can use the following links as resources:

http://ubicomp.algoritmi.uminho.pt/local/concavehull.html

http://marknelson.us/2007/08/22/convex/

Is there an efficient algorithm to generate a 2D concave hull?

http://courses.csail.mit.edu/6.854/06/scribe/s26-randomIC.pdf

http://softsurfer.com/Archive/algorithm_0109/algorithm_0109.htm

Community
  • 1
  • 1
Svetlin Ralchev
  • 614
  • 6
  • 5
0

Old thread, but straight forward solution. This is actually pretty simple if you make full use of the Graphics and Drawing2D namespaces.

GraphicsPath.AddLines( Point[] )

Graphics.DrawPath( Drawing2D.GraphicsPath )

Graphics.FillPath( Drawing2D.GraphicsPath )

GraphcisPath.CloseFigure( )

So you'd simply create a new path, then call AddLines passing your array of points, and then call CloseFigure.

If you want to stroke it with a pen, use DrawPath. If you want to fill it with a brush, use FillPath.

Hit test for any points inside the polygon is simple as well, you would use Drawing.Region( Drawing2D.GraphicsPath ) and any overload of Region.IsVisible that allows you to specify a position and graphics object.

Kogitsune
  • 415
  • 4
  • 8