How to find out if GraphicsPath contains a dot in C #

I use .NET to draw a diagram, and I want to highlight objects when the user clicks on them. This is easy when the shape is completely contained in the rectangle:

if (figure.Bounds.Contains(p)) // bounds is a rectangle 

But I do not know how to do this if the shape is complex GraphicsPath .

I defined the following GraphicsPath for the drawing (green circle).

GraphicsPath

I want to highlight a shape when the user clicks on it. I would like to know if Point contained in this GraphicsPath .

Any ideas? Thanks in advance.

+6
c # drawing system.drawing
source share
2 answers

I don't know DrawingPath (you probably mean graphics.DrawPath), but GraphicsPath has IsVisible to check if a point is on the way.

 bool isInPath = graphicsObj.IsVisible(point) 
+12
source share

Using both .IsOutlineVisible and .IsVisible together covers the whole thing, the border and inside the border, for this example rectangle, but as you know, GraphicsPath can work for different shapes.

  bool b = gp.IsVisible(point) || gp.IsOutlineVisible(point, pen); 

For this in code

  Rectangle r = new Rectangle(new Point(50, 100), new Size(500, 100)); bool b; // say Point p is set. // say Pen pen is set. using (var gp = new GraphicsPath()) using (var pen = new Pen(Color.Black, 44)) { gp.AddRectangle(r); bool b = gp.IsVisible(point) || gp.IsOutlineVisible(point, pen); } 
+1
source share

All Articles