/**************************************************** * RESOLUTION OF A TRIANGLE * * ------------------------------------------------- * * Given three side or angle elements of a triangle * * out of six, this program will determine the mis- * * sing elements and calculate the surface. The not * * given elements must be put to zero. * * ------------------------------------------------- * * Ref.: "Mathématiques en Turbo-Pascal By M. Ducamp * * and A. Reverchon (vol 2), Eyrolles, Paris, 1988" * * [BIBLI 05]. * * ------------------------------------------------- * * SAMPLE RUN: * * * * RESOLUTION OF A TRIANGLE * * * * A = 18 * * B = 0 * * C = 0 * * * * Angle unit: PI = 180 * * * * TA (angle opposite to side A) = 0 * * TB (angle opposite to side B) = 110 * * TC (angle opposite to side C) = 52.2 * * * * * * A = 18.0000000000 * * B = 55.3311316842 * * C = 46.5260342304 * * * * TA = 17.8000000000 * * TB = 110.0000000000 * * TC = 52.2000000000 * * * * Surface: 393.4815393691 * * * * C++ version by J-P Moreau. * * (www.jpmoreau.fr) * ****************************************************/ #include #include #define PI 3.14159265359 double a,b,c,ta,tb,tc,one_pi; int two; //=0 1 solution, =1 2 solutions char rep[2]; double SQR(double x) { return x*x; } void Exchange(int i, int j, double *angle, int *index, double *side) { double r; int n; n=index[i]; index[i]=index[j]; index[j]=n; r=angle[i]; angle[i]=angle[j]; angle[j]=r; r=side [i]; side [i]=side [j]; side [j]=r; } void CalculateAngle(int i,double *angle,double *side) { int j,k; double r; j=1+(i % 3); k=1+(j % 3); r=(SQR(side[j])+SQR(side[k])-SQR(side[i]))/2/side[j]/side[k]; angle[i]=PI/2-atan(r/sqrt(1-r*r)); } bool Triangle1(double *a, double *b, double *c, double *ta, double *tb, double *tc, double *s) { double side[4]; double angle[4]; int index[4]; int i,nbside,nbangle; double r; //------------ prepare index tables ------------- side[1]=*a; angle[1]=*ta; side[2]=*b; angle[2]=*tb; side[3]=*c; angle[3]=*tc; for (i=1; i<4; i++) index[i]=i; //------------ verify consistency of data ------- nbside=0; nbangle=0; //angles must be in 0,PI and sides > 0 for (i=1; i<4; i++) if (angle[i]<0 || angle[i]>PI || side[i]<0) return 0; for (i=1; i<4; i++) if (side[i]>0) nbside++; for (i=1; i<4; i++) if (angle[i]>0) nbangle++; if (nbside==0 || nbside+nbangle!=3 || angle[1]+angle[2]+angle[3]>PI) return 0; //end verify consistency of data switch(nbside) { //-------------------- 1 side given ----------- case 1: { for (i=1;i<4; i++) if (angle[i]==0) //calculate missing angle angle[i]=PI-angle[1]-angle[2]-angle[3]; if (side[2]>0) Exchange(1,2,angle,index,side); // put given side if (side[3]>0) Exchange(1,3,angle,index,side); // in position 1 side[2]=side[1]*sin(angle[2])/sin(angle[1]); side[3]=side[1]*sin(angle[3])/sin(angle[1]); break; } //-------------------- 2 sides given ---------- case 2: { if (side[3]>0) //put unknown side in position 3 if (side[1]>0) Exchange(2,3,angle,index,side); else Exchange(1,3,angle,index,side); if (angle[3]>0) { // a, b, tc given side[3]=sqrt(SQR(*a)+SQR(*b)-2*(*a)*(*b)*cos(angle[3])); for (i=1; i<3; i++) CalculateAngle(i,angle,side); } else { // a, b, ta given if (angle[1]==0) Exchange(1,2,angle,index,side); r=side[2]*sin(angle[1]); if (r>side[1]) return 0; r=sqrt(SQR(side[1])-SQR(r)); if (angle[1] >= PI/2 && side[1] <= side[2]) return 0; if (angle[i] < PI/2 && side[1] < side[2]) { if (two==1) side[3]=side[2]*cos(angle[1]) - r; else { side[3]=side[2]*cos(angle[1]) + r; two= 1; } } else side[3]=side[2]*cos(angle[1]) + r; for (i=2; i<4; i++) CalculateAngle(i,angle,side); } break; } //-------------------- 3 sides given ---------- case 3: { if (*c <= fabs(*a-*b) || *c >= *a+*b) return 0; for (i=1; i<4; i++) CalculateAngle(i,angle,side); } } //case //----------------------- desindex -------------- *a=side[index[1]]; *ta=angle[index[1]]; *b=side[index[2]]; *tb=angle[index[2]]; *c=side[index[3]]; *tc=angle[index[3]]; //---------------------- calculate surface ------ r=(*a+*b+*c)/2; *s=sqrt(r*(r-*a)*(r-*b)*(r-*c)); return 1; // success } // Function Triangle1() void DisplayTriangle() { double xa,xb,xc,xta,xtb,xtc,s; xa=a; xb=b; xc=c; xta=ta; xtb=tb; xtc=tc; if (Triangle1(&xa,&xb,&xc,&xta,&xtb,&xtc,&s)) { printf(" A = %21.10f\n", xa); printf(" B = %21.10f\n", xb); printf(" C = %21.10f\n\n", xc); printf(" TA = %21.10f\n", xta*(one_pi/PI)); printf(" TB = %21.10f\n", xtb*(one_pi/PI)); printf(" TC = %21.10f\n\n", xtc*(one_pi/PI)); printf(" Surface: %21.10f\n", s); } else printf(" Wrong data or no solution found.\n\n"); printf("\n"); } void main() { printf("\n RESOLUTION OF A TRIANGLE\n\n"); printf(" Give three side or angle elements out of six. The other elements\n"); printf(" must be given as zero. The program will determine the missing\n"); printf(" elements and display results.\n\n"); printf(" Side A = "); scanf("%lf",&a); printf(" Side B = "); scanf("%lf",&b); printf(" Side C = "); scanf("%lf",&c); printf("\n Angle unit: PI = "); scanf("%lf",&one_pi); printf("\n TA (angle opposite to side A) = "); scanf("%lf",&ta); printf(" TB (angle opposite to side B) = "); scanf("%lf",&tb); printf(" TC (angle opposite to side C) = "); scanf("%lf",&tc); ta=ta*PI/one_pi; tb=tb*PI/one_pi; tc=tc*PI/one_pi; printf("\n\n"); two=0; DisplayTriangle(); if (two==1) { printf(" There is a second triangle solution:\n"); printf(" Any key + to continue... "); scanf("%s",rep); printf("\n"); DisplayTriangle(); } } // end of file triangle.cpp