Bresenham's Algorithm not drawing horizontal lines 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-&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 }
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.