views:

565

answers:

4

I am currently using Bresenham's algorithm to draw lines but they are (of course) one pixel in thickness. My question is what is the most efficient way to draw lines of arbitrary thickness?

The language I am using is C.

+3  A: 

I think the best way is to draw a rectangle rather than a line since a line with width is a two dimensional object. Tring to draw a set of parallel lines to avoid overdraw (to reduce write bandwidth) and underdraw (missing pixels) would be quite complex. It's not too hard to calculate the corner points of the rectangle from the start and end point and the width.

Skizz
+4  A: 

Here is a paper and Delphi implementation of a modified version of Bresenham's algorithm for drawing thickened lines.

You may also want to take a look at Anti-Grain Geometry, a library for high-quality and high-performance software rendering of 2D graphics. Take a look at the demo page to get an idea of what it can do.

Martin B
+1  A: 

Some simple routes to use:

  1. for any width n where n is odd. for any point p plotted also plot the points above/below it for n/2 (if the line is > 45 degree angle draw side to side instead).
    • not really a proper line of the right thickness, more like an italic pen, but very fast.
  2. for start point p(x,y) pick the points t0 and b such that they are centred on p but n pixels apart. for the end point do the same resulting in t1 b1. Draw lines from t0 -> t1, t1->b1, b1 -> t0, b0 -> t1. Fill in the resulting rectangle.
    • The trick here is picking the points such that they appear orthogonal to the path direction.
  3. for each point p on the line instead of drawing a point draw a circle.
    • This has the advantage of making the end points 'clean' no matter what the orientation.
    • there should be no need to render any circles in solid except for the first.
    • somewhat slow
ShuggyCoUk
+1  A: 

I assume that you would draw horizontal spans from one bounding line to another, and compute the x-value of each of the lines by Bresenham's method as you go (in a single loop).

Haven't tried it.

The end points may need some attention, lest they look oddly cut-off.

dmckee
yes this was the strategy that i had in mind too and i agree that the end-points will need some thought.
banister