Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Bresenham's line algorithm  (Read 959 times) 0 Members and 1 Guest are viewing this topic.
Porlus

Junior Member

 « Posted 2013-09-18 21:28:15 »

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:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30 `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. If anyone could explain this I'd be grateful.

Thanks
pjt33
 « Reply #1 - Posted 2013-09-19 09:22:25 »

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.

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.

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

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.

or what the significance of this being over -dy or under dx is.

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.

pjt33
 « Reply #2 - Posted 2013-09-19 15:48:44 »

Sorry,

err -= dy; xx0 += stepx; induces the invariant err * stepx + xx0 * dy = constant. err += dx; yy0 += stepy; induces the invariant err * stepy - yy0 * dx = constant.

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))
lcass
 « Reply #3 - Posted 2013-09-19 20:27:18 »

I personally prefer to use the atan formula as i personally find it easier to implement and is much neater.
 1  2  3  4  5  6  7  8  9 `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() 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)
opiop65

JGO Kernel

Medals: 154
Projects: 7
Exp: 3 years

JumpButton Studios

 « Reply #4 - Posted 2013-09-20 00:57:56 »

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.

Porlus

Junior Member

 « Reply #5 - Posted 2013-10-15 19:41:19 »

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. Here's the article I'm referring to again:
http://en.wikipedia.org/wiki/Bresenham's_line_algorithm

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?
Porlus

Junior Member

 « Reply #6 - Posted 2013-10-15 21:13:08 »

Ah, I see why it's 1. :x Sorry, didn't read the preceding paragraph clearly enough.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 BurntPizza (25 views) 2014-09-19 03:14:18 Dwinin (39 views) 2014-09-12 09:08:26 Norakomi (68 views) 2014-09-10 13:57:51 TehJavaDev (93 views) 2014-09-10 06:39:09 Tekkerue (47 views) 2014-09-09 02:24:56 mitcheeb (68 views) 2014-09-08 06:06:29 BurntPizza (51 views) 2014-09-07 01:13:42 Longarmx (38 views) 2014-09-07 01:12:14 Longarmx (44 views) 2014-09-07 01:11:22 Longarmx (40 views) 2014-09-07 01:10:19
 BurntPizza 36x Riven 18x Rayvolution 18x basil_ 16x ags1 16x princec 16x KevinWorkman 15x LiquidNitrogen 12x kevglass 11x deathpat 11x nsigma 11x theagentd 11x HeroesGraveDev 8x The Lion King 7x Gibbo3771 6x cylab 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org