import display.*;
import java.lang.Math;


//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 dx, dy, xstep, ystep, e;
	int da, db, a, b, astep, bstep, a_f, ix, iy;
	int[] coord = new int[2];

	dx = x2 - x1;
	dy = y2 - y1;
	if (dx < 0) { dx = -dx; xstep = -1; } else { xstep = 1;}
	if (dy < 0) { dy = -dy; ystep = -1; } else { ystep = 1;}

	dx <<= 1; dy <<= 1;

	if (dx > dy)
	{
	    coord[0] = x1;  astep = xstep;  da = dx;  ix = 0;
	    coord[1] = y1;  bstep = ystep;  db = dy;  iy = 1;
	    a_f = x2 + xstep;	    
	    e = -(dx >> 1);
	}
	else
        {
	    coord[0] = y1;  astep = ystep;  da = dy;  iy = 0;
	    coord[1] = x1;  bstep = xstep;  db = dx;  ix = 1;
	    a_f = y2 + ystep;	    
	    e = -(dy >> 1);
	}

	while(coord[0] != a_f)
	{ 
	    view.setPixel(coord[ix], coord[iy]);
	    if((e += db) > 0)
	    {
		coord[1] += bstep;
		e -= da;
	    }
	    coord[0] += astep;
	}
    }

}





