title: Drawing lines with Bresenham's Line Algorithm
body: <p>My computer graphics homework is to implement OpenGL algorithms using only the ability to draw points.</p>
  
  <p>So obviously I need to get <code>drawLine()</code> to work before I can draw anything else.  <code>drawLine()</code> has to be done using integers only. No floating point.</p>
  
  <p>This is what I was taught. Basically, lines can be broken up into 4 different categories, positive steep, positive shallow, negative steep and negative shallow. This is the picture I am supposed to draw:</p>
  
  <p><img src="https://i.imgur.com/xePCI.png" alt="expected result"></p>
  
  <p>and this is the picture my program is drawing:</p>
  
  <p><img src="https://i.imgur.com/2bcLp.png" alt="actual result"></p>
  
  <p>The colors are done for us.  We are given vertices and we need to use Bresenham's Line algorithm to draw the lines based on the start and end points.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>int dx = end.x - start.x;
  int dy = end.y - start.y;
  
  //initialize varibales
  int d;
  int dL;
  int dU;
  
  if (dy &gt; 0){
          if (dy &gt; dx){
                  //+steep
                  d = dy - 2*dx;
                  dL = -2*dx;
                  dU = 2*dy - 2*dx;
  
                  for (int x = start.x, y = start.y; y &lt;= end.y; y++){
                          Vertex v(x,y);
                          drawPoint(v);
  
                          if (d &gt;= 1){
                                  d += dL;
                          }else{
                                  x++;
                                  d += dU;
                          }
                  }              
          } else {
                  //+shallow
                  d = 2*dy - dx;
                  dL = 2*dy;
                  dU = 2*dy - 2*dx;
  
                  for (int x = start.x, y = start.y; x &lt;= end.x; x++) {
                          Vertex v(x,y);
                          drawPoint(v);
  
                          // if choosing L, next y will stay the same, we only need
                          // to update d by dL
                          if (d &lt;= 0) {
                                  d += dL;
                          // otherwise choose U, y moves up 1
                          } else {
                                  y++;
                                  d += dU;
                          }
                  }
          }
  } else {
          if (-dy &gt; dx){
                  cout &lt;&lt; "-steep\n";
                  //-steep
                  d = dy - 2*dx;
                  //south
                  dL = 2*dx;
                  //southeast
                  dU = 2*dy - 2*dx;
  
                  for (int x = start.x, y = start.y; y &gt;= end.y; --y){
                          Vertex v(x,y);
                          drawPoint(v);
  
                          //if choosing L, next x will stay the same, we only need
                          //to update d
                          if (d &gt;= 1){
                                  d -= dL;
                          } else {
                                  x++;
                                  d -= dU;
                          }
                  }
  
          } else {
                  cout &lt;&lt; "-shallow\n";
                  //-shallow
                  d = 2*dy - dx;
                  dL = 2*dy;
                  dU = 2*dy - 2*dx;
  
                  for (int x = start.x, y = start.y; x &lt;= end.x; x++){
                          Vertex v(x,y);
                          drawPoint(v);
  
                          if (d &gt;= 0){
                                  d += dL;
                          } else {
                                  --y;
                                  d -= dU;
                          }
                  }
          }
  }
  </code></pre>
  
  <p>I know my error is going to be something silly, but I honestly cannot figure out what I am doing wrong. Why are some of the lines drawn incorrectly as shown above?</p>