<p>The first thing to make note of is that the algorithm listed in the question is the integer algorithm. So no floating point math and the plotting increments by +1 or -1 in the x and y directions.</p>
<p>The function takes for input the line starting position (x0,y0) and ending position (x1,y1). The starting position variables(x0,y0) are reused when drawing the line.</p>
<p>Using inputs the algorithm computes the x and y increment directions. These values get held in variables sx and sy and are always either +1 or -1. They can be negative or positive depending on the line. For example we should get consistent results whether or not (x0,y0) and (x1,y1) are switched.</p>
<pre><code>sx = x0 &lt; x1 ? 1 : -1; // step x left or right
sy = y0 &lt; y1 ? 1 : -1; // step y left or right
</code></pre>
<p>To actually draw the line we need to determine whether to step in the x direction, the y direction or both x and y (ie diagonally). (that is why the algorithm uses two separate if statements instead of an if then else.) To step in the x direction it is simply:</p>
<pre><code> x0 = x0 + sx; // move x0 in the x direction setup in sx
 y0 = y0 + sy; // move y0 in the y direction setup in sy
</code></pre>
<p>Our end condition happens when x0==x1 or y0==y1 or (x0,y0) == (x1,y1). This is handled in the algorithm by the &quot;if(..) then break;&quot; statements.</p>
<pre><code>    if x0 == x1 &amp;&amp; y0 == y1 break; // break the while true loop
    if x0 == x1 break; // only x meets the break condition
    if y0 == y1 break  // only y meets the break condition
</code></pre>
<p>Finally we need to be able to decide when to step in the x direction or the y direction or both. That is where the variables e2, dx, dy, and error come in.</p>
<p>Notice that dx is always positive ie <em>dx = abs(x1-x0)</em> and dy is always negative ie <em>dy = -abs(y1-y0)</em> ... and error starts out as the sum of a positive and negative number ie <em>error = dx+dy</em>. The algorithm adds dx(which is postive) to error when the line is plotted in the x direction, and it adds dy(which is negative) to error when it is plotted in the y direction.</p>
<pre><code>dx = abs(x1-x0);  // distance x needs to move in increments of 1
dy = -abs(y1-y0); // negative distance y needs to move in increments of 1
error = dx + dy;  // Start state for difference between dx and dy
e2 = error * 2;   // look ahead
</code></pre>
<p>If for example the line is diagonal such as the line (0,0):(5,5). The value of error will be zero, and so will e2. Making both if statements true for each step. And since dx is added and dy is subtracted both if statements will remain true until x0,y0==x1,y1!</p>
<p>dx and dy can be thought of as the numerator and denominator of the slope of a line. When dx is 3 units bigger then dy, it will increment 3 times for every 1 dy.</p>
<p>So how do we figure out the next step? Simple look ahead one step which is <em>e2=2*error</em> When either x0 or y0 is greater then e2 then they have gone too far, ie they are in an &quot;error state&quot;.</p>
<p>One way to look at this is: As the algorithm plots in the x direction error will increase in the positive direction eventually making e2 greater then dx...which will stop it from plotting in the x direction ie <em>if(e2&lt;=dx)</em> will become false. Also as the algorithm plots in the y direction error will increment in the negative direction eventually making e2 less then dy, ie <em>if(e2&gt;=dy)</em> will become false.</p>
<p>e2 isn't being pulled out of the air, it is the calculation of the next step.</p>
<p>So that's a pretty long description...the way I originally came to understand it was to code it up in C and then step through various lines in the debugger looking at the values until that light bulb moment happened for me.</p>

CC BY-SA 4.0
2022-12-31T23:31:18.520Z
2023-01-02T20:56:50.227Z
2023-01-02T20:56:50.227Z
pmw1234
<p>the wikipedia page is quite straightforward: <a href="https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm</a></p>

CC BY-SA 4.0
2023-01-28T16:19:06.243Z
2023-01-28T16:19:06.243Z
Fabrice NEYRET
<p>I've been trying to wrap my head around this algorithm, and I need it for my drawing function. But I can't seem to understand it.</p>
<p>The <a href="https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm" rel="nofollow noreferrer">Wikipedia page</a> gives this piece of code right at the end:</p>
<pre><code>plotLine(x0, y0, x1, y1)
    dx = abs(x1 - x0)
    sx = x0 &lt; x1 ? 1 : -1
    dy = -abs(y1 - y0)
    sy = y0 &lt; y1 ? 1 : -1
    error = dx + dy
    
    while true
        plot(x0, y0)
        if x0 == x1 &amp;&amp; y0 == y1 break
        e2 = 2 * error
        if e2 &gt;= dy
            if x0 == x1 break
            error = error + dy
            x0 = x0 + sx
        end if
        if e2 &lt;= dx
            if y0 == y1 break
            error = error + dx
            y0 = y0 + sy
        end if
    end while
</code></pre>
<p>So it starts by getting the <code>x</code> and <code>y</code> distance between the end points.<br />
It then uses <code>sx</code>/<code>sy</code> to change the computation depending on the direction.<br />
But then it introduces the error value, and I can't wrap my head around how it can mix apples and oranges.</p>
<p>I do kind of superficially understand that this value flip flops as you go along the line and determines whether the next pixel is above or below ... but I just can't understand how you can mix <code>x</code> and <code>y</code>.</p>
<p>Secondly, I take issue with e2 and its nonchalant doubling of the error value for some reason.<br />
And then the value is used and compared to <code>dx</code> and <code>dy</code>.</p>
<p>I have researched this for a week. I found many sites showing the code, but none have had an explanation that would make me go: &quot;oh, now I understand.&quot;</p>
<p>I'm in need of an explanation that is in simple terms without too much math notation. It's just coordinates and a line.</p>

<mathematics><algorithm><computational-geometry>
Can you explain to me the Bresenham's line algorithm in simple terms?
sneaker
# Can you explain to me the Bresenham's line algorithm in simple terms?

(Asked by: username_0 on 2022-12-31)

I've been trying to wrap my head around this algorithm, and I need it for my drawing function. But I can't seem to understand it.

The [Wikipedia page](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm) gives this piece of code right at the end:

```
plotLine(x0, y0, x1, y1)
    dx = abs(x1 - x0)
    sx = x0 < x1 ? 1 : -1
    dy = -abs(y1 - y0)
    sy = y0 < y1 ? 1 : -1
    error = dx + dy
    
    while true
        plot(x0, y0)
        if x0 == x1 && y0 == y1 break
        e2 = 2 * error
        if e2 >= dy
            if x0 == x1 break
            error = error + dy
            x0 = x0 + sx
        end if
        if e2 <= dx
            if y0 == y1 break
            error = error + dx
            y0 = y0 + sy
        end if
    end while

```

So it starts by getting the `x` and `y` distance between the end points.  
It then uses `sx`/`sy` to change the computation depending on the direction.  
But then it introduces the error value, and I can't wrap my head around how it can mix apples and oranges.

I do kind of superficially understand that this value flip flops as you go along the line and determines whether the next pixel is above or below ... but I just can't understand how you can mix `x` and `y`.

Secondly, I take issue with e2 and its nonchalant doubling of the error value for some reason.  
And then the value is used and compared to `dx` and `dy`.

I have researched this for a week. I found many sites showing the code, but none have had an explanation that would make me go: "oh, now I understand."

I'm in need of an explanation that is in simple terms without too much math notation. It's just coordinates and a line.

**Tags:** mathematics, algorithm, computational-geometry


---

## 2 answers

### Answer by username_1 on 2022-12-31. Score: 3

The first thing to make note of is that the algorithm listed in the question is the integer algorithm. So no floating point math and the plotting increments by +1 or -1 in the x and y directions.

The function takes for input the line starting position (x0,y0) and ending position (x1,y1). The starting position variables(x0,y0) are reused when drawing the line.

Using inputs the algorithm computes the x and y increment directions. These values get held in variables sx and sy and are always either +1 or -1. They can be negative or positive depending on the line. For example we should get consistent results whether or not (x0,y0) and (x1,y1) are switched.

```
sx = x0 < x1 ? 1 : -1; // step x left or right
sy = y0 < y1 ? 1 : -1; // step y left or right

```

To actually draw the line we need to determine whether to step in the x direction, the y direction or both x and y (ie diagonally). (that is why the algorithm uses two separate if statements instead of an if then else.) To step in the x direction it is simply:

```
 x0 = x0 + sx; // move x0 in the x direction setup in sx
 y0 = y0 + sy; // move y0 in the y direction setup in sy

```

Our end condition happens when x0==x1 or y0==y1 or (x0,y0) == (x1,y1). This is handled in the algorithm by the "if(..) then break;" statements.

```
    if x0 == x1 && y0 == y1 break; // break the while true loop
    if x0 == x1 break; // only x meets the break condition
    if y0 == y1 break  // only y meets the break condition

```

Finally we need to be able to decide when to step in the x direction or the y direction or both. That is where the variables e2, dx, dy, and error come in.

Notice that dx is always positive ie *dx = abs(x1-x0)* and dy is always negative ie *dy = -abs(y1-y0)* ... and error starts out as the sum of a positive and negative number ie *error = dx+dy*. The algorithm adds dx(which is postive) to error when the line is plotted in the x direction, and it adds dy(which is negative) to error when it is plotted in the y direction.

```
dx = abs(x1-x0);  // distance x needs to move in increments of 1
dy = -abs(y1-y0); // negative distance y needs to move in increments of 1
error = dx + dy;  // Start state for difference between dx and dy
e2 = error * 2;   // look ahead

```

If for example the line is diagonal such as the line (0,0):(5,5). The value of error will be zero, and so will e2. Making both if statements true for each step. And since dx is added and dy is subtracted both if statements will remain true until x0,y0==x1,y1!

dx and dy can be thought of as the numerator and denominator of the slope of a line. When dx is 3 units bigger then dy, it will increment 3 times for every 1 dy.

So how do we figure out the next step? Simple look ahead one step which is *e2=2\*error* When either x0 or y0 is greater then e2 then they have gone too far, ie they are in an "error state".

One way to look at this is: As the algorithm plots in the x direction error will increase in the positive direction eventually making e2 greater then dx...which will stop it from plotting in the x direction ie *if(e2<=dx)* will become false. Also as the algorithm plots in the y direction error will increment in the negative direction eventually making e2 less then dy, ie *if(e2>=dy)* will become false.

e2 isn't being pulled out of the air, it is the calculation of the next step.

So that's a pretty long description...the way I originally came to understand it was to code it up in C and then step through various lines in the debugger looking at the values until that light bulb moment happened for me.


---

### Answer by username_2 on 2023-01-28. Score: -1

the wikipedia page is quite straightforward: <https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm>


---

