1

I have an application (WinForms C#) that allows users to draw a line (free draw) to highlight roads of interest on a bitmap taken from Google Maps.

I am using the Mouse Down/Move/Up events to add points to a GraphicsPath object. This works well for straight stretches o road that can be represented by just two points. But when the mouse draws turns with the button down for long periods, a lot of point data is generated.

This is not only inefficient but also a clunky way to draw over curves (specially when the zoom level is too low).

Question:

  • Once the path is complete, how can I programatically reduce the number of points without losing accuracy.
  • Is there a better way to capture point information using some form of geometric construct?
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • 1
    Have a look at the [Ramer–Douglas–Peucker algorithm](http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm) to reduce the number of points. – Clemens Jan 27 '13 at 17:33
  • @Clemens: Thanks. This sounds like exactly what I was looking for for the first point. In your experience, would you have the app process the points as they are being drawn or once after they are done. Considering the stated worst-case complexity of O(n2), it should not be a computational challenge (we probably won't have more than one or two thousand). Your thoughts? – Raheel Khan Jan 27 '13 at 17:44
  • I use the algorithm to reduce the number of points in a calculated route in a map application. Actually I run it multiple times with increasing tolerance until the total number of points is less than a fixed limit (e.g. 500). I do this once after the route has been calculated, and even with very large routes never encountered any serious loss of visual accuracy. I can post my implementation if you like. – Clemens Jan 27 '13 at 18:44
  • @Clemens: A quick implementation and the points are down from 1000 to <100 with a tolerance of 5. That's excellent. However, in sharp turns, some are converted to pointy edges. Do you suppose there is a way to handle this? – Raheel Khan Jan 27 '13 at 18:51
  • And by the way, you should post this as an answer. – Raheel Khan Jan 27 '13 at 18:51
  • I'm not aware of any trick that would increase the number of points in sharp turns. If you know however where the turns are, you could exclude those polyline segments from the reduction. – Clemens Jan 27 '13 at 18:55

1 Answers1

2

A commonly used way to reduce the number of points in a polygonal curve is the Ramer–Douglas–Peucker algorithm. It is perfectly suited for reducing the number of points in a map drawing, e.g. a route.

Clemens
  • 123,504
  • 12
  • 155
  • 268