void myActions() { // actions to be executed at each frame scribe("Jarek Rossignac's triangle / tringle Booleans (starting point for project)"); //*:01 prints text at top of the graphic window (see TAB UI), line 0 if ((mousePressed)&&(!keyPressed)) dragPoint(); // while mouse pressed moves the selected point by mouse displacement (in TAB polygon) fill(black); //*:05 reset fill color to black (for writing text in the windows) pt A=P[0], B=P[2], C=P[4], D=P[1], E=P[3], F=P[5]; stroke(dblue); fill(cyan,200); strokeWeight(2); showTriangle(A,B,C); showTriangle(D,E,F); A.show(2); B.show(2); C.show(2); D.show(2); E.show(2); F.show(2); fill(black); A.showLabel("A"); B.showLabel("B"); C.showLabel("C"); D.showLabel("D"); E.showLabel("E"); F.showLabel("F"); // INTERSECTION pushMatrix(); translate(width/2,0); stroke(dblue); noFill(); strokeWeight(2); showTriangle(A,B,C); showTriangle(D,E,F); fill(red,220); stroke(black); strokeWeight(4); showIntersection(A,B,C,D,E,F); popMatrix(); // DIFFERENCE pushMatrix(); translate(0,height/2); stroke(dblue); noFill(); strokeWeight(2); showTriangle(A,B,C); showTriangle(D,E,F); fill(blue,220); stroke(black); strokeWeight(4); showDifference(A,B,C,D,E,F); popMatrix(); // UNION pushMatrix(); translate(width/2,height/2); stroke(dblue); noFill(); strokeWeight(2); showTriangle(A,B,C); showTriangle(D,E,F); fill(green,220); stroke(black); strokeWeight(4); showUnion(A,B,C,D,E,F); popMatrix(); }; void showTriangle(pt A, pt B, pt C) {beginShape(); A.v(); B.v(); C.v(); endShape(CLOSE); }; void showIntersection(pt A, pt B, pt C, pt D, pt E, pt F) { // Jarek's cheat! Replace with proper code for computing the loop(s) of the intersection and rendering them beginShape(); EE(A,B,D,E).v(); EE(B,C,D,E).v(); EE(B,C,E,F).v(); EE(C,A,E,F).v(); EE(C,A,F,D).v(); EE(A,B,F,D).v(); endShape(CLOSE); }; void showDifference(pt A, pt B, pt C, pt D, pt E, pt F) { // Jarek's cheat! Replace with proper code for computing the loop(s) of the difference and rendering them showTriangle(A,B,C); stroke(white); fill(white); showTriangle(D,E,F); }; void showUnion(pt A, pt B, pt C, pt D, pt E, pt F) { // Jarek's cheat! Replace with proper code for computing the loop(s) of the union and rendering them strokeWeight(4); showTriangle(A,B,C); showTriangle(D,E,F); noStroke(); showTriangle(A,B,C); showTriangle(D,E,F); }; boolean ordered(pt A, pt B, pt C) {return (00 != dot(U(R(V(A,B))),V(A,D))>0) // tests whether two edges cross && (dot(U(R(V(C,D))),V(C,A))>0 != dot(U(R(V(C,D))),V(C,B))>0) ; } pt EE(pt A, pt B, pt C, pt D) { // returns intersection point between two edges assuming it exists vec T=V(A,B); // make vector T=AB vec N = R(V(C,D)); // makes vector N by rotating CD by 90 degrees vec AC = V(A,C); float t = dot(AC,N) / dot(T,N); // compute intersection parameter return S(A,t,T); // returns intersection. S() computes a point on a line (see TAB geo2D) }