title: C++ triangle rasterization
body: <p>I'm trying to fix this triangle rasterizer, but cannot make it work correctly. For some reason it only draws half of the triangles.</p>
  
  <pre><code>void DrawTriangle(Point2D p0, Point2D p1, Point2D p2)
  {
      Point2D Top, Middle, Bottom;
      bool MiddleIsLeft;
  
      if (p0.y &lt; p1.y)                    // case: 1, 2, 5
      {
          if (p0.y &lt; p2.y)                // case: 1, 2
          {
              if (p1.y &lt; p2.y)            // case: 1
              {
                  Top = p0;
                  Middle = p1;
                  Bottom = p2;
                  MiddleIsLeft = true;
              }
              else                        // case: 2
              {
                  Top = p0;
                  Middle = p2;
                  Bottom = p1;
                  MiddleIsLeft = false;
              }
          }
          else                            // case: 5
          {
              Top = p2;
              Middle = p0;
              Bottom = p1;
              MiddleIsLeft = true;                
          }
      }
      else                        // case: 3, 4, 6
      {
          if (p0.y &lt; p2.y)        // case: 4
          {
              Top = p1;
              Middle = p0;
              Bottom = p2;
              MiddleIsLeft = false;
          }
          else                    // case: 3, 6
          {
              if (p1.y &lt; p2.y)    // case: 3
              {
                  Top = p1;
                  Middle = p2;
                  Bottom = p0;
                  MiddleIsLeft = true;
              }
              else                // case 6
              {
                  Top = p2;
                  Middle = p1;
                  Bottom = p0;
                  MiddleIsLeft = false;
              }
          }
      }
  
      float xLeft, xRight;
      xLeft = xRight = Top.x;
      float mLeft, mRight;
      // Region 1
      if(MiddleIsLeft)
      {
          mLeft = (Top.x - Middle.x) / (Top.y - Middle.y);
          mRight = (Top.x - Bottom.x) / (Top.y - Bottom.y);
      }
      else
      {
          mLeft = (Top.x - Bottom.x) / (Top.y - Bottom.y);
          mRight = (Middle.x - Top.x) / (Middle.y - Top.y);
      }
      int finalY;
      float Tleft, Tright;
      for (int y = ceil(Top.y); y &lt; (int)Middle.y; y++)
      {        
          Tleft=float(Top.y-y)/(Top.y-Middle.y);
          Tright=float(Top.y-y)/(Top.y-Bottom.y);
          for (int x = ceil(xLeft); x &lt;= ceil(xRight) - 1 ; x++)
          {
              FrameBuffer::SetPixel(x, y, p0.r,p0.g,p0.b);
  
          }  
          xLeft += mLeft;
          xRight += mRight;
          finalY = y;
      }
  
      // Region 2 
      if (MiddleIsLeft)
      {
          mLeft = (Bottom.x - Middle.x) / (Bottom.y - Middle.y);
      }
      else
      {
          mRight = (Middle.x - Bottom.x) / (Middle.y - Bottom.y);
      }
  
      for (int y = Middle.y; y &lt;= ceil(Bottom.y) - 1; y++)
      {
          Tleft=float(Bottom.y-y)/(Bottom.y-Middle.y);
          Tright=float(Top.y-y)/(Top.y-Bottom.y);
          for (int x = ceil(xLeft); x &lt;= ceil(xRight) - 1; x++)
          {
              FrameBuffer::SetPixel(x, y, p0.r,p0.g,p0.b);
          }
          xLeft += mLeft;
          xRight += mRight; 
  
      }
  }
  </code></pre>
  
  <p>Here is what happens when I use it to draw shapes.</p>
  
  <p><img src="https://i.stack.imgur.com/JryXt.jpg" alt="Tank image"></p>
  
  <p>When I disable the second region, all those weird triangles disappear.</p>
  
  <p><img src="https://i.stack.imgur.com/uWsVx.jpg" alt="without the second region"></p>
  
  <p>The wireframe mode works perfect, so this eliminates all the other possibilities other than the triangle rasterizer.</p>
  
  <p><img src="https://i.stack.imgur.com/eo2Xk.jpg" alt="wireframe"></p>