Log of rendering a lot of curves in WPF Canvas
.
I have a customized canvas inheriting Canvas
and overrides the OnRender(DrawingContext dc)
method. And I need to draw some curves. I have precomputed the points on the curve and stored them in a List<Tuple<double, double>>
because I am drawing different parts of the curve at different time. So, it is natural to come up with
1 | for (int i = start; i <= end; ++i) |
However this approach is slow and CPU-intensive as hell. Guess what? I had a naive test and wrote
1 | var gstr = new StringBuilder(); |
Basically the code constructs a Path Markup, parse it to a Geometry object and draw it. It looks terrible - that’s a lot of string operations… but it is actually very, very fast. In fact, much faster than the multiple DrawLine
calls.
One possible optimization is to, apparently, precompute all the Geometry objects, although it may result in worse performance because that’s a lot of objects. So I’ll just stick to this construct-on-the-fly thing at the moment :P