> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.8$ttnY7HVvZ/rev.3208
 * 
 * authors: 
 *   
 *   
 *   Sebastian Rieger
 *   
 *   
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



    int xa = 30;
    int ya = 40;
    int xb = 200;
    int yb = 10;
    int xc = 0;
    int yc = 200;
    
    int za = 40;
    int zb = 250;
    int zc = 0;
    int h;
    
    int x;
    int y;
void setup() {  // this is run once.  
size(300,300); 

   
} 

void draw() {       
background(255);
x = mouseX;
y = mouseY;
stroke(255,255,0);
ellipse(x,y,4,4);



int dA = sqrt( pow((xa-x),2)+pow( (ya-y),2) );
int dB = sqrt( pow((xb-x),2)+pow( (yb-y),2) );
int dC = sqrt( pow((xc-x),2)+pow( (yc-y),2) );
int dT = dA+dB+dC;

//int angA = 
float ABC = abs(xa * (yb - yc) + xb* (yc - ya) + xc * (ya - yb)) / 2;
double ABC = Math.abs (xa * (yb - yc) + xb * (yc - ya) + xc * (ya - yb));
double ABP = Math.abs (xa * (yb - y) + xb * (y - ya) + x * (ya - yb));
double APC = Math.abs (xa * (y - yc) + x * (yc - ya) + xc * (ya - y));
double PBC = Math.abs (x * (yb - yc) + xb * (yc - y) + xc * (y - yb));

boolean isInTriangle = ABP + APC + PBC == ABC;
/*
float [] gamma = new float [3];
float [][] pos = new float [3][3];
pos[0][0] = xa;
pos[0][1] = ya;
pos[0][2] = za;

pos[1][0] = xb;
pos[1][1] = yb;
pos[1][2] = zb;

pos[2][0] = xc;
pos[2][1] = yc;
pos[2][2] = zc;

 
for(int i  = 0;i<1;i++){
    gamma[i] =
    ( ( (pos[i][0]*pos[i][2]*pos[i][1]*pos[i][1])-(pos[i][0]*pos[i][1]*pos[i][1]*pos[i][2]) ) /
    ( (pos[i][2]*pos[i][2]*pos[i][1]*pos[i][1])-(pos[i][2]*pos[i][1]*pos[i][1]*pos[i][2] ) ) );
    
   // text(gamma[i],40,200+i*30);
    }
    
//xc += 0.03;

float counter1 = ( (x-xa)*(yb*ya) )-( (y-ya)*(xb-xa) );
float denominator1 = -(xb*yc)+(xb*ya)+(xa*yc)+(yb*xc)-(yb*xa)-(ya*xc);
float r = counter1 / denominator1;

float counter2 = x+r*xa-xa-(r*xc);
float denominator2 = (xb-xa);

//float counter2 = (x-xa)-(r*(xc-xa) );
//float denominator2 = xb-xa;

float s = counter2 / denominator2;
// s = 0.5;
// r = 0.5;

float theHeight = za+s*(zb-za)+(r*(zc-za) );
int wert1 = theHeight;
int wert2 = s;
int wert3 = r;

text(wert3 ,x,y+40);

*/



float Ax = xa;
float Ay = za;
float Az = ya;

float Bx = xb;
float By = zb;
float Bz = yb;

float Cx = xc;
float Cy = zc;
float Cz = yc;

float Px = x;
float Py;
float Pz = y;

float r,s;

float VecBx = Bx-Ax;
float VecBy = By-Ay;
float VecBz = Bz-Az;

float VecCx = Cx-Ax;
float VecCy = Cy-Ay;
float VecCz = Cz-Az;

float nenner1 = ( (Px-Ax)*VecBz)-( (Pz-Az)*VecBx);
float zaehler1 = ( (VecCx*VecBz)-(VecCz*VecBx) );
r = nenner1 / zaehler1;
xc += 0.03;

float nenner2 = Px-Ax-r*VecCx;
float zaehler2 = VecBx;
s = nenner2 / zaehler2;


Py = Ay +s*VecBy+r*VecCy;



text(Py,x,y+40);







//theHeight
if(isInTriangle){
fill(0,255,0);
text("Point is inside!",200,230);
}else{
    fill(255,0,0);
    text("Point is outside!",200,230);
    }
    fill(0);
    
text(round(dA)+" to A",200,100);
text(round(dB)+" to B",200,130);
text(round(dC)+" to C",200,160);
text(round(dT)+" Total",200,190);

/*
int xToA = abs(x-xa);
int xToB = abs(x-xb);
int xAtoB = xToA+xToB;
int xAtoBP = xToA /xAtoB ;
int yAtoB = (1-xAtoBP)*ya+xAtoBP*yb;
*/

int xAB = abs(xa-xb);
int yAB = abs(ya-yb);
int xAC = abs(xa-xc);
int yAC = abs(ya-yc);

int xAP = abs(xa-x);
int yAP = abs(ya-y);
//int Vec
text(xAP,200,200);

int partA = 0;
int partB = 0;
int partC = (dA+dB)/dT;
h = (partA)*za+(partB)*zb+(partC)*zc;
//text(xAtoB ,200,100);

stroke(0);
strokeWeight(3);

  line(xa,ya,xb,yb);
  line(xa,ya,xc,yc);
  line(xb,yb,xc,yc);
 
  
  textSize(20);
  fill(0,155,155);
  text("A "+za,xa,ya);
  text("B "+zb,xb,yb);
  text("C "+zc,xc,yc);
 /* 
  //lines to mouse
  stroke(0,255,0);
  strokeWeight(1);
  line(xa,ya,x,y);
  line(xb,yb,x,y);
  line(xc,yc,x,y);
  */
  myx1 = x;
  myx2 = 100;
    myx3 = 0;
  myx4 = 100;
  
    myy1 = y;
  myy2 = 100;
    myy3 = 100;
  myy4 = 0;
  float thex = intersectionX(myx3,myy3,myx4,myy4,myx1,myy1,myx2,myy2);
float they = intersectionY(myx3,myy3,myx4,myy4,myx1,myy1,myx2,myy2);

/*
line(myx3,myy3,myx4,myy4);
line(myx1,myy1,myx2,myy2);
stroke(0,255,0);
ellipse(thex,they,15,15);
  */
}
float intersectionX(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4){
flaot t = (y4 * (x1 - x3) - x4 * (y1 - y3)) / (x4 * y2 - y2 * x2);

//    x = c->x + (d->x - c->x) * t;
  //  float y = c->y + (d->y - c->y) * t;
 //   return xt;
}
float intersectionY(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4){
 

    // Zaehler
    float zx = (x1 * y2 - y1 * x2) - (x1 - x2) * (x3 * y4 - y3 * x4);
    float zy = (x1 * y2 - y1 * x2) - (y1 - y2) * (x3 * y4 - y3 * x4);
      
    // Nenner
    float n = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
    
    // Koordinaten des Schnittpunktes
    float xt = zx/n;
    float yt = zy/n;
    print (x+"  "+y);
    return yt;
}