float tolerance = 10; void myActions() { // actions to be executed at each frame scribe("DEREK TATUM mod of [Jarek Rossignac's edge / edge intersection (general position)]"); // 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); // set fill color to black (for writing text in the windows) strokeWeight(3); // set stroke width to 3 stroke(green); // set current color to blue pt A=P[0]; A.show(2); A.showLabel("A"); // Assign variable point A to P[0], show it as a disk of radius 4, write its label "A" next to it pt B=P[1]; B.show(2); B.showLabel("B"); pt C=P[2]; C.show(2); C.showLabel("C"); A.to(B); // draw a line from A to B in current color (see TAB geo2D, end of class pt) B.to(C); C.to(A); stroke(blue); pt D=P[3]; D.show(2); D.showLabel("D"); pt E=P[4]; E.show(2); E.showLabel("E"); D.to(E); stroke(orange); //showEdgeEdgeIntersection(A,B,C,D,E); // WHERE THE ACTUAL INTERSECTION IS COMPUTED AND DISPLAYED (see below) fill(black); int count = 0; //pt P, Q; pt P = P(0,0); pt Q = P(0,0); ///////////////////////////////////// if (edgesCross(A,B,D,E)) { P = computeIntersectionPoint(A,B,D,E); stroke(orange); P.show(2); count++; } if (edgesCross(B,C,D,E)) { if (count==1){ Q = computeIntersectionPoint(B,C,D,E); stroke(orange); Q.show(2); } else { P = computeIntersectionPoint(B,C,D,E); stroke(orange); P.show(2); } count++; } if (edgesCross(C,A,D,E)) { if (count==1){ Q = computeIntersectionPoint(C,A,D,E); stroke(orange); Q.show(2); } else { P = computeIntersectionPoint(C,A,D,E); stroke(orange); P.show(2); } count++; } ////////////////////////// if(count==0){ if (PinT(A,B,C,D)&&PinT(A,B,C,E)){ stroke(red); D.to(E); D.show(2); E.show(2); //line(D.x,D.y,E.x,E.y); } }//DONE with count==0 ///////////////////////////////////// if (count==1){ stroke(red); if (PinT(A,B,C,D)){ D.to(P); D.show(2); } if (PinT(A,B,C,E)){ E.to(P); E.show(2); } }//DONE with count==1 /////////////////////////////////// if (count==2){ stroke(red); P.to(Q); }//DONE with count==2 toleranceImplementation(A,B,C,D,E,P,Q); }; /////////////////////////////////////////// void toleranceImplementation(pt A, pt B, pt C, pt D, pt E, pt P, pt Q) { // show intersection point between two edges if it exists if (PinE(A,B,P)||PinE(A,C,P)||PinE(B,C,P)) { stroke(orange); P.show(4); } if (PinE(A,B,Q)||PinE(A,C,Q)||PinE(B,C,Q)) { stroke(orange); Q.show(4); } if (PinE(A,B,D)||PinE(A,C,D)||PinE(B,C,D)) { stroke(cyan); D.show(4); } if (PinE(A,B,E)||PinE(A,C,E)||PinE(B,C,E)) { stroke(cyan); E.show(4); } } //from slide 25 of geometry2D.ppt pt computeIntersectionPoint(pt A, pt B, pt D, pt E) { // compute intersection point P (between edges AB and DE) pt S = A.clone(); // = new pt(A.x, A.y); vec T = V(A,B); vec QS = V(D,A); vec N = R(V(D,E)); float t = -(dot(QS,N))/dot(T,N); //pt makeTranslatedBy(float s, vec V) {return(new pt(x + s*V.x, y + s*V.y));}; pt P = S.makeTranslatedBy(t,T); return P; } boolean ordered(pt A, pt B, pt C){return (00); } boolean edgesCross(pt A, pt B, pt C, pt D) {return (dot(U(R(V(A,B))),V(A,C))>0 != 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) ; } //boolean MYedgesCross(pt A, pt B, pt C, pt D, pt E) {return (dot(U(R(V(A,B))),V(A,D))>0 != dot(U(R(V(A,B))),V(A,E))>0) // tests whether two edges cross // && (dot(U(R(V(D,E))),V(D,A))>0 != dot(U(R(V(D,E))),V(D,B))>0) ; } boolean PinT(pt A,pt B,pt C,pt P) {return (right1(A,B,P)== right1(B,C,P)) && (right1(A,B,P)== right1(C,A,P)) ;} boolean PinE(pt A,pt B,pt C) { // is C on AB? vec U=V(A,B); return ( (abs(dot(R(U.makeUnit()),V(A,C))) < tolerance) && //makeUnit normalizes U (0 < dot(V(A,B),V(A,C))) && (dot(V(A,B),V(A,C)) < dot(V(A,B),V(A,B))) ) ; } /// /* boolean PinEOLD(pt A,pt B,pt C) { // is C on AB? return ( dot(R(V(A,B)),V(A,C))==0&& (0 < dot(V(A,B),V(A,C))) && (dot(V(A,B),V(A,C)) < dot(V(A,B),V(A,B))) ) ; } */ //boolean edgesCross(pt A, pt B, pt C, pt D) {return (dot(U(R(V(A,B))),V(A,C))>0 != 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) ; } //boolean edgesCross(pt A, pt B, pt C, pt D, pt E) {return true;}