import display.*;

//The class that the students will fill up with the correct algorithm.

public class Bresenham {

    /*
      Inputs: x1, y1, x2 and y2 -- the endpoints of the line. The line
				   stretches from (x1, y1) to (x2, y2)
              Grid view         -- an object representing the display

      Outputs: none, just draw the line

      Something useful: in order to draw the line, you must set a
	 	 	series of individual pixels. You can do this
	 	 	by making a call to the setPixel method of the
	 	 	Grid object. In other words, the command
	 	 	         view.setPixel(3,4); 
			would turn the pixel at position (3, 4) black.


      NOTE: If you want to have any auxiliary methods (helpers), you
	    must make sure to declare them "static" as well.

     */
    public static void BresenhamAlgorithm(int x1, int y1, 
					  int x2, int y2, Grid view) {

      // This works on a modified version of the Bresenham algorithm.
      // It works by taking the parametric equation for a line:

      // y = [(y2 - y1)/(x2 - x1)] * t + y1

      // The program keeps track of the current y value and if it differs
      // by more than .5 from the target y it increments the current y:

      // if (.5 <  [(y2 - y1)/(x2 - x1)] * t + y1 + yCur) then ...

      // Multiplying through by 2 * (x2 - x1) gets rid of all the floating
      // point math.

      // This only works if the slope is between 0 and 1.

      System.out.println ("Entering Henry Wong's code:");
      
      int yCur = y1;
      int yDif = y2 - y1;
      int xDif = x2- x1;

      System.out.println ("x1: " + x1 + " y1: " + y1 + " x2: " + x2 +
			  " y2: " + y2);

      for (int i = 0; i <= xDif; i++) {
	if (xDif < ((2 * yDif * i) + 2 * (y1 - yCur) * xDif)) {
	  yCur++;
	}

	//System.out.println ("y should be: " + ((i * (float)yDif)/xDif + y1));
	//System.out.println ("y is: " + yCur);

	view.setPixel(x1 + i, yCur);
      }
    }
}





