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 *****
	if (x1 == x2) {  // line is vertical
	    if (y1 < y2) {
		for (int i = y1; i <= y2; i++) {
		    view.setPixel(x1, i);
		}
	    }
	    else {
		for (int i = y2; i <= y1; i++) {
		    view.setPixel(x1, i);
		}
	    }
	}
	else if (y1 == y2) {  // line is horizontal
	    if (x1 < x2) {
		for (int i = x1; i <= x2; i++) {
		    view.setPixel(i, y1);
		}
	    }
	    else {
		for (int i = x2; i <= x1; i++) {
		    view.setPixel(i, y1);
		}
	    }
	}
	else {
	    int x, y, xEnd, yEnd;
	    int dx, dy, p, twoDy, twoDyDx;
	    
	    double slope = ((double)(y1 - y2)) / ((double)(x1 - x2));
	    dx = Math.abs(x1-x2);
	    dy = Math.abs(y1-y2);
	    if ( (slope > 0) && (slope <= 1) ) {
		p = 2*dy - dx;
		twoDy = 2*dy;
		twoDyDx = 2*(dy-dx);
		if (x1 < x2) {
		    x = x1;
		    y = y1;
		    xEnd = x2;
		}
		else {
		    x = x2;
		    y = y2;
		    xEnd = x1;
		}
		view.setPixel(x,y);
		while (x < xEnd) {
		    x++;
		    if ((p < 0) || ((p==0) && (x1<x2)))
			p += twoDy;
		    else {
			y++;
			p += twoDyDx;
		    };
		    view.setPixel(x,y);
		}
	    }
	    else if ( slope > 1) {
		p = 2*dx - dy;
		twoDy = 2*dx;
		twoDyDx = 2*(dx-dy);
		if (y1 < y2) {
		    y = y1;
		    x = x1;
		    yEnd = y2;
		}
		else {
		    y = y2;
		    x = x2;
		    yEnd = y1;
		}
		view.setPixel(x,y);
		while (y < yEnd) {
		    y++;
		    if ((p < 0) || ((p==0) && (y1 < y2)))
			p += twoDy;
		    else {
			x++;
			p += twoDyDx;
		    };
		    view.setPixel(x,y);
		}
	    }
	    else if (slope >= -1) {
		p = 2*dy - dx;
		twoDy = 2*dy;
		twoDyDx = 2*(dy-dx);
		if (x1 < x2) {
		    x = x1;
		    y = y1;
		    xEnd = x2;
		}
		else {
		    x = x2;
		    y = y2;
		    xEnd = x1;
		}
		view.setPixel(x,y);
		while (x < xEnd) {
		    x++;
		    if ((p < 0) || ((p==0) && (x1<x2)))
			p += twoDy;
		    else {
			y--;
			p += twoDyDx;
		    }
		    view.setPixel(x,y);
		}

	    }
	    else {
		p = 2*dx - dy;
		twoDy = 2*dx;
		twoDyDx = 2*(dx-dy);
		if (y1 < y2) {
		    y = y1;
		    x = x1;
		    yEnd = y2;
		}
		else {
		    y = y2;
		    x = x2;
		    yEnd = y1;
		}
		view.setPixel(x,y);
		while (y < yEnd) {
		    y++;
		    if ((p < 0) || ((p == 0) && (y1 < y2)))
			p += twoDy;
		    else {
			x--;
			p += twoDyDx;
		    };
		    view.setPixel(x,y);
		}
	    }
	}
	//For debugging:
	System.out.println("Entering BresenhamAlgorithm");
	
    }
}





