# Line rasterization problem

## Recommended Posts

I'm writing a small software renderer as an experiment. I have made it so far as line rasterisation and i'm already having troubles. Here's an image: http://img515.imageshack.us/img515/4369/problemcg4.jpg It seems like the same vertex doesn't have the same screen position for two or more lines. Obviously, there's an error stacking up (and a big one) but I can't seem to find it. Here's code: steep := abs(fry2 - fry1) > abs(frx2 - frx1);
  if steep then
begin
//  Swaps
aux := frx1; frx1 := fry1; fry1 := aux;
aux := frx2; frx2 := fry2; fry2 := aux;
end;

if (frx1 > frx2) then
begin
//  Swaps
aux := frx1; frx1 := frx2; frx2 := aux;
aux := fry1; fry1 := fry2; fry2 := aux;
end;

rx1 := Round(frx1);
rx2 := Round(frx2);

cs := frx2 - frx1;

deltax := Round(frx2 - frx1);
deltay := Round(Abs(fry2 - fry1));
error := -deltax div 2;
ry := Round(fry1);
if (fry1 < fry2) then ystep := 1 else ystep := -1;
for rx := rx1 to rx2 do
begin
cs2 := (rx - rx1) / cs;
cs3 := 1 - cs2;

if steep then
PlotFragment(ry, rx)
else
PlotFragment(rx, ry);

error := error + deltay;
if (error > 0) then
begin
ry := ry + ystep;
error := error - deltax;
end;
end;

This is an optimised bresenham's algorithm that I found on wikipedia. Any ideeas ? PS: I seem getting the same error with a DDS algorithm !

##### Share on other sites
The problem is the rounding to integer coordinates. Depending on whether the line is x-major or y-major (steep), the coordinates get rounded differently.

One solution is to round all coordinates (x and y) to integers first. That way you're guaranteed that every line uses the same endpoint coordinates.

Once you've fixed that you can further improve quality with sub-pixel accuracy. Instead of rounding the coordinates to integers, round them to fixed-point numbers with about 4 bits below the digit. You can keep using Bresenham's algorithm for rasterization, but you need to compute the correct starting values and step values...

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978790

• 9
• 21
• 14
• 12
• 42