简体   繁体   中英

Polygon drawing

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM