// Wendy Chien
// Recitation F11
// Project 2

import java.awt.*;
import java.lang.*;
import java.util.*;

public class Triangle implements Drawable {
  private Vertex2D v[];
  private boolean breakright;  // if breakright is 1, then the break vertex is to the right 
  private ColorFinder cf;

  public Triangle(Vertex2D vtx0, Vertex2D vtx1, Vertex2D vtx2) {

    v = new Vertex2D[3];

    // Sort the vertices (y values)  
    // v[0] contains the vertex with the smallest y value
    // v[1] contains the vertex with the middle y value
    // v[2] contains the vertex with the largest y value
    if (vtx0.y <= vtx1.y) {
      v[0] = vtx0;
      v[1] = vtx1;
    }
    else {
      v[0] = vtx1;
      v[1] = vtx0;
    }
    if (v[0].y <= vtx2.y) {
      if (v[1].y <= vtx2.y) {
	v[2] = vtx2;
      }
      else {
	v[2] = v[1];
	v[1] = vtx2;
      }
    }
    else {
      v[2] = v[1];
      v[1] = v[0];
      v[0] = vtx2;
    }
    
    // Find the position of the breakpoint
    float slope = (float) (v[0].y - v[2].y) / (float) (v[0].x - v[2].x);
    float xOnLine = (1/slope)*(v[1].y-v[2].y) + v[2].x;
    if (xOnLine < v[1].x) 
      breakright = true;
    else
      breakright = false;

    // Creates the ColorFinder
    cf = new ColorFinder(v);
  }

  public void colorPixel(Raster r, int x, int y) {
    int c;

    c = cf.getColor(x,y);
    r.setPixel(c,x,y);

  }

  public void Draw(Raster r) {
    // Draws the triangle.
    int y, y1, y2; 
    int x;
    int x1, x2;
    float dx1, dx2;
    boolean horiz1 = false;
    boolean horiz2 = false;
    float x1OnLine, x2OnLine;

    if ((v[0].y - v[1].y) != 0) 
      dx1 = (float) (v[0].x - v[1].x)/ (float) (v[0].y - v[1].y);
    else {
      dx1 = 0;
      horiz1 = true;
    }
    if ((v[0].y - v[2].y) != 0) 
      dx2 = (float) (v[0].x - v[2].x)/ (float) (v[0].y - v[2].y);
    else {
      dx2 = 0;
      horiz2 = true;
    }
    
    y = (int) Math.ceil(v[0].y);
    y1 = (int) Math.ceil(v[1].y);
    y2 = (int) Math.ceil(v[2].y);

    while (y < y1) {
      // Goes through the scan lines above the breakpoint

      x1OnLine = (float) ((dx1)*((float)y-v[1].y) + v[1].x);
      x2OnLine = (float) ((dx2)*((float)y-v[2].y) + v[2].x);

      x1 = (int) Math.ceil(x1OnLine);
      x2 = (int) Math.ceil(x2OnLine);
      
      // Scans the line.
      if (x1 <= x2) {
	for (x=x1;x<x2;x++)
	  colorPixel(r,x,y);
      }
      else {
	for (x=x2;x<x1;x++)
	  colorPixel(r,x,y);
      }

      y++;
      
    }

    // Scan the second part of the triangle (the part after the breakpoint)

    // Changes the slope of one of the edges (because we are past the breakpoint)
    if ((v[1].y - v[2].y) != 0) 
      dx1 = (float) (v[1].x - v[2].x)/ (float) (v[1].y - v[2].y);
    else {
      dx1 = 0;
      horiz1 = true;
    }
    
    while (y < y2) {
      // Goes through the scan lines below the breakpoint
      x1OnLine = (float) ((dx1)*((float)y-v[2].y) + v[2].x);
      x2OnLine = (float) ((dx2)*((float)y-v[2].y) + v[2].x);

      x1 = (int) Math.ceil(x1OnLine);
      x2 = (int) Math.ceil(x2OnLine);
	
      // Scans the line.
      if (x1 <= x2) {
	for (x=x1;x<x2;x++)
	  colorPixel(r,x,y);
      }
      else {
	for (x=x2;x<x1;x++)
	  colorPixel(r,x,y);
      }

      y++;

    }
  }

  public void printLine(String s) {
    System.out.println(s);
  }

  public void printStats(int x1, int x2, float x1OnLine, float x2OnLine) {
    
    System.out.println("Statistics");
    System.out.println("x1: "+x1);
    System.out.println("x2: "+x2);
    System.out.println("x1OnLine: "+x1OnLine);
    System.out.println("x2OnLine: "+x2OnLine);
    
  }

  public void printOut() {
        
    System.out.println("v[0]: ("+v[0].x+","+v[0].y+")");
    System.out.println("v[1]: ("+v[1].x+","+v[1].y+")");
    System.out.println("v[2]: ("+v[2].x+","+v[2].y+")");
    if (breakright) 
      System.out.println("breakright");
    else
      System.out.println("breakleft");
      
  }

}

