import display.*;

public class CohenSutherland {

    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int TOP = 4;
    public static final int BOTTOM = 8;
    
    /*
      Inputs: the original line from (x1,y1) to (x2,y2), and a
      viewport The viewport object has four public instance variables,
      which can be accessed by: 
      var1 = viewport.top; 
      var2 = viewport.bottom; 
      var3 = viewport.left; 
      var4 = viewport.right;

      Output: a LineSegment object 
            You can create a LineSegment object by making the call
			      LineSegment ls = new LineSegment()

            Having created it, you must set five values: 
	    x1, y1, x2, y2    the coordinates of each of the
                              endpoints of the clipped line You set
                              these values by including the line
                              ls.setEndPoints(x1, y1, x2, y2); 
			      or alternatively, by setting each of
                              the endpoints individually: 
			      ls.x1 = x1;
                              ls.y1 = y1; 
			      etc.

	    clipType          The clip type can be set to three
	                      values:

			      "internal" if the input line segment
			      falls entirely within the clip region,
			      otherwise:

                              "clipped" if it was determined that
                              some part of the input line segment does
                              fall within the boundaries defined by the
                              clip region, otherwise:

			      "external" if NO part of the input
			      line segment falls within the clip region
			      (i.e. the entire line was clipped.)
                              
			      You can set these values by including
			      the java statements:
                                    ls.setClipType("internal");
			      OR
                                    ls.setClipType("clipped");
                              OR
                                    ls.setClipType("external");
    
	NOTE: If you want to have any auxiliary methods (helpers), you
	must make sure to declare them "static" as well.
    */

    public static LineSegment ClipSegment(double x1, double y1, 
				       double x2, double y2, 
				       Viewport vp) {

	// ******* YOUR CODE HERE *********

	int c1,c2,c;
	boolean done=false;
	double m=0;
	
	//for debugging:
	System.out.println("Entering Cohen-Sutherland");

	LineSegment ls = new LineSegment();
	ls.x1 = 0.0;
	ls.y1 = 0.0;
	ls.x2 = 0.0;
	ls.y2 = 0.0;
	ls.setClipType("internal");
	

	while (!done){
	    c1=outcode(x1,y1,vp.top,vp.bottom,vp.left,vp.right);
	    c2=outcode(x2,y2,vp.top,vp.bottom,vp.left,vp.right);
	    c = (c1|c2);

	    if (c==0){//trivial accept
		done=true;
	    }
	    else if ((c1&c2)!=0){//trivial reject
		done=true;
		ls.setClipType("external");			
	    }
	    else {
		ls.setClipType("clipped");
		m = (y2-y1)/(x2-x1);
		if ((c&LEFT)!=0){
		    if ((c1&LEFT)!=0){
			y1 += (vp.left - x1) * m;
			x1 = vp.left;
		    }
		    else{
			y2 += (vp.left - x2) * m;
			x2 = vp.left;
		    }
		}
		else if ((c&RIGHT)!=0){
		    if ((c1&RIGHT)!=0){
			y1 += (vp.right - x1) * m;
			x1 = vp.right;
		    }
		    else{
			y2 += (vp.right - x2) * m;
			x2 = vp.right;
		    }
		}
		else if ((c&TOP)!=0){
		    if ((c1&TOP)!=0){
			x1 += (vp.top - y1) / m;
			y1 = vp.top;
		    }
		    else{
			x2 += (vp.top - y2) / m;
			y2 = vp.top;
		    }
		}
		else{
		    if ((c1&BOTTOM)!=0){
			x1 += (vp.bottom - y1) / m;
			y1 = vp.bottom;
		    }
		    else{
			x2 += (vp.bottom - y2) / m;
			y2 = vp.bottom;
		    }
		}
	    }
	    ls.x1 = x1;
	    ls.y1 = y1;
	    ls.x2 = x2;
	    ls.y2 = y2;

	}
	
	
	
	
	return ls;
    }

    /*
      This method, given a point (x,y) and some clip region
      boundaries, should produce the binary clip codes for the
      Cohen-Sutherland algorithm.  
    */
    public static int outcode(double x, double y,
			      double top, double bottom, 
			      double left, double right) {

	int result = 0;

	if (x<left)
	    result = LEFT;
	else if (x>right)
	    result = RIGHT;
	if (y>top)
	    result = result | TOP;
	else if (y<bottom)
	    result = result | BOTTOM;

	return result;
    }	
}





