initVariables();
sMess = "ROTACIÓN DEL HOMBRO"
lFlex = true;
puntoOriginal = { x: 1, y: 0, z: 0 };
puntoRotado = { x: 0, y: 0, z: 1 };
anguloGiro = 0;
///////////////////////////////////////////////////////
function funcionGo(ctx, width, height) {
  if (lInit) {
    jsreinitProcedure();
    jsinitTimer(4);
    txClocalUp = "ROTACIÓN DE HOMBRO";
    txClocalDown = "sepárese de la cámara, la cara debe activarse";
    posHandLW = width / 2;
    posHandLH = height / 2;
  }
  //displaymodulo(0);
  textSize(26);
  /*if (lFlex) {
    fill(255, 255, 0);
    stroke(255, 255, 0)
  }
  else {
    fill(255, 0, 0);
    stroke(255, 0, 0)
  }*/
  fill(255);
  noStroke();
  text(sMess, wsIni + wVideo / 2, windowHeight - 80);
  displayPose();
  if (lDelayLocal) {
    lFlex = !lFlex;
  }
  noStroke();
}

function displayPose() {
  let angle = 0;
  textSize(20)
  strokeWeight(20)
  stroke(200, 200, 200, 128)
  fill(255, 255, 255, 128);

  //cabeza
  beginShape();
  curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
  curveVertex(x9, y9);
  curveVertex(x0, y0);
  curveVertex(x1, y1);
  curveVertex(x2, y2);
  curveVertex(x3, y3);
  curveVertex(x7, y7);
  curveVertex(x0, y0);
  curveVertex(x9, y9);
  curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
  endShape();

  beginShape();
  curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
  curveVertex(x10, y10);
  curveVertex(x0, y0);
  curveVertex(x4, y4);
  curveVertex(x5, y5);
  curveVertex(x6, y6);
  curveVertex(x8, y8);
  curveVertex(x0, y0);
  curveVertex(x10, y10);
  curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
  endShape();

  //eess
  if (!modoReflejo) {
    beginShape();
    curveVertex(x0, y0);
    curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
    curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
    curveVertex(x11, y11);
    curveVertex(x13, y13);
    curveVertex(x15, y15);
    curveVertex(x17, y17);
    curveVertex(x19, y19);
    curveVertex(x21, y21);
    curveVertex(x15, y15);
    curveVertex(x13, y13);
    curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
    curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
    endShape();

    beginShape();
    curveVertex(x0, y0);
    curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
    curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
    curveVertex(x12, y12);
    curveVertex(x14, y14);
    curveVertex(x16, y16);
    curveVertex(x18, y18);
    curveVertex(x20, y20);
    curveVertex(x22, y22);
    curveVertex(x16, y16);
    curveVertex(x14, y14);
    curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
    curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
    endShape();
  }
  else {
    if (!modoDiestro) {
      beginShape();
      curveVertex(x0, y0);
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex(x11, y11);
      curveVertex(x13, y13);
      curveVertex(x15, y15);
      curveVertex(x17, y17);
      curveVertex(x19, y19);
      curveVertex(x21, y21);
      curveVertex(x15, y15);
      curveVertex(x13, y13);
      curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      endShape();
      beginShape();
      curveVertex(width - x0, y0);
      curveVertex(width - (x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex(width - (x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex(width - x11, y11);
      curveVertex(width - x13, y13);
      curveVertex(width - x15, y15);
      curveVertex(width - x17, y17);
      curveVertex(width - x19, y19);
      curveVertex(width - x21, y21);
      curveVertex(width - x15, y15);
      curveVertex(width - x13, y13);
      curveVertex(width - (x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex(width - (x11 + x12) / 2, (y11 + y12) / 2)
      endShape();

    }
    else {
      beginShape();
      curveVertex(x0, y0);
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex(x12, y12);
      curveVertex(x14, y14);
      curveVertex(x16, y16);
      curveVertex(x18, y18);
      curveVertex(x20, y20);
      curveVertex(x22, y22);
      curveVertex(x16, y16);
      curveVertex(x14, y14);
      curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      endShape();

      beginShape();
      curveVertex(width - x0, y0);
      curveVertex(width - (x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex(width - (x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex(width - x12, y12);
      curveVertex(width - x14, y14);
      curveVertex(width - x16, y16);
      curveVertex(width - x18, y18);
      curveVertex(width - x20, y20);
      curveVertex(width - x22, y22);
      curveVertex(width - x16, y16);
      curveVertex(width - x14, y14);
      curveVertex(width - (x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex(width - (x11 + x12) / 2, (y11 + y12) / 2)
      endShape();
    }
  }

  noStroke();
  fill(0);
  let rshoulder = [x12, y12]
  let relbow = [x14, y14]
  let rwrist = [x16, y16]
  angle = calculate_angle(rshoulder, relbow, rwrist)
  //text(parseInt(angle), x14, y14)

  let lshoulder = [x11, y11]
  let lelbow = [x13, y13]
  let lwrist = [x15, y15]
  angle = calculate_angle(lshoulder, lelbow, lwrist)
  //text(parseInt(angle), x13, y13)

  let rhip = [x24, y24]
  angle = calculate_angle(rhip, rshoulder, relbow)
  //text(parseInt(angle), x12, y12)

  let lhip = [x23, y23]
  angle = calculate_angle(lhip, lshoulder, lelbow)
  //text(parseInt(angle), x11, y11)

  let rknee = [x26, y26]
  let rankle = [x28, y28]
  angle = calculate_angle(rhip, rknee, rankle)
  //text(parseInt(angle), x26, y26)

  let lknee = [x25, y25]
  let lankle = [x27, y27]
  angle = calculate_angle(lhip, lknee, lankle)
  //text(parseInt(angle), x25, y25)

  angle = calculate_angle(lshoulder, lhip, lknee)
  //text(parseInt(angle), x23, y23)

  angle = calculate_angle(rshoulder, rhip, rknee)
  //text(parseInt(angle), x24, y24)

  let lfoot = [x31, y31]
  angle = calculate_angle(lknee, lankle, lfoot)
  //text(parseInt(angle), x27, y27)

  let rfoot = [x32, y32]
  angle = calculate_angle(rknee, rankle, rfoot)
  //text(parseInt(angle), x28, y28)

  let loreja = [x7, y7]
  let cervical = [(x11 + x12) / 2, (y11 + y12) / 2]
  angle = calculate_angle(loreja, cervical, lshoulder)
  //text(parseInt(angle), x7, y7)

  let roreja = [x8, y8]
  angle = calculate_angle(roreja, cervical, rshoulder)
  //text(parseInt(angle), x8, y8)

  let nariz = [x0, y0]
  angle = calculate_angle(nariz, cervical, lshoulder)
  //text(parseInt(angle), x9, y9)

  angle = calculate_angle(nariz, cervical, rshoulder)
  //text(parseInt(angle), x10, y10)

  let entrecejo = [(x4 + x1) / 2, (y4 + y1) / 2]
  angle = calculate_angle(entrecejo, nariz, cervical)
  //text(parseInt(angle), (x11 + x12) / 2, (y11 + y12) / 2)

  strokeWeight(8)
  /*if (lFlex) {
    stroke(255, 255, 0, 128)
    fill(255, 255, 0, 128);
  }
  else {
    stroke(255, 0, 0, 128)
    fill(255, 0, 0, 128);
  }*/

  textSize(48);
  noStroke();
  switch (true) {
    case (nSwitch === 0):
      stroke(255, 255, 0, 128)
      fill(255, 255, 0, 128);
      sMess = "CODO PEGADO, ROTAR HACIA FUERA";
      txClocalUp = "ROTACIÓN EXTERNA DE HOMBRO";
      //txClocalDown = "levante el brazo por delante";
      beginShape();
      curveVertex(x0, y0);
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex(x12, y12);
      curveVertex(x14, y14);
      curveVertex(x16, y16);
      curveVertex(x18, y18);
      curveVertex(x20, y20);
      curveVertex(x22, y22);
      curveVertex(x16, y16);
      curveVertex(x14, y14);
      curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      endShape();
      noStroke();
      fill(255);
      pV1 = createVector(0, 0, 0);
      pV2 = createVector(0, 0, 0);
      pV1.x = x16
      pV1.y = y16
      pV2.x = (x12 + x14) / 2
      pV2.y = (y12 + y14) / 2
      //drawGonio(2, pV2, pV1, 1)
      if (pV2.x > 0 && pV2.y > 0) {
        posHandLW = pV2.x;
        posHandLH = pV2.y;
      }
      drawBase(posHandLW, posHandLH, 90, 270);
      drawText(5, pV1, width / 2, 125);
      drawTracers(5, pV1, posHandLW, posHandLH);

      let v0 = createVector(x12, y12);
      let v1 = createVector(x14, y14);
      //drawArrow(v0, v1, 'red');
      let v2 = createVector(x16 - x12, y16 - y12);
      //drawArrow(v0, v2, 'blue');
      let angleBetween = v1.angleBetween(v2);
      noStroke();
      text(
        'angle between: ' +
        angleBetween.toFixed(2) +
        ' radians or ' +
        degrees(angleBetween).toFixed(2) +
        ' degrees',
        10,
        50,
        90,
        50
      );

      break;
    case (nSwitch === 1):
      stroke(255, 255, 0, 128)
      fill(255, 255, 0, 128);
      sMess = "CODO PEGADO, ROTAR HACIA FUERA";
      txClocalUp = "ROTACIÓN EXTERNA DE HOMBRO";
      //txClocalDown = "levante el brazo por delante";
      beginShape();
      curveVertex(x0, y0);
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex(x11, y11);
      curveVertex(x13, y13);
      curveVertex(x15, y15);
      curveVertex(x17, y17);
      curveVertex(x19, y19);
      curveVertex(x21, y21);
      curveVertex(x15, y15);
      curveVertex(x13, y13);
      curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      endShape();
      noStroke();
      fill(255);
      pV1 = createVector(0, 0, 0);
      pV2 = createVector(0, 0, 0);
      pV1.x = x15
      pV1.y = y15
      pV2.x = (x11 + x13) / 2
      pV2.y = (y11 + y13) / 2
      //drawGonio(2, pV2, pV1, 1)
      if (pV2.x > 0 && pV2.y > 0) {
        posHandLW = pV2.x;
        posHandLH = pV2.y;
      }
      drawBase(posHandLW, posHandLH, 90, 270);
      drawText(5, pV1, width / 2, 125);
      drawTracers(5, pV1, posHandLW, posHandLH);
      break;
    case (nSwitch === 2):
      stroke(255, 0, 0, 128)
      fill(255, 0, 0, 128);
      sMess = "CODO SEPARADO, ROTAR HACIA FUERA";
      txClocalUp = "ROTACIÓN EXTERNA DE HOMBRO";
      //txClocalDown = "levante el brazo lateralmente";
      beginShape();
      curveVertex(x0, y0);
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex(x12, y12);
      curveVertex(x14, y14);
      curveVertex(x16, y16);
      curveVertex(x18, y18);
      curveVertex(x20, y20);
      curveVertex(x22, y22);
      curveVertex(x16, y16);
      curveVertex(x14, y14);
      curveVertex((x12 + (x11 + x12) / 2) / 2, (y12 + (y11 + y12) / 2) / 2)
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      endShape();
      noStroke();
      fill(255);
      pV1 = createVector(0, 0, 0);
      pV2 = createVector(0, 0, 0);
      pV1.x = x16
      pV1.y = y16
      pV2.x = (x12 + x14) / 2
      pV2.y = (y12 + y14) / 2
      //drawGonio(2, pV2, pV1, 1)
      if (pV2.x > 0 && pV2.y > 0) {
        posHandLW = pV2.x;
        posHandLH = pV2.y;
      }
      drawBase(posHandLW, posHandLH, 180, 0);
      //drawBase(posHandLW, posHandLH, 90, 270);
      drawText(2, pV1, width / 2, 125);
      drawTracers(2, pV1, posHandLW, posHandLH);
      break;
    case (nSwitch === 3):
      stroke(255, 0, 0, 128)
      fill(255, 0, 0, 128);
      sMess = "CODO SEPARADO, ROTAR HACIA FUERA";
      txClocalUp = "ROTACIÓN EXTERNA DE HOMBRO";
      //txClocalDown = "levante el brazo lateralmente";
      beginShape();
      curveVertex(x0, y0);
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex(x11, y11);
      curveVertex(x13, y13);
      curveVertex(x15, y15);
      curveVertex(x17, y17);
      curveVertex(x19, y19);
      curveVertex(x21, y21);
      curveVertex(x15, y15);
      curveVertex(x13, y13);
      curveVertex((x11 + (x11 + x12) / 2) / 2, (y11 + (y11 + y12) / 2) / 2)
      curveVertex((x11 + x12) / 2, (y11 + y12) / 2)
      endShape();
      noStroke();
      fill(255);
      pV1 = createVector(0, 0, 0);
      pV2 = createVector(0, 0, 0);
      pV1.x = x15
      pV1.y = y15
      pV2.x = (x11 + x13) / 2
      pV2.y = (y11 + y13) / 2
      //drawGonio(2, pV2, pV1, 1)
      if (pV2.x > 0 && pV2.y > 0) {
        posHandLW = pV2.x;
        posHandLH = pV2.y;
      }
      //drawBase(posHandLW, posHandLH, 90, 270);
      drawBase(posHandLW, posHandLH, 0, 180);
      drawText(2, pV1, width / 2, 125);
      drawTracers(2, pV1, posHandLW, posHandLH);
      break;
  }
}

function drawArrow(base, vec, myColor) {
  push();
  stroke(myColor);
  strokeWeight(3);
  fill(myColor);
  translate(base.x, base.y);
  line(0, 0, vec.x, vec.y);
  rotate(vec.heading());
  let arrowSize = 7;
  translate(vec.mag() - arrowSize, 0);
  triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
  pop();
}