<pre><code>e = (float)(delta_x / delta_y) - 1.0;

e += (float)delta_x / delta_y;
</code></pre>

<p>Either of these lines will cause a divide-by-zero error when <code>delta_y</code> is zero, which is when the line is horizontal (or zero-length). You need to check for this condition and have some special-case code.</p>

<p>As an aside, I don't know why people still teach Bresenham's algorithm to new CG students. It's a good example of an algorithm but it's not very useful knowledge in itself: you're unlikely to need to know it unless it's for a job interview.</p>

CC BY-SA 4.0
2018-08-07T14:58:54.173Z
The underlying principle of Bresenham (the error value getting accumulated and moved back to 0 as events happen) is nice but no-one seems to teach it
ratchet freak
CC BY-SA 4.0
2018-08-08T16:38:10.517Z
Yep, that fixes the floating point exception, now I just need to find the math logic. What algorithm you'd recommend?
koperandus
CC BY-SA 4.0
2018-08-08T17:04:40.040Z
Well, if you want to draw lines one pixel at a time, Bresenham's algorithm is the right one. But why do you want to draw lines one pixel at a time? Any OpenGL implementation will draw them all at once for you.
Dan Hulme
CC BY-SA 4.0
2018-09-07T08:20:06.157Z
@DanHulme Sometimes it's useful to know a little history.... even if it's only to give you a greater appreciation of the present ;-)
Simon F
CC BY-SA 4.0
2018-08-07T14:11:50.857Z
2018-08-07T14:11:50.857Z
Dan Hulme
<p>I'm getting started with graphics and I'm having trouble implementing the Bresenham's algorithm. Now, I know that my implementation is incomplente, but I can't see why it doesn't make horizontal lines(same height(y) values, different width(x values). I get a floating point exception. Here's the algo:</p>

<pre><code>int draw_line(void *mlx_ptr, void*win_ptr, t_2dpoint *initial_p, t_2dpoint *final_p)
{
 53     int delta_x;

 53     int delta_x;
 54     int y;
 55     int delta_y;
 56
 57     delta_x = final_p-&gt;x - initial_p-&gt;x;
 58     delta_y = final_p-&gt;y - initial_p-&gt;y;
 59
 60     x = initial_p-&gt;x;
 61     y = initial_p-&gt;y;
 62     e = (float)(delta_x / delta_y) - 1.0;
 63
 64     while(x &lt; final_p-&gt;x - 1)
 65     {
 66         mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0XFF00FF);
 67         if ( e &gt;= 0)
 68         {
 69             y += 1;
 70             e -= 1.0;
 71         }
 72         x += 1;
 73         e += (float)delta_x / delta_y;
 74     }
 75     mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0XFF00FF);
 76 }
</code></pre>

<vector-graphics><line-drawing><pixel-graphics>
Bresenham's Algorithm not drawing horizontal lines
koperandus
# Bresenham's Algorithm not drawing horizontal lines

(Asked by: username_0 on 2018-08-07)

I'm getting started with graphics and I'm having trouble implementing the Bresenham's algorithm. Now, I know that my implementation is incomplente, but I can't see why it doesn't make horizontal lines(same height(y) values, different width(x values). I get a floating point exception. Here's the algo:

```
int draw_line(void *mlx_ptr, void*win_ptr, t_2dpoint *initial_p, t_2dpoint *final_p)
{
 53     int delta_x;

 53     int delta_x;
 54     int y;
 55     int delta_y;
 56
 57     delta_x = final_p->x - initial_p->x;
 58     delta_y = final_p->y - initial_p->y;
 59
 60     x = initial_p->x;
 61     y = initial_p->y;
 62     e = (float)(delta_x / delta_y) - 1.0;
 63
 64     while(x < final_p->x - 1)
 65     {
 66         mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0XFF00FF);
 67         if ( e >= 0)
 68         {
 69             y += 1;
 70             e -= 1.0;
 71         }
 72         x += 1;
 73         e += (float)delta_x / delta_y;
 74     }
 75     mlx_pixel_put(mlx_ptr, win_ptr, x, y, 0XFF00FF);
 76 }

```

**Tags:** vector-graphics, line-drawing, pixel-graphics


---

## 1 answers

### Answer by username_1 on 2018-08-07. Score: 2

```
e = (float)(delta_x / delta_y) - 1.0;

e += (float)delta_x / delta_y;

```

Either of these lines will cause a divide-by-zero error when `delta_y` is zero, which is when the line is horizontal (or zero-length). You need to check for this condition and have some special-case code.

As an aside, I don't know why people still teach Bresenham's algorithm to new CG students. It's a good example of an algorithm but it's not very useful knowledge in itself: you're unlikely to need to know it unless it's for a job interview.

> **Comments:**
> **username_2**: The underlying principle of Bresenham (the error value getting accumulated and moved back to 0 as events happen) is nice but no-one seems to teach it  
> **username_0**: Yep, that fixes the floating point exception, now I just need to find the math logic. What algorithm you'd recommend?  
> **username_1**: Well, if you want to draw lines one pixel at a time, Bresenham's algorithm is the right one. But why do you want to draw lines one pixel at a time? Any OpenGL implementation will draw them all at once for you.  
> **username_3**: @username_1 Sometimes it's useful to know a little history.... even if it's only to give you a greater appreciation of the present ;-)  

---

