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) 
    {
	// ***** YOUR CODE HERE *****
	//For debugging:
	System.out.println("Entering BresenhamAlgorithm"); 
	int criteria; 
	int error = 0; 
	int stepInX = 1; 
	int stepInY = 1; 
	int dX = x2 - x1;
	int dY = y2 - y1; 
	if ( dX < 0 ) 
	    { 
		stepInX = -1; 
		dX = -dX; 
	    } 
	if ( dY < 0 ) 
	    { 
		stepInY = -1; 
		dY = -dY; 
	    } 
	if ( dY <= dX ) 
	    { 
		criteria = dX / 2; 
		while ( x1 != x2 + stepInX ) 
		    { 
			view.setPixel(x1,y1);
			error += dY; 
			x1 += stepInX; 
			if ( error > criteria ) 
			    { 			
				y1 += stepInY; 
				error -= dX; 
			    } 
		    } 
	    } 
	else 
	    { 
		criteria = dY / 2; 
		while ( y1 != y2 + stepInY ) 
		    { 
			view.setPixel(x1, y1);
			error += dX; 
			y1 += stepInY; 
			if ( error > criteria ) 
			    { 
				x1 += stepInX; 
				error -= dY; 
			    } 
		    } 
	    } 
    }
}

    






