Bresenham's line algorithm

Hi all, I’ve just found a Javascript implementation of the Bresenham’s line algorithm and I’m just trying to understand what it’s actually doing and was wondering whether anybody would mind giving me a brief explanation about a few lines of it.

Here’s the algorithm itself:


function renderLine(x0, y0, x1, y1) {
    var xx0 = x0;
	var yy0 = y0;
	var xx1 = x1;
	var yy1 = y1;
	
	var dx = Math.abs(xx1 - xx0);
	var dy = Math.abs(yy1 - yy0);
	
	var stepx, stepy;
	if(xx0 < xx1) stepx = 1;
	else stepx = -1;
	if(yy0 < yy1) stepy = 1;
	else stepy = -1;
	
	var err = dx - dy;
	while(true) {
		renderPoint(xx0, yy0);
		if(xx0 == xx1 && yy0 == yy1) break;
		[b]var e2 = err << 1;
		if(e2 > -dy) {
			err -= dy;
			xx0 += stepx;
		}
		if(e2 < dx) {
			err += dx;
			yy0 += stepy;
		}[/b]
	}
}

The section I’m having difficulty getting my head around is emboldened. I understand from reading the wikipedia page on the different methods of implementing it that you create an error variable and loop through every value between the two points X components, then you add the slope of the line to the error until it reaches over 0.5 and then you increase the Y component and subtract 1 from the error. I also understand that this is more or less what’s happening above using integer arithmetic rather than floating point arithmetic although I’m not entirely sure why error needs to be multiplied by 2 or what the significance of this being over -dy or under dx is. Forgive my noobiness - I’m just stuck with this. :stuck_out_tongue: If anyone could explain this I’d be grateful.

Thanks

That’s the simple implementation which has different cases to handle shallow lines (iterate over x conditionally changing y) and steep lines (iterate over y conditionally changing x). This code is considerably cleverer: it combines the cases into a single loop using multiple tests.

[quote=“Porlus,post:1,topic:44225”]
You mentioned that in the simple implementation you need to compare the error to 0.5. To do that in integers you compare twice the error to 1.

[quote=“Porlus,post:1,topic:44225”]
That’s where it gets really clever.

To analyse a loop you want to ask three main questions: does the loop always do something? What are its invariants? Does it stop?

In this case, since dx and dy are both non-negative by design, the only way to fail both tests e2 > -dy and e2 < dx if is e2 == -dy == dx == 0, in which case we will certainly hit the break before getting to either of the tests. Since dx and dy don’t change, we conclude that the loop always does something.

For the invariants, look at the changes. err -= dy; xx0 += stepx; induces the invariant err * stepx + xx0 * dy = constant. err += dx; yy0 += stepy; induces the invariant err * stepy - yy0 * dx = constant. You can look at the values before entering the loop to find out what the constants are.

I hope that gets you on the right track.

Sorry,

was incorrect. Since both of them affect err, it’s necessary to consider them together. Track through enough algebra and I think you find that the true loop invariant is err = stepx * (x1 - x0 + dy * (x0 - xx0)) - stepy * (y1 - y0 + dx * (y0 - yy0))

I personally prefer to use the atan formula as i personally find it easier to implement and is much neater.


double x = 0;
double y = 0;
double angle = atan2(targetx,targety);
double nextx = sin(angle);
double nexty = cos(angle);
for(int i = 0; i < linelength;i++){
     x += nextx;
     y+= nexty;
     render(int(x),int(y),pic);

simple to use if you want to use an angle you can just use atan(angle) (it returns a decimal value used for the sin methods so do not use .todegrees():wink: also note to do it from a specific point say the middle of the screen you would need to account for that in the targetx,targety by taking away the x and y position because the calculation occurs around point(0,0)

He’s asking about the line algorithm, not a algorithm based on trig.

The error is basically the amount of steps a certain component of the vector needs to take to reach the end goal. So, for instance if you have a point at 3,1 and you start at 0,0 you need to increase the x by 3 and the y by 1. This is how the algorithm can tell what component needs to b increased and when. Its really a very clever algorithm.

Hey guys, thanks for the replies and sorry to resurrect an old topic, but I just have one quick question about the floating point method. :slight_smile: Here’s the article I’m referring to again:

My question is, after the error exceeds 0.5 and the Y component is increased, why is the error decreased by 1? I understand why it’s decreased, but what is the significance of decreasing it by 1 exactly?

Ah, I see why it’s 1. :x Sorry, didn’t read the preceding paragraph clearly enough.