Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 975 times)
0 Members and 1 Guest are viewing this topic.
Offline 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. Tongue If anyone could explain this I'd be grateful.

Thanks
Offline 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.


Offline 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))
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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()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)
Offline 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.

Offline 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. Smiley 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?
Offline 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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!