'***************************************************** '* 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.: "Mathematiques 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.0000000000000 * '* B = 55.3311291183785 * '* C = 46.5260338924988 * '* * '* TA = 17.8000000000000 * '* TB = 110.0000000000000 * '* TC = 52.2000000000000 * '* * '* Surface: 393.481559106180 * '* * '* Basic version by J-P Moreau. * '* (www.jpmoreau.fr) * '***************************************************** defdbl a-h,o-z defint i-n PI=3.14159265359 Dim angle(3),indx(3),side(3) 'integer itwo cls print print " RESOLUTION OF A TRIANGLE" print print " Give three side or angle elements out of six. The other elements" print " must be given as zero. The program will determine the missing" print " elements and display results." print input " Side A = ", a input " Side B = ", b input " Side C = ", c print input " Angle unit: PI = ", onepi if onepi=0 then opepi=PI print input " TA (angle opposite to side A) = ", ta input " TB (angle opposite to side B) = ", tb input " TC (angle opposite to side C) = ", tc ta=ta*PI/onepi: tb=tb*PI/onepi: tc=tc*PI/onepi print 'save original values (in case of two solutions) xa=a:xb=b:xc=c:xta=ta:xtb=tb:xtc=tc itwo=0 'call DisplayTriangle(a,b,c,ta,tb,tc,onepi) with itwo=0 gosub 2000 if itwo=1 then print " There is a second triangle solution:" input "", ch\$ print 'restore original values a=xa:b=xb:c=xc:ta=xta:tb=xtb:tc=xtc 'call DisplayTriangle(a,b,c,ta,tb,tc,onepi) with itwo=1 gosub 2000 end if END 'Subroutine Exchange(i,j) 1000 n=indx (i): indx (i)=indx (j): indx (j)=n r=angle(i): angle(i)=angle(j): angle(j)=r r=side (i): side (i)=side (j): side (j)=r return 'Subroutine CalculateAngle(i) 1100 j=1+i MOD 3: k=1+j MOD 3 r=side(j)*side(j)+side(k)*side(k)-side(i)*side(i) r=r/2/side(j)/side(k) angle(i)=PI/2-ATN(r/SQR(1-r*r)) return 'Function Triangle1(a,b,c,ta,tb,tc,s,two) '------------ prepare index tables ------------ 1200 side(1)=a: angle(1)=ta side(2)=b: angle(2)=tb side(3)=c: angle(3)=tc for i=1 to 3 indx(i)=i next '------------ verify consistency of data ------- Triangle1%=0: nbside=0: nbangle=0 'angles must be in 0,PI and sides > 0 for i=1 to 3 if angle(i)<0 or angle(i)>PI or side(i)<0 then return end if next for i=1 to 3 if side(i)>0 then nbside=nbside+1 next for i=1 to 3 if angle(i)>0 then nbangle=nbangle+1 next if nbside=0 or nbside+nbangle<>3 or angle(1)+angle(2)+angle(3)>PI then return end if 'end verify consistency of data '-------------------- 1 side given ----------- if nbside=1 then for i=1 to 3 if angle(i)=0 then 'calculate missing angle angle(i)=PI-angle(1)-angle(2)-angle(3) end if next if side(2)>0 then 'put given side in position 1 i=1: j=2: gosub 1000 'call Exchange(1,2) end if if side(3)>0 then i=1: j=3: gosub 1000 'call Exchange(1,3) end if side(2)=side(1)*sin(angle(2))/sin(angle(1)) side(3)=side(1)*sin(angle(3))/sin(angle(1)) end if '-------------------- 2 sides given ---------- if nbside=2 then if side(3)>0 then 'put unknown side in position 3 if side(1)>0 then i=2: j=3: gosub 1000 'call Exchange(2,3) else i=1: j=3: gosub 1000 'call Exchange(1,3) end if end if if angle(3)>0 then 'a, b, tc given side(3)=SQR(a*a+b*b-2*a*b*cos(angle(3))) for i=1 to 2 gosub 1100 'call CalculateAngle(i) next else 'a, b, ta given if angle(1)=0 then i=1: j=2: gosub 1000 'call Exchange(1,2) end if r=side(2)*sin(angle(1)) if r>side(1) then return r=SQR(side(1)*side(1)-r*r) if angle(1) >= PI/2 and side(1) <= side(2) then return if angle(1) < PI/2 and side(1) < side(2) then if itwo=1 then side(3)=side(2)*cos(angle(1)) - r else side(3)=side(2)*cos(angle(1)) + r itwo=1 end if else side(3)=side(2)*cos(angle(1)) + r end if for i=2 to 3 gosub 1100 'call CalculateAngle(i) next end if end if '-------------------- 3 sides given ---------- if nbside=3 then if c<=ABS(a-b) or c>=a+b then return for i=1 to 3 gosub 1100 'call CalculateAngle(i) next end if '----------------------- desindex --------------- a=side(indx(1)): ta=angle(indx(1)) b=side(indx(2)): tb=angle(indx(2)) c=side(indx(3)): tc=angle(indx(3)) '---------------------- calculate surface ------- r=(a+b+c)/2 s=SQR(r*(r-a)*(r-b)*(r-c)) Triangle1%=1 'success return 'Subroutine DisplayTriangle(a,b,c,ta,tb,tc,one_pi) 2000 gosub 1200 'call Triangle1(a,b,c,ta,tb,tc,s) F\$="###.######" print if Triangle1%=1 then '1=success 0=fail print " A = "; : print using F\$; a print " B = "; : print using F\$; b print " C = "; : print using F\$; c print print " TA = "; : print using F\$; ta*(onepi/PI) print " TB = "; : print using F\$; tb*(onepi/PI) print " TC = "; : print using F\$; tc*(onepi/PI) print print " Surface: "; : print using F\$; s else print " Wrong data or no solution found." end if print return 'end of file triangle.bas