//Square.cc - revised version //ECNA 2012 #include #include #include using namespace std; const bool DEBUG=false; #define IFDB if (DEBUG) #define FOR(i,n) for (int i=0;i=0) return a; else return -1.0*a; } bool IsEq(double a, double b){ // equal func for doubles return myabs(a-b) < 0.0001; } //***** //true if any 3 of P[] are colinear bool IsColinear(){ int rise1, run1, rise2, run2; //1st check P0, P1, P2 rise1 = P[1].y - P[0].y; run1 = P[1].x - P[0].x; rise2 = P[2].y - P[0].y; run2 = P[2].x - P[0].x; //see if slopes equal if(rise1*run2 == rise2*run1) return true; //now P0, P1, P3 rise2 = P[3].y - P[0].y; run2 = P[3].x - P[0].x; //see if slopes equal if(rise1*run2 == rise2*run1) return true; //now P0, P2, P3 rise1 = P[3].y - P[0].y; run1 = P[3].x - P[0].x; rise2 = P[2].y - P[0].y; run2 = P[2].x - P[0].x; //see if slopes equal if(rise1*run2 == rise2*run1) return true; //now P1, P2, P3 rise1 = P[3].y - P[1].y; run1 = P[3].x - P[1].x; rise2 = P[2].y - P[1].y; run2 = P[2].x - P[1].x; //see if slopes equal if(rise1*run2 == rise2*run1) return true; return false; //no colinear } //***** //crude sort for points - get them in cc order void SortPts(){ point temp, temp1, temp2, temp3; double cos1, cos2, cos3; int a,b; for(int i=3;i>0;i--) if (P[i].ycos2 && cos1>cos3){ //P[1] next temp1=P[1]; if(cos2>cos3) { temp2=P[2]; temp3=P[3]; // ..., P2, P3 } else { temp2=P[3]; temp3=P[2]; // ..,P3, P2 } } if(cos2>cos1 && cos2>cos3){ //P[2] next temp1=P[2]; if(cos1>cos3) { temp2=P[1]; temp3=P[3]; } else { temp2=P[3]; temp3=P[1]; } } if(cos3>cos2 && cos3>cos1){ //P[3] next temp1=P[3]; if(cos2>cos1) { temp2=P[2]; temp3=P[1]; } else { temp2=P[1]; temp3=P[2]; } } P[1]=temp1; P[2]=temp2; P[3]=temp3; } //***** void PrintVectors(){ cout<<"vectors p2-p0 and p3-p1: "< dist from P0 side (slope==m) to P1 v.x = P[1].x-P[0].x; v.y = P[1].y-P[0].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; //see that side > length from P0 side (slope==m) to parallel side thru P3 v.x = P[3].x-P[0].x; v.y = P[3].y-P[0].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; //see that side > length from P2 side (slope==m) to parallel side thru P1 v.x = P[1].x-P[2].x; v.y = P[1].y-P[2].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; //see that side > length from P2 side (slope==m) to parallel side thru P3 v.x = P[3].x-P[2].x; v.y = P[3].y-P[2].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; m = -1.0/m; //see that side > length from P1 side (slope==m) to parallel side thru P0 v.x = P[0].x-P[1].x; v.y = P[0].y-P[1].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; //see that side > length from P1 side (slope==m) to parallel side thru P2 v.x = P[2].x-P[1].x; v.y = P[2].y-P[1].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; //see that side > length from P3 side (slope==m) to parallel side thru P0 v.x = P[0].x-P[3].x; v.y = P[0].y-P[3].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; //see that side > length from P3 side (slope==m) to parallel side thru P2 v.x = P[2].x-P[3].x; v.y = P[2].y-P[3].y; altside = sqrt((v.x*v.x)+(v.y*v.y)-((v.x+m*v.y)*(v.x+m*v.y))/(1+m*m)); if(altside>side) return true; return false; } //***** //true if 2 of Pt[] are on one of the 4 ver/horz lines bool TwoOnLine(int R, int L, int hi, int lo){ int cnt=0; FOR(k,4) if(P[k].x==L) cnt++; if (cnt>1) { return true; } cnt=0; FOR(k,4) if(P[k].x==R) cnt++; if (cnt>1) { return true; } cnt=0; FOR(k,4) if(P[k].y==lo) cnt++; if (cnt>1) { return true; } cnt=0; FOR(k,4) if(P[k].y==hi) cnt++; if (cnt>1) { return true; } return false; } //****** //true if there is a P[] on each side of HV square bool OnHVSquare(int R, int L, int hi, int lo){ FOR(k,4){ if((P[k].x>R || P[k].xhi || P[k].y>n; FOR(i,n){ cout<<"Case "<>P[j].x>>P[j].y; //sort so P[0] is lowest (left) and around counterclockwise //1st check in any 3 pts colinear. If so, no square if (IsColinear()){ cout<<"no solution"<P[1].x) L = P[1].x; if (L>P[2].x) L = P[2].x; if (L>P[3].x) L = P[3].x; R = P[0].x; if (R0 && A1>=A2) { if(IsDegenerate(A1,m1)){ cout<<"no solution"<0 ... ) else if(A2>0 && A2>A1){ //check if degenerate if(IsDegenerate(A2,m2)){ cout<<"no solution"<