Draw line with varying thickness

2.2k views Asked by At

I want to draw one continuous line/curve with varying thickness using C#. If I set the width of the Pen as fixed, thickness will not vary. How is it possible to change the thickness of line as it is drawn? Any help is appreciated.

2

There are 2 answers

0
Roy Dictus On

You can only vary the thickness of the line if you draw the line point by point, and then vary the thickness of the points.

To find out how to draw lines point by point, look up line drawing algorithms if you don't have them handy. One example for drawing straight lines is Bresenham's algorithm. You can find out more about that at http://en.wikipedia.org/wiki/Bresenham's_line_algorithm.

Then when you plot a dot, you can choose the thickness of the dot. So instead of just drawing a single pixel, you draw a circle with radius r, where r is the thickness of the line you want at that position in the line.

3
olydis On

General approach: split the line into segments/points and make several draw calls with different pens (as mentioned in other posts).

Now for curves - if you do not want to reimplement their algorithms, you might wanna use GraphicsPath to convert arbitrary paths to line segments (with sufficient resolution).

  1. add your path (lines, curves, beziers, ...) to a fresh GraphicsPath instance
  2. call Flatten to let GraphicsPath perform the magic (= conversion to lines only)
  3. iterate over PathPoints to get the endpoints of corresponding line segments
  4. subdivide them further until they are short enough for your "varying" pen strategy