/*Triangle practice PCS 6th grade - Z. J. Wood 10/2015 */
/* click each of the specified missing vertices */

boolean easy = true;

void setup() {
  /* YOU can change these values to change where the final missing 
   triangle vertex is */
  //missing vertex for the first triangle
  v1x = 400;
  v1y = 300;

  if (easy) {
    //missing vertex for the second triangle
    v2x = 600;
    v2y = 100;
    //missing vertex for the third triangle
    v3x = 550;
    v3y = 550;
  } else {
    //missing vertex for the second triangle
    v2x = 600 + (int)random(-4, 4)*25;
    v2y = 100 + (int)random(-4, 4)*25;
    //missing vertex for the third triangle
    v3x = 550 + (int)random(-4, 4)*25;
    v3y = 550 + (int)random(-4, 4)*25;
  }

  /* YOU can change these vertex locations to change the 
     shape of the first triangle as well - see handout */
  t1x = 200;
  t1y = 200;
  t2x = 200;
  t2y = 400;

  size(800, 800);
  C1x = 400;
  C1y = 300;
  C2x = 500;
  C2y = 400;
  C3x = 300;
  C3y = 500;
  mouseX = mouseY = 100;
  anim1 = true;
  anim2 = anim3 = false;
  t1 = color(250, 210, 210);
  t2 = color(210, 250, 210);
  t3 = color(210, 210, 250);
}

/* code to test if the mouse it near the missing vertex */
void mousePressed() {
  if (abs(mouseX-v1x) < 10 && abs(mouseY-v1y) < 10) {
    anim1 = false;
    C1x = v1x;
    C1y = v1y;
    anim2 = true;
  }
  if (abs(mouseX-v2x) < 10 && abs(mouseY-v2y) < 10) {
    anim2 = false;
    C2x = v2x;
    C2y = v2y;
    anim3 = true;
  }
  if (abs(mouseX-v3x) < 10 && abs(mouseY-v3y) < 10) {
    anim3 = false;
    C3x = v3x;
    C3y = v3y;
  }
}

void draw() {

  background(12, 34, 56);

  drawGrid();
  fill(250, 200, 200);
  ellipse(v1x, v1y, 10, 10);

  if (anim1) {
    fill(250, 250, 24);
    triangle(mouseX, mouseY, t1x, t1y, t2x, t2y);
    text("Pin  {x, y}: {" + v1x + ", " + v1y + "}", 20, 20);
  } else {
    fill(t1);
    triangle(C1x, C1y, t1x, t1y, t2x, t2y);
  }

  if (anim2) {
    fill(250, 250, 24);
    triangle(mouseX, mouseY, t1x, t1y, v1x, v1y);
    text("Pin  {x, y}: {" + v2x + ", " + v2y + "}", 20, 20);
  } else if (!anim1 && !anim2) {
    fill(t2);
    triangle(C2x, C2y, t1x, t1y, v1x, v1y);
  }

  if (anim3) {
    fill(250, 250, 24);
    triangle(mouseX, mouseY, t2x, t2y, v1x, v1y);
    text("Pin  {x, y}: {" + v3x + ", " + v3y + "}", 20, 20);
  } else if (!anim1 && !anim2 && !anim3) {
    fill(t3);
    triangle(C3x, C3y, t2x, t2y, v1x, v1y);
    text("You did it congratulations!", 20, 20);
  }
  fill(255);
  text("Current {x, y}: {" + mouseX + ", " +mouseY + "}", 20, 40);
}

int C1x, C1y, C2x, C2y, C3x, C3y;
int v1x, v1y, v2x, v2y, v3x, v3y;
int t1x, t1y, t2x, t2y;
color t1, t2, t3;
boolean anim1, anim2, anim3; 

/*Method to draw a grid every 50th pixel 
 every 100th line is grey */
void drawGrid() {
  //the horizontal lines - every 50th pixel draw a line

  for (int i=0; i < height/50; i++) {
    if (i%2 == 0) {
      strokeWeight(1);
      stroke(0, 0, 128);
    } else {
      strokeWeight(2);
      stroke(128, 128, 128);
    }
    line(0, (i+1)*50, width, (i+1)*50);
  }

  //the vertical lines - every 50th pixel draw a line
  for (int i=0; i < width/50; i++) {
    if (i%2 == 0) {
      stroke(128, 0, 128);
      strokeWeight(1);
    } else {

      strokeWeight(2);
      stroke(128, 128, 128);
    }
    line((i+1)*50, 0, (i+1)*50, height);
  }
}