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) {

	int absdeltay, absdeltax;
	absdeltay = y2-y1;
	if (absdeltay < 0) {
		absdeltay = -absdeltay;
	}
	absdeltax = x2-x1;
	if (absdeltax < 0) {
		absdeltax = -absdeltax;
	}
	if (absdeltay < absdeltax) {
		if (x1 > x2) {
			int tmp;
			tmp = x1;
			x1 = x2;
			x2 = tmp;
			tmp = y1;
			y1 = y2;
			y2 = tmp;
		}
		int yi, m, deltax, F;
		yi = y1;
		m = y2-y1;
		deltax = x2-x1;
		F = 0;
		view.setPixel(x1,y1);
		for (int xi=x1; xi<x2; xi++) {
			F = F + m;
			if (F > .5*deltax) {
				F = F - deltax;
				yi++;
			}
			if (F < -.5*deltax) {
				F = F + deltax;
				yi--;
			}
			view.setPixel(xi+1,yi);
		}
	}
	else {
		if (y1 > y2) {
			int tmp;
			tmp = x1;
			x1 = x2;
			x2 = tmp;
			tmp = y1;
			y1 = y2;
			y2 = tmp;
		}
                int xi, antim, deltay, F;
                xi = x1;
                antim = x2-x1;
                deltay = y2-y1;
                F = 0;
                view.setPixel(x1,y1);
                for (int yi=y1; yi<y2; yi++) {
                        F = F + antim;
                        if (F > .5*deltay) {
                                F = F - deltay;
                                xi++;
                        }
                        if (F < -.5*deltay) {
                                F = F + deltay;
                                xi--;
                        }
                        view.setPixel(xi,yi+1);
                }

	}

	//For debugging:
	System.out.println("Entering BresenhamAlgorithm");

    }
}





