Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Right Triangle Algorythms
#1
I've been writing a script that basically creates a building in TS7 all by itself. But I ran into a bunch of road blocks but figured out the math and figured I would share a few formulas with everyone to help solve some problems that I know I've been trying to figure out and finally did.

A Right Triangle is a tringle that has one 90 degree corner. The side away from the lowest angle corner is called the Opposite, the side 90 degrees from that is the Adjacent and the longest side is the Hypotenuse. Now just knowing one or two of these dimensions you can calculate and find all the rest of the dimensions of that triangle (these formulas come in handy for all kinds of uses)... here's a diagram:

[Image: righttri.gif]

a = Opposite
b = Adjacent
c = Hypotenuse

To find any of the side lengths knowing the lengths of the other two sides you use the following formulas in QM

Code:
Copy      Help
double Opp, Adj, Hyp, AngA, AngB  ;; This is where we get the results
double O, A, H, Aa, Ab, Tmp       ;; We'll use these for our temporary known values

;// We'll set up the temp values for examples.
O = 15.00
A = 25.00
H = 29.15
Aa = 30.96
Ab = 59.04
def PI 3.1415926535897932384626433832795

Hyp  = sqrt((O*O)+(A*A)) ;; Hyp = Square Root of Opp squared plus Adj squared
Opp  = sqrt((H*H)-(A*A)) ;; Opp = Square Root of Hyp squared minus Adj squared
Adj  = sqrt((H*H)-(O*O)) ;; Adj = Square Root of Hyp squared minus Opp squared

AngA = asin(Opp/Hyp)*180/PI ;; use this if the Opp and Hyp are the only known dimensions
AngA = acos(Adj/Hyp)*180/PI ;; Use this if the Adj and Hyp are the only known dimensions
AngA = atan(Opp/Adj)*180/PI ;; Use this if the App and Adj are the only known dimensions

AngB = 90-AngA

Opp  = Round(Opp 2)
Adj  = Round(Adj 2)
Hyp  = Round(Hyp 2)
AngA = Round(AngA 2)
AngB = Round(AngB 2)

out F"The Opposite   = Original {O} : Calculated {Opp}"
out F"The Adjacent   = Original {A} : Calculated {Adj}"
out F"The Hypotenuse = Original {H} : Calculated {Hyp}"
out F"The Angle A    = Original {Aa} : Calculated {AngA}"
out F"The Angle B    = Original {Ab} : Calculated {AngB}"

Go ahead and use these with your own values and hopefully I've helped someone! Enjoy!
#2
I've done a bit more work on this and figured I would share with everyone that might be interested. The following is a little helper program I wrote that will take any 2 known values for a right triangle and return the results of every other side and angle. It's a quick little program that I built off my previous example above but totally contained! Really comes in handy for doing all the math!

Code:
Copy      Help
/Dialog_Editor

function# hDlg message wParam lParam
if(hDlg) goto messages

BEGIN DIALOG
1 "" 0x90C80AC8 0x0 0 0 188 158 "Right Triangle Calculator"
3 Static 0x54000000 0x0 6 6 176 30 "Enter any two known values of a right triangle to calculate the other values automatically.  Press SOLVE to begin."
4 Edit 0x54030080 0x200 72 44 96 13 ""
5 Edit 0x54030080 0x200 72 62 96 12 ""
6 Edit 0x54030080 0x200 72 80 96 12 ""
7 Edit 0x54030080 0x200 72 98 96 12 ""
9 Static 0x54000000 0x0 16 44 48 13 "Opposite Side"
10 Static 0x54000000 0x0 16 62 48 12 "Adjacent Side"
11 Static 0x54000000 0x0 16 80 48 12 "Hypotenuse"
12 Static 0x54000000 0x0 16 98 48 12 "Angle A or B"
14 ComboBox 0x54230243 0x0 72 116 96 213 "[]1[]2[]3[]4[]5[]Unlimited[]"
15 Button 0x54032000 0x0 16 138 96 14 "SOLVE"
16 Static 0x54000000 0x0 16 116 48 13 "Decimal Places[]"
17 Button 0x54032000 0x0 120 138 48 14 "Cancel"
END DIALOG
DIALOG EDITOR: "" 0x2040202 "*" "" "" ""

showdialog
str controls = "4 5 6 7 14"
str e4 e5 e6 e7 cb14
cb14 = "0[]1[]&2[]3[]4[]5[]Unlimited"
if(!ShowDialog("RTC" 0 &controls)) ret

goto beginmacro

messages
sel message
    case WM_INITDIALOG DT_Init(hDlg lParam); ret 1
    case WM_DESTROY DT_DeleteData(hDlg)
    case WM_COMMAND goto messages2
ret
messages2
ARRAY(str)- numbers
sel wParam
    case CBN_SELENDOK<<15|3
    _i=CB_SelectedItem(lParam)
    numbers[_i].setwintext(id(4 hDlg))
    
    case IDOK DT_Ok hDlg
    case IDCANCEL DT_Cancel hDlg
    case EN_CHANGE<<16|8
    
ret 1

beginmacro

double Opp, Adj, Hyp, AngA, AngB  ;; This is where we get the results
double O, A, H, Aa, Ab, Ac, Tmp   ;; We'll use these for our temporary known values
def PI 3.1415926535897932384626433832795

O = val(e4)
A = val(e5)
H = val(e6)
Aa = val(e7)
int Rd = val(cb14)

if Aa = 0
    if O && A
        Aa=atan(O/A)
    if A && H
        Aa=acos(A/H)
    if H && O
        Aa=asin(O/H)

Hyp = H
Opp = O
Adj = A
AngA = Aa
AngB = Ab

if Opp && Hyp
    AngA = asin(Opp/Hyp)*180/PI ;; use this if the Opp and Hyp are the only known dimensions
if Adj && Hyp
    AngA = acos(Adj/Hyp)*180/PI ;; Use this if the Adj and Hyp are the only known dimensions
if Opp && Adj
    AngA = atan(Opp/Adj)*180/PI ;; Use this if the App and Adj are the only known dimensions

if AngA == 0
    AngA = 90 - AngB
if AngB == 0
    AngB = 90 - AngA
    
if (AngB+AngA) == 0
    end

Aa = (AngA*PI)/180
Ab = (AngB*PI)/180

if Opp
    if Hyp == 0
        A = sin(Aa)
        Hyp = Opp/A
    if Adj == 0
        A = tan(Aa)
        Adj = Opp/A
if Adj
    if Hyp == 0
        A = cos(Aa)
        Hyp = Adj/A
    if Opp == 0
        A = tan(Aa)
        Opp = A*Adj
if Hyp
    if Adj == 0
        A = cos(Aa)
        Adj = A*Hyp
    if Opp == 0
        A = sin(Aa)
        Opp = A*Hyp

Opp  = Round(Opp Rd)
Adj  = Round(Adj Rd)
Hyp  = Round(Hyp Rd)
AngA = Round(AngA Rd)
AngB = Round(AngB Rd)

out "--------------------------------------------------"
out F"The Opposite   = {Opp}"
out F"The Adjacent   = {Adj}"
out F"The Hypotenuse = {Hyp}"
out F"The Angle A    = {AngA}"
out F"The Angle B    = {AngB}"

mes (F"The Opposite     : {Opp}[]The Adjacent     : {Adj}[]The Hypotenuse: {Hyp}[]The Angle A       : {AngA}[]The Angle B       : {AngB}")
#3
awesome work but hard to paste into QM so reformatted for QM Forum. S

Function RTC
Code:
Copy      Help
/Dialog_Editor

function# hDlg message wParam lParam
if(hDlg) goto messages

;BEGIN DIALOG
;1 "" 0x90C80AC8 0x0 0 0 188 158 "Right Triangle Calculator"
;3 Static 0x54000000 0x0 6 6 176 30 "Enter any two known values of a right triangle to calculate the other values automatically.  Press SOLVE to begin."
;4 Edit 0x54030080 0x200 72 44 96 13 ""
;5 Edit 0x54030080 0x200 72 62 96 12 ""
;6 Edit 0x54030080 0x200 72 80 96 12 ""
;7 Edit 0x54030080 0x200 72 98 96 12 ""
;9 Static 0x54000000 0x0 16 44 48 13 "Opposite Side"
;10 Static 0x54000000 0x0 16 62 48 12 "Adjacent Side"
;11 Static 0x54000000 0x0 16 80 48 12 "Hypotenuse"
;12 Static 0x54000000 0x0 16 98 48 12 "Angle A or B"
;14 ComboBox 0x54230243 0x0 72 116 96 213 "[]1[]2[]3[]4[]5[]Unlimited[]"
;15 Button 0x54032000 0x0 16 138 96 14 "SOLVE"
;16 Static 0x54000000 0x0 16 116 48 13 "Decimal Places[]"
;17 Button 0x54032000 0x0 120 138 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040202 "*" "" "" ""

;showdialog
str controls = "4 5 6 7 14"
str e4 e5 e6 e7 cb14
cb14 = "0[]1[]&2[]3[]4[]5[]Unlimited"
if(!ShowDialog("RTC" 0 &controls)) ret

goto beginmacro

;messages
sel message
,case WM_INITDIALOG DT_Init(hDlg lParam); ret 1
,case WM_DESTROY DT_DeleteData(hDlg)
,case WM_COMMAND goto messages2
ret
;messages2
ARRAY(str)- numbers
sel wParam
,case CBN_SELENDOK<<15|3
,_i=CB_SelectedItem(lParam)
,numbers[_i].setwintext(id(4 hDlg))
,
,case IDOK DT_Ok hDlg
,case IDCANCEL DT_Cancel hDlg
,case EN_CHANGE<<16|8
;;;
ret 1

;beginmacro

double Opp, Adj, Hyp, AngA, AngB  ;; This is where we get the results
double O, A, H, Aa, Ab, Ac, Tmp   ;; We'll use these for our temporary known values
def PI 3.1415926535897932384626433832795

O = val(e4)
A = val(e5)
H = val(e6)
Aa = val(e7)
int Rd = val(cb14)

if Aa = 0
,if O && A
,,Aa=atan(O/A)
,if A && H
,,Aa=acos(A/H)
,if H && O
,,Aa=asin(O/H)

Hyp = H
Opp = O
Adj = A
AngA = Aa
AngB = Ab

if Opp && Hyp
,AngA = asin(Opp/Hyp)*180/PI ;; use this if the Opp and Hyp are the only known dimensions
if Adj && Hyp
,AngA = acos(Adj/Hyp)*180/PI ;; Use this if the Adj and Hyp are the only known dimensions
if Opp && Adj
,AngA = atan(Opp/Adj)*180/PI ;; Use this if the App and Adj are the only known dimensions

if AngA == 0
,AngA = 90 - AngB
if AngB == 0
,AngB = 90 - AngA
;;;
if (AngB+AngA) == 0
,end

Aa = (AngA*PI)/180
Ab = (AngB*PI)/180

if Opp
,if Hyp == 0
,,A = sin(Aa)
,,Hyp = Opp/A
,if Adj == 0
,,A = tan(Aa)
,,Adj = Opp/A
if Adj
,if Hyp == 0
,,A = cos(Aa)
,,Hyp = Adj/A
,if Opp == 0
,,A = tan(Aa)
,,Opp = A*Adj
if Hyp
,if Adj == 0
,,A = cos(Aa)
,,Adj = A*Hyp
,if Opp == 0
,,A = sin(Aa)
,,Opp = A*Hyp

Opp  = Round(Opp Rd)
Adj  = Round(Adj Rd)
Hyp  = Round(Hyp Rd)
AngA = Round(AngA Rd)
AngB = Round(AngB Rd)

out "--------------------------------------------------"
out F"The Opposite   = {Opp}"
out F"The Adjacent   = {Adj}"
out F"The Hypotenuse = {Hyp}"
out F"The Angle A    = {AngA}"
out F"The Angle B    = {AngB}"

mes (F"The Opposite     : {Opp}[]The Adjacent     : {Adj}[]The Hypotenuse: {Hyp}[]The Angle A       : {AngA}[]The Angle B       : {AngB}")


Forum Jump:


Users browsing this thread: 1 Guest(s)