Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (564) Games in Android Showcase (151) games submitted by our members Games in WIP (605) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Bresenham's line algorithm  (Read 1224 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: 39
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: 39
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))
 Games published by our own members! Check 'em out!
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: 165
Projects: 7
Exp: 4 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 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.

 theagentd (9 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (43 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (35 views) 2015-03-13 17:50:16 Olo (40 views) 2015-03-13 17:47:07 ClaasJG (30 views) 2015-03-10 11:36:42 ClaasJG (39 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 LiquidNitrogen 23x BurntPizza 21x KevinWorkman 21x basil_ 20x Riven 18x EgonOlsen 17x theagentd 16x princec 12x Roquen 12x Varkas 10x SHC 9x NegativeZero 9x teletubo 7x Ecumene 7x nsigma 7x wessles 7x
 How 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:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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