Skip to content

Instantly share code, notes, and snippets.

@Nekodigi
Created March 10, 2020 14:20
Show Gist options
  • Select an option

  • Save Nekodigi/81bc575522908431d5896b4de95cb380 to your computer and use it in GitHub Desktop.

Select an option

Save Nekodigi/81bc575522908431d5896b4de95cb380 to your computer and use it in GitHub Desktop.
Triangle fromT;
Triangle toT;
void setup(){
fullScreen();
strokeWeight(20);
ArrayList<PVector> vertices = polygon(-width/4, 0, 3, 1000);
fromT = new Triangle(vertices.get(0), vertices.get(1), vertices.get(2));
toT = new Triangle(new PVector(width/4, -height/4), new PVector(width/2, height/4), new PVector(width/4, height/4));
}
void draw(){
background(255);
translate(width/2, height/2);
fromT.P = new PVector(mouseX-width/2, mouseY-height/2);
fromT.show();
PVector uvw = fromT.getUVW();
toT.setUVW(uvw);
toT.show();
}
class Triangle{
PVector A, B, C;
PVector P = new PVector();;
Triangle(PVector A, PVector B, PVector C){
this.A = A;
this.B = B;
this.C = C;
}
PVector getUVW(){
float ABCArea = getArea();
float APBArea = new Triangle(A, P, B).getArea();
float BPCArea = new Triangle(B, P, C).getArea();
float CPAArea = new Triangle(C, P, A).getArea();
float u = CPAArea/ABCArea;
float v = APBArea/ABCArea;
float w = BPCArea/ABCArea;
return new PVector(u, v, w);
}
void setUVW(PVector uvw){//xyz >= uvw
P = PVector.add(PVector.mult(A, uvw.z), PVector.mult(B, uvw.x)).add(PVector.mult(C, uvw.y));
}
float getArea(){//with Heon's formula
float a = PVector.dist(B, C);
float b = PVector.dist(C, A);
float c = PVector.dist(A, B);
float s = (a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
void show(){
triangle(A, B, C);
circle(P, 100);
}
}
void triangle(PVector A, PVector B, PVector C){
triangle(A.x, A.y, B.x, B.y, C.x, C.y);
}
void circle(PVector P, float r){
ellipse(P.x, P.y, r, r);
}
ArrayList<PVector> polygon(float x, float y, int n, float r) {
ArrayList<PVector> result = new ArrayList<PVector>();
for (float i = 0; i < n; i++) {
result.add(new PVector(x+sin(i/n*TWO_PI)*r, y-cos(i/n*TWO_PI)*r));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment