0

I need to work with concave polygons. Basically I need to know their width and height, a method to know if a point is inside the polygon and possibly x,y coordinates of the rectangle that circumscribes it (so I can easily implement drag&drop function). What can I use?

I could define a custom Polygon type, but I would have problems to implement the "Contains" classic method, considering polygons are concave.

Suggests?

Frank Lioty
  • 949
  • 3
  • 10
  • 17
  • Have you included the System.windows.shapes namespace? What error are you getting when you try to instantiate a new polygon? "doesn't work" is too vague. – DGH Jun 12 '12 at 22:23
  • when I try to instantiate the polygon: "Type Polygon not defined" when I try to include System.Windows.Shapes: "namespace 'Shapes' not defined" – Frank Lioty Jun 12 '12 at 22:30
  • 3
    The `System.Windows.Shapes.Polygon` is not a mathematical model of a polygon. Rather, it is a UI element in WPF. You may be able to use it as a mathematical model, but that's not what it was designed for. Would you use a `Panel` as a rectangle? – Kendall Frey Jun 12 '12 at 23:00
  • Understood, so it doesn't exist a Polygon class like the Rectangle one. Damn, are you talking to me that I have to create myself a Polygon type? No idea how to implement the Contains method, considering it's a concave one... – Frank Lioty Jun 12 '12 at 23:19
  • Sounds to me like you want [`System.Drawing.Region`](http://msdn.microsoft.com/en-us/library/system.drawing.region.aspx)... – ildjarn Jun 12 '12 at 23:56

3 Answers3

1

You can use System.Drawing.Drawing2D.GraphicsPath:

GraphicsPath path = new GraphicsPath();
path.AddPolygon(new[] {new Point(0,1), new Point(5,8), new Point(2,4)});

bool isPointInPolygon = path.IsVisible(4, 1);
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
  • Yeah, it works but the GraphicsPath hasn't its x,y coordinates, so polygon ones are not relative to the path but absolute. This is going to complicate a drag&drop operation, even considering I have to save all these data in a XML file. Surely it's more easy than update all polygon coordinates each time. I can't even inherit the class to define a new one for my aims... – Frank Lioty Jun 13 '12 at 11:11
  • @FrankLioty, to deal with the absolute coordinates, can't you just translate your test point in the opposite direction to your graphics path's rendering translation and use that point for the hit test against your absolutely defined graphics path? – Simon MᶜKenzie Jun 13 '12 at 23:26
  • not understood. BTW I was implementing drag&drop method changing all coordinates vertex values but it doesn't make a move. I got them with the PathPoints property. – Frank Lioty Jun 14 '12 at 11:52
  • @FrankLioty, if you don't want to extract the points and offset them each time you do a drag/drop, you can use `GraphicsPath.Transform` to perform a translation tranformation on the path - `Matrix m = new Matrix();m.Translate(offsetX,offsetY);path.Transform(m);` – Simon MᶜKenzie Jun 14 '12 at 21:56
1

You can use the Shape and in particolar the Polygon Class, interact with these object is particularly simple, but if you are dealing with thousands of edges I suggest to use the DrawingVisual Class, a visual object that can be used to render vector graphics on the screen, and its RenderOpen method.

DrawingVisual does not provide event handling, so if with Shape you can use events to interact with edges, with DrawingVisual you need to implement Hit-Testing.

With a Polygon to know Height and Width simply use the element properties.

If you want to check if a Point is inside your polygon you can use the InputHitTest Method or esle the VisualTreeHelper.HitTest Method:

let res = yourPolygon.InputHitTest(new Point(x, y))
let res = VisualTreeHelper.HitTest(yourPolygon, new Point(x, y))

In conclusion: if you seek an elegant method to check if a point is inside a polygon take a look at this answer.

Community
  • 1
  • 1
gliderkite
  • 8,828
  • 6
  • 44
  • 80
0

There are some nice routines to find out if a point is within a polygon. Check out http://www.softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm

Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35