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 *****
	
	// NOTE:  I used two loops here.  For fastet performance, I'd use four loops and remove one of the
	// conditionals inside each loop.
					  
		int     slope, ii, jj, x, y, add, width, height, norm;
		int     end, inc;
        
        width = x2 - x1;
        height = y2 - y1;
                             
        if (Math.abs(width) > Math.abs(height)) {       // slope less than zero (Mostly horizontal)
            
            y = y1;       

            add = width;        
            slope = (2 * height);
            norm = 2 * width;
            
            if (height < 0) {
                add *= -1;
            }

            if (x1 < x2) {
                inc = 1;
            }
            else {
                inc = -1;
                norm *= -1;
                add *= -1;
            }
            
            end = x2 + inc; // increase by inc so we include the endpoint
            
            for (ii = x1; ii != end; ii += inc) {
                if (add > norm) {
                    add -= norm;
                    y++;
                }
                if (add < -norm) {
                    add += norm;
                    y--;
                }
                add += slope;
                view.setPixel(ii, y);
            }                                   
        }
        else {                                          // slope greater than zero (Mostly vertical)

            x = x1;       

            add = height;        
            slope = (2 * width);
            norm = 2 * height;
            
            if (width < 0) {
                add *= -1;
            }

            if (y1 < y2) {
                inc = 1;
            }
            else {
                inc = -1;
                norm *= -1;
                add *= -1;
            }
            
            end = y2 + inc;  // increase by inc so we include the endpoint
            
            for (ii = y1; ii != end; ii += inc) {
                if (add > norm) {
                    add -= norm;
                    x++;
                }
                if (add < -norm) {
                    add += norm;
                    x--;
                }
                add += slope;
                view.setPixel(x, ii);
            }                                   
        }
					 
	//For debugging:
	System.out.println("Entering BresenhamAlgorithm");

    }
}





