Thin out points on a `GraphicsPath`

I have a UserControl that draws a graph using a GraphicsPath. This path is created once and a copy of it can be retrieved by GetGraph. I want to be able to select the graph by clicking on it within a certain range, so I created a Region and used it’s IsVisible method. I solved this problem with the code below.

protected override void OnMouseClick(MouseEventArgs e) {     using (System.Drawing.Drawing2D.GraphicsPath graph = GetGraph())     using (Pen p1 = new Pen(Color.Black, 10))     {         p1.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;         graph.Widen(p1);          Region graphRegion = new Region(graph);         isSelected = graphRegion.IsVisible(e.Location.X, e.Location.Y);         OnGraphSelectedChanged(EventArgs.Empty);     }      base.OnMouseClick(e); } 

The issue is that it takes a few seconds for the IsVisible method when the path consists of a lot of points. In my case there are 10’000 points and graph.Widen makes it even more (more than 60’000 points). The graph looks something like this: 1

Since the path is widen by 10 pixel in the above-mentioned method a lot of points might overlap. Is there an elegant method to reduce the amount of points? Sure I could extract every 10th point or so and create a GraphicsPath from them, but this ignores dense point clouds or sharp edges. Like you can use AddCurve on a GraphicsPath to make a nice curve from a few points; but the opposite. I am also open to different approaches that allows me to select a drawn graph.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.