// FILE:      BreakpointDrawer.java
// PURPOSE:   Uses the TriangleInterface to create a drawer which only
//            draws the outside of the triangle and a line signifying the
//            breakpoint.
// METHOD:    Overrides TriangleDrawer and puts in the breakpoint line
// MODS:      10.11.98 -- original [JL]
//
// CREATOR:   Jeremy Lueck (jlueck@mit.edu)

import Raster;
import Triangle;
import WireframeDrawer;
import java.awt.Color;

public class BreakpointDrawer extends WireframeDrawer {

    protected Vertex2D[] sorty;
    protected Raster     raster;

    public BreakpointDrawer(Color color){
	super(color);
	sorty = new Vertex2D[3];
    }

    public BreakpointDrawer() {
	super();
	sorty = new Vertex2D[3];
    }

    public void Draw(Raster r, Triangle t) {
	super.Draw(r, t);

	Vertex2D v0 = t.v[0];
	Vertex2D v1 = t.v[1];
	Vertex2D v2 = t.v[2];
	
	// Do a quick sort of the vertices in ascending order by the
	// y-coordinate.  FIXME: refine by assumptions of triangle files

	if (v0.y >= v1.y) {
	    if (v0.y >= v2.y) {
		sorty[0] = v0;
		if (v1.y >= v2.y) {
		    sorty[1] = v1;
		    sorty[2] = v2;
		} else {
		    sorty[1] = v2;
		    sorty[2] = v1;
		}
	    } else {
		sorty[0] = v2;
		sorty[1] = v0;
		sorty[2] = v1;
	    }
	} else {
	    if (v1.y >= v2.y) {
		sorty[0] = v1;
		if (v0.y >= v2.y) {
		    sorty[1] = v0;
		    sorty[2] = v2;
		} else {
		    sorty[1] = v2;
		    sorty[2] = v0;
		}
	    } else {
		sorty[0] = v2;
		sorty[1] = v1;
		sorty[2] = v0;
	    }
	}

	// set global variable so we don't have to pass it.
	raster = r;

	// test various cases to see what types of triangles we need to draw.
	TriEdge left, right;
	if (sorty[0].y == sorty[1].y) {
	    // only draw lower triangle
	    int leftnum = (sorty[0].x < sorty[1].x) ? 0 : 1;
	    int rightnum = (leftnum == 1) ? 0 : 1;
	    left = new TriEdge(sorty[leftnum], sorty[2]);
	    right = new TriEdge(sorty[rightnum], sorty[2]);
	    drawLowerTriangle((int)sorty[1].y, left, right);

	} else if (sorty[1].y == sorty[2].y) {
	    // only draw upper trianlge
	    int leftnum = (sorty[1].x < sorty[2].x) ? 1 : 2;
	    int rightnum = (leftnum == 1) ? 2 : 1;
	    left = new TriEdge(sorty[0], sorty[leftnum]);
	    right = new TriEdge(sorty[0], sorty[rightnum]);
	    drawUpperTriangle((int)sorty[2].y, left, right);

	} else if (sorty[0].x > sorty[1].x) {
	    // draw upper triangle to breakpoint on the left
	    left = new TriEdge(sorty[0], sorty[1]);
	    right = new TriEdge(sorty[0], sorty[2]);
	    drawUpperTriangle((int)sorty[1].y, left, right);
	    // draw lower triangle
	    left = new TriEdge(sorty[1], sorty[2]);
	    drawLowerTriangle((int)sorty[1].y, left, right);

	} else {
	    // draw upper triangle to breakpoint on the left
	    left = new TriEdge(sorty[0], sorty[2]);
	    right = new TriEdge(sorty[0], sorty[1]);
	    drawUpperTriangle((int)sorty[1].y, left, right);
	    // draw lower triangle
	    right = new TriEdge(sorty[1], sorty[2]);
	    drawLowerTriangle((int)sorty[1].y, left, right);
	}	    

    }

    /** 
     * Draws an upper triangle, with the top point being the point
     * where the two edges meet, and the bottom line being a straight
     * line at y connecting the two edges.  
     *
     * @param y  the y-coordinate of the lower line connecting t1 & t2
     * @param t1 the leftmost edge
     * @param t2 the rightmost edge
     */
    public void drawUpperTriangle(int y, TriEdge right, TriEdge left) {
	int pix = color.getRGB();
	for(int x=0; x < 400; x++) {
	    raster.setPixel(pix, x, y);
	}
    }

    /** 
     * Draws a lower triangle, with the top point being the point
     * where the two edges meet, and the bottom line being a straight
     * line at y connecting the two edges
     *
     * @param y  the y-coordinate of the lower line connecting t1 & t2
     * @param t1 the leftmost edge
     * @param t2 the rightmost edge
     */
    public void drawLowerTriangle(int y, TriEdge right, TriEdge left) {
	int pix = color.getRGB();
	for(int x=0; x < 400; x++) {
	    raster.setPixel(pix, x, y);
	}
    }

}
    
