Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (593) Games in Android Showcase (168) games submitted by our members Games in WIP (646) 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 1385 times) 0 Members and 1 Guest are viewing this topic.
Porlus

Junior Devvie

 « 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

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « 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

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « 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
 « 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 Devvie

 « 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 Devvie

 « 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.

 Riven (21 views) 2015-07-03 11:22:16 deepthought (46 views) 2015-06-30 15:39:44 deepthought (49 views) 2015-06-30 15:39:09 deepthought (62 views) 2015-06-30 15:36:52 Za\'Anzabar (33 views) 2015-06-29 05:44:54 TritonDreyja (45 views) 2015-06-24 17:10:40 CopyableCougar4 (44 views) 2015-06-23 00:34:45 BurntPizza (48 views) 2015-06-21 20:36:46 cookiecompiler (90 views) 2015-06-11 15:42:53 cookiecompiler (54 views) 2015-06-11 15:41:14
 princec 30x wessles 23x BurntPizza 19x CopyableCougar4 19x opiop65 18x Riven 15x nsigma 15x EgonOlsen 14x KaiHH 12x ags1 11x SauronWatchesYou 11x theagentd 11x KevinWorkman 11x sunburn 11x NegativeZero 10x DavidBVal 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 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