title: All cases covered Bresenham's line-algorithm
body: <p>I need to check all pixels in a line, so I'm using Bresenham's algorithm to access each pixel in it. In particular I need to check if all pixels are located on valid pixel of a bitmap. This is the code:</p>
  
  <pre><code>private void Bresenham(Point p1, Point p2, ref List&lt;Point&gt; track) {
    int dx = p2.X - p1.X;
    int dy = p2.Y - p1.Y;
  
    int swaps = 0;
    if (dy &gt; dx) {
      Swap(ref dx, ref dy);
      swaps = 1;
    }
  
    int a = Math.Abs(dy);
    int b = -Math.Abs(dx);
  
    double d = 2*a + b;
    int x = p1.X;
    int y = p1.Y;
    color_track = Color.Blue;
    Check_Pixel(ref area, new Point(x,y));
    track.Clear();
    track.Add(new Point(x, y));
  
    int s = 1;
    int q = 1;
    if (p1.X &gt; p2.X) q = -1;
    if (p1.Y &gt; p2.Y) s = -1;
  
    for(int k = 0; k &lt; dx; k++) {
      if (d &gt;= 0) {
        d = 2*(a+b) + d;
        y = y + s;
        x = x + q;
      }
      else {
        if (swaps == 1) y = y + s;
        else x = x + q;
        d = 2 * a + d;
      }
      Check_Pixel(ref area, new Point(x, y));
      track.Add(new Point(x, y));
    }
  }
  
  private void Swap(ref int x, ref int y) {
    int temp = x;
    x = y;
    y = temp;
  }
  </code></pre>
  
  <p>Check_Pixel is used to check if the line pixel is on a valid bitmap one. I add the pixel founded to a <code>List&lt;Point&gt;</code> to render point by point his element and be sure <code>Bresenham</code> checks the correct pixels (it does). The problem is that my algorithm doesn't cover all the cases but about 50% of them.</p>
  
  <p>P.S: my coordinates system origin is on the left-upper corner (x grows left to right, y up to down)</p>