LNXdev.com

Triangle solving function

Print missing parameter on screen

This function prints the result on the notification bar on machine view. This function can easily be modified to return the value back to the calling program.

PROC TRIA(REAL valueAngle,REAL componentLength,REAL valueLength)

; TRIANGLE SOLVER
; V1.0, 14th JUL 2010 ( Juha Wisakanto )
; Resolve all components of a triangle using known angle and length

; Variable definitions

DEF BOOL kill
DEF REAL resultA
DEF REAL resultB
DEF REAL resultC
DEF REAL resultAng



; Input control, secure.

IF valueAngle <= 0 GOTOF ERR02
IF valueAngle >= 90 GOTOF ERR04
IF componentLength <= 0 GOTOF ERR03
IF componentLength > 3 GOTOF ERR03
IF valueLength <= 0 GOTOF ERR01

; Arithmetics

; componentLength takes catheti or hypotenuse as input
; 1 = Adjacent side
; 2 = Opposite side
; 3 = Hypotenuse

; Calculate triangle if adjacent side/cathetus is known
IF componentLength == 1
   resultB = tan(valueAngle) * valueLength
   resultC = valueLength / cos(valueAngle)
   resultAng = 180 - 90 - valueAngle
   MSG("Opposite cathetus: " <<resultB <<"; Hypotenuse: " <<resultC <<"; Opposite angle: " <<resultAng <<"deg.")
   M0
   GOTOF END
ENDIF

; Calculate when opposite side/cathetus is known
IF componentLength == 2
   resultA = valueLength / tan(valueAngle)
   resultC = valueLength / sin(valueAngle)
   resultAng = 180 - 90 - valueAngle
   MSG("Closest cathetus: "<<resultA <<"; Hypotenuse: "<<resultC <<"; Opposite angle: "<<resultAng <<"deg.")
   M0
   GOTOF END
ENDIF

; Hypotenuse is known
IF componentLength == 3
   resultA = valueLength * cos(valueAngle)
   resultB = valueLength * sin(valueAngle)
   resultAng = 180 - 90 - valueAngle
   MSG("Closest cathetus: "<<resultA <<"; Opposite cathetus "<<resultB<<"; Opposite angle: "<<resultAng<<" deg.")
   M0
   GOTOF END
ENDIF

; Error handling
ERR01:
   MSG("Length is equal or less than zero, cannot calculate.")
   M0
   GOTOF END

ERR02:
   MSG("Error: Angle is zero or less, cannot calculate.")
   M0
   GOTOF END

ERR03:
   MSG("Error: Known length must be 1,2 or 3. No.1 is for closest cathetus, no.2 for opposite and no.3 for hypotenuse.")
   M0
   GOTOF END
   
ERR04:
   MSG("Error: Angle is over 90 degrees, cannot calculate.")
   M0
   GOTOF END
; The End
END:
RET