<p>Like Rahul said that algorithm is only in the case where <span class="math-container">$0 \lt \Delta y \lt \Delta x$</span>. </p>

<p>So you need to adjust the coordinates to fit within that. </p>

<p>There are 8 cases:</p>

<ol>
<li><span class="math-container">$0 \lt \Delta y \lt \Delta x$</span> the normal base case</li>
<li><span class="math-container">$0 \lt \Delta x \lt \Delta y$</span> swap x and y except for the drawPixel call.</li>
<li><span class="math-container">$\Delta x \lt 0 \lt \Delta y$</span> and <span class="math-container">$|\Delta y &lt; \Delta x|$</span> use case 1 but negate <span class="math-container">$\Delta x$</span> and instead of counting from x0 to x1 you count from x1 to x0</li>
<li><span class="math-container">$\Delta x \lt 0 \lt \Delta y$</span> and <span class="math-container">$|\Delta x \lt \Delta y|$</span> use case 2 but negate <span class="math-container">$\Delta x$</span> and instead of counting from x0 to x1 you count from x1 to x0</li>
</ol>

<p>The remaining cases have <span class="math-container">$\Delta y \lt 0$</span> so swap the 2 points and it will fall within the previous 4 cases.</p>

CC BY-SA 4.0
2019-05-29T11:23:19.093Z
2019-05-29T11:23:19.093Z
ratchet freak
<p>I am using the Bresenham algorithm from wikipedia:</p>

<pre><code> plotLine(x0,y0, x1,y1)
      dx = x1 - x0
      dy = y1 - y0
      D = 2*dy - dx
      y = y0

     for x from x0 to x1
        plot(x,y)
        if D &gt; 0
           y = y + 1
           D = D - 2*dx
        end if
        D = D + 2*dy
</code></pre>

<p>I have the following two points:</p>

<pre><code>P1 = (10, 120) and P2 = (18, 117)
</code></pre>

<p>I wrote a C program to run the algorithm and I got as output:</p>

<pre><code>ePlot the following points: 
10, 120
11, 120
12, 120
13, 120
14, 120
15, 120
16, 120
17, 120
</code></pre>

<p>Which is totally wrong, is there any other version of this algorithm which works for this scenario or it does not exist?</p>

<algorithm><computational-geometry><offline>
Why the Bresenham's algorithm does not work for those two points?
Ardit Shala
# Why the Bresenham's algorithm does not work for those two points?

(Asked by: username_0 on 2019-05-25)

I am using the Bresenham algorithm from wikipedia:

```
 plotLine(x0,y0, x1,y1)
      dx = x1 - x0
      dy = y1 - y0
      D = 2*dy - dx
      y = y0

     for x from x0 to x1
        plot(x,y)
        if D > 0
           y = y + 1
           D = D - 2*dx
        end if
        D = D + 2*dy

```

I have the following two points:

```
P1 = (10, 120) and P2 = (18, 117)

```

I wrote a C program to run the algorithm and I got as output:

```
ePlot the following points: 
10, 120
11, 120
12, 120
13, 120
14, 120
15, 120
16, 120
17, 120

```

Which is totally wrong, is there any other version of this algorithm which works for this scenario or it does not exist?

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


---

## 1 answers

### Answer by username_1 on 2019-05-29. Score: 2

Like Rahul said that algorithm is only in the case where $0 \lt \Delta y \lt \Delta x$.

So you need to adjust the coordinates to fit within that.

There are 8 cases:

1. $0 \lt \Delta y \lt \Delta x$ the normal base case
2. $0 \lt \Delta x \lt \Delta y$ swap x and y except for the drawPixel call.
3. $\Delta x \lt 0 \lt \Delta y$ and $|\Delta y < \Delta x|$ use case 1 but negate $\Delta x$ and instead of counting from x0 to x1 you count from x1 to x0
4. $\Delta x \lt 0 \lt \Delta y$ and $|\Delta x \lt \Delta y|$ use case 2 but negate $\Delta x$ and instead of counting from x0 to x1 you count from x1 to x0

The remaining cases have $\Delta y \lt 0$ so swap the 2 points and it will fall within the previous 4 cases.


---

