Jump to content

Triangle Solver!


monoceres
 Share

Recommended Posts

Here's my latest project, it's a small app that calculates sides/angles of a standard triangle.

I did this because I have just started studying trigonometry at school and thought it would be cool to do some real work with the Law of Sines and Law of Cosines.

The code turned out to be a lot harder than I thought and it ended up being a bit messy with loads of ifs, but it works so at least, mission accomplished :D

Any suggestion how to shorten the code would be great, maybe an equation solver?

Ps. If you get -1.#IND it means you have entered a non-real triangle, it's not a bug :D

Triangle_solver.zip

(Previous downloads: 51)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

When I enter:

a=1

b=1

c=2

I get:

A=0

B=0

C=180

Expected:

A=45

B=45

C=90

I checked this in Microsoft Math, and it said c must be lesser than the sum of the other sides.

I have checked a great deal of values against microsoft math and they seem do be correct.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I guess I do get the same result here:

http://www.trig.ionichost.com/trig.php

Also I didn't know I was supposed to click the triangle after entering my values, perhaps this should be indicated somehow?

Didn't a little tooltip arise from the autoit tray icon?

But if you missed it, it's a sign it's not visible enough. Will add some visual info :D

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Didn't a little tooltip arise from the autoit tray icon?

But if you missed it, it's a sign it's not visible enough. Will add some visual info :D

You know what, it popped up but under all of my other windows.

Link to comment
Share on other sites

Oh man I just realized why this is wrong...I was not thinking at all. This would in effect make a straight line. :D

Which make my script right :D

That's why I love math formulas, they never turn out wrong, even when you try to f with them.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

  • 1 month later...

The sides can't be floats?

(ex: 3.2 units)

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

Edit:

something is still wrong

a=1 A=30

b=2 B=60

c=? C=90

Returns

a=1 A=30

b=1.73205080756888 B=60

c=2 C=90

Where it should return

a=1 A=30

b=2 B=60

c=2.23606... C=90

Edit: I mean, it's correct - but it shouldn't be changing fields I inputted manually... because sides (1,2,?) and angles (30,60,90) are possible if c is 2.23606...

As I defined it already as a right-triangle with correct angles and sides, we should be able to use:

Sqrt((1^2)+(2^2))=2.23606797749979

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

  • 3 weeks later...

Sorry if I sounded like I was trashing your script - I really like it, I just have a few things that bug me about it.

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

It's the damn $ES_NUMBER style... I upload one without it. (Who is stupid enough to write text into a triangle solver anyway?).

StringisDigit()

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Thx for the input, really I appreciate it. It just that the code is not very well written so I just don't think I will update it in the near future.

Actually I rather rewrite the whole solving function than begin editing it.

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Thx for the input, really I appreciate it. It just that the code is not very well written so I just don't think I will update it in the near future.

Actually I rather rewrite the whole solving function that begin editing it.

Interesting. This could be useful. You should make it work backwards too. For example, if they fill in the angles of the triangle, it should return the length of the sides. :)

Link to comment
Share on other sites

  • 10 months later...

Right!! I know this is old, but i needed this for h/w, and It looks fun!

Done a bit of work, and made it a bit better with another theorem used - pythagoras' a^2 + b^2 = c^2. Now if you input no angles, but 2 sides, it will presume angle C is 90 and work it out.

I also changed most of the variables and re did the help bit (The tray icon tip was pssing me off).

I like this idea so i'm going to completely re do it soon, so the inputs are actually on the triangle. (need to have a look at GDI though, i haven't done that before)

The updated code:

CODE

; Triangle solver, solves every solveable standard triangle ;---------------------------------------------------------------------

;

; Author: monoceres

; Thanks to: eviltoaster, weaponx and The Kandie Man for help about degrees and radians

;

;---------------------------------------------------------------------------------------------------------------------------------

;

; Triangle laws used:

; * Law of Cosines: a^2=b^2+c^2 -2bcCos(A)

; * Law of Sines: a/Sin(A) = b/Sin(B) = c/Sin©

; * A+B+C = 180

;

; Edit by MDiesel: ;--------------------------------------------------------------------------------------------------------------

;

; New triangle laws:

; * a^2 + b^2 = c^2

;

; Other edits:

; Renamed variables and tried to neaten it up... it needed a bit more consistency - he sometimes uses a space, he sometimes

; doesn't, and it annoys OCD people like me.

;

;---------------------------------------------------------------------------------------------------------------------------------

; Includes, beta safe!

#include <Math.au3>

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <StaticConstants.au3>

#include <EditConstants.au3>

#include <Misc.au3>

Opt ("GUIOnEventMode", 1)

Opt ("MouseCoordMode", 2)

#NoTrayIcon

$hGUI = GUICreate ("Triangle Solver", 501, 339, -1, -1)

GUICtrlCreateLabel ("Left click the triangle to solve it. Right click to reset all fields.", 0, 0, 300, 20) ; Replaced the help tooltip

GUICtrlSetOnEvent (-1, "Help")

$Triangle_Pic = GUICtrlCreatePic (@ScriptDir & "\triangle.gif", 8, 20, 480, 180, BitOR ($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))

GUICtrlSetOnEvent ($Triangle_Pic, "_ProcessTriangle")

$gSides = GUICtrlCreateGroup ("Sides", 8, 200, 237, 113)

GUICtrlCreateLabel ("a", 24, 216, 10, 17)

$sA_Inp = GUICtrlCreateInput ("", 48, 216, 121, 21)

GUICtrlCreateLabel ("b", 24, 248, 10, 17)

$sB_Inp = GUICtrlCreateInput ("", 48, 248, 121, 21)

GUICtrlCreateLabel ("c", 24, 280, 10, 17)

$sc_Inp = GUICtrlCreateInput ("", 48, 280, 121, 21)

$gAngles = GUICtrlCreateGroup ("Angles", 256, 200, 237, 113)

GUICtrlCreateLabel ("A", 272, 216, 11, 17)

$aA_Inp = GUICtrlCreateInput ("", 296, 216, 121, 21)

GUICtrlCreateLabel ("B", 272, 248, 11, 17)

$aB_Inp = GUICtrlCreateInput ("", 296, 248, 121, 21)

GUICtrlCreateLabel ("C", 272, 280, 11, 17)

$aC_Inp = GUICtrlCreateInput ("", 296, 280, 121, 21)

GUISetOnEvent (-3, "close")

GUISetState (@SW_SHOW)

;TrayTip ("Tip", "Left click the triangle to solve it." & @CRLF & "Right click to reset all fields.", 8)

While 1

Sleep(100)

If _IsPressed("02") Then ; Reset fields if mouse button 2 is pressed on triangle.

$mouse = MouseGetPos()

If $mouse[0] >= 8 And $mouse[0] <= 488 And $mouse[1] >= 8 And $mouse[1] <= 188 Then

GUICtrlSetData($sA_Inp, "")

GUICtrlSetData($sB_Inp, "")

GUICtrlSetData($sc_Inp, "")

GUICtrlSetData($aA_Inp, "")

GUICtrlSetData($aB_Inp, "")

GUICtrlSetData($aC_Inp, "")

Sleep(500)

EndIf

EndIf

WEnd

Func _ProcessTriangle()

Local $ifsum, $ifsum2, $bool = False

$aA = GUICtrlRead ($aA_Inp)

$aB = GUICtrlRead ($aB_Inp)

$aC = GUICtrlRead ($aC_Inp)

$sA = GUICtrlRead ($sA_Inp)

$sB = GUICtrlRead ($sB_Inp)

$sC = GUICtrlRead ($sc_Inp)

If StringRight ($aA, 1) = Chr (176) Then $aA = StringTrimRight ($aA, 1)

If StringRight ($aB, 1) = Chr (176) Then $aB = StringTrimRight ($aB, 1)

If StringRight ($aC, 1) = Chr (176) Then $aC = StringTrimRight ($aC, 1)

; Info is gathered to see how many things are known

If $aA <> "" Then $ifsum += 1

If $aB <> "" Then $ifsum += 1

If $aC <> "" Then $ifsum += 1

If $sA <> "" Then $ifsum2 += 1

If $sB <> "" Then $ifsum2 += 1

If $sC <> "" Then $ifsum2 += 1

If ($ifsum < 2 And $ifsum2 < 2) Or $ifsum2 < 1 Then ; 1 angle + 2 sides or 2 angles + 1 side is required to solve triangle

MsgBox(16, "Error", "Not enough information entered to solve this triangle")

Return 0

EndIf

If $ifsum = 2 And $aA + $aC + $aB < 180 Then

If $aA = "" Then

$aA = 180 - $aB - $aC

ElseIf $aB = "" Then

$aB = 180 - $aA - $aC

Else

$aC = 180 - $aA - $aB

EndIf

$ifsum += 1

EndIf

If ($aA + $aB + $aC > 180) Or ($ifsum = 3 And $aA + $aB + $aC < 180) Then ; User tried inputting invalid triangle (Angles not add up to 180)

MsgBox(16, "Triangle error", "Total sum of the angles must be 180°")

Return 0

EndIf

If $sA <> "" And $sB <> "" And $sC <> "" Then ; If all sides are know, lets calculate angles

$aA = Round(_Degree(ACos(($sB ^ 2 + $sC ^ 2 - $sA ^ 2) / (2 * $sC * $sB))), 2) ; Law of Cosines

$aB = Round(_Degree(ACos(($sA ^ 2 + $sC ^ 2 - $sB ^ 2) / (2 * $sC * $sA))), 2) ; Law of Cosines

$aC = 180 - $aB - $aA

ElseIf $ifsum > 1 Then ; If we know more than one angle

If $sA <> "" Then

$sB = ($sA / Sin(_Radian($aA))) * Sin(_Radian($aB)) ; Law of Sines

$sC = ($sA / Sin(_Radian($aA))) * Sin(_Radian($aC)) ; Law of Sines

ElseIf $sB <> "" Then

$sA = ($sB / Sin(_Radian($aB))) * Sin(_Radian($aA)) ; Law of Sines

$sC = ($sB / Sin(_Radian($aB))) * Sin(_Radian($aC)) ; Law of Sines

ElseIf $sC <> "" Then

$sA = ($sC / Sin(_Radian($aC))) * Sin(_Radian($aA)) ; Law of Sines

$sB = ($sC / Sin(_Radian($aC))) * Sin(_Radian($aB)) ; Law of Sines

EndIf

ElseIf $ifsum2 > 1 Then ; We know more than one side

If $sA <> "" And $aA <> "" Then

If $sB <> "" Then

$aB = _Degree(ASin($sB / ($sA / Sin(_Radian($aA))))) ; Law of Sines

$aC = 180 - $aB - $aA

$sC = ($sA / Sin(_Radian($aA))) * Sin(_Radian($aC)) ; Law of Sines

ElseIf $sC <> "" Then

$aB = _Degree(ASin($sC / ($sA / Sin(_Radian($aA))))) ; Law of Sines

$aB = 180 - $aC - $aA

$sB = ($sA / Sin(_Radian($aA))) * Sin(_Radian($aB)) ; Law of Sines

EndIf

ElseIf $sC <> "" And $aC <> "" Then

If $sB <> "" Then

$aB = _Degree(ASin($sB / ($sC / Sin(_Radian($aC))))) ; Law of Sines

$aA = 180 - $aB - $aC

$sA = ($sC / Sin(_Radian($aC))) * Sin(_Radian($aA)) ; Law of Sines

ElseIf $sA <> "" Then

$aA = _Degree(ASin($sA / ($sC / Sin(_Radian($aC))))) ; Law of Sines

$aB = 180 - $aA - $aC

$sB = ($sC / Sin(_Radian($aC))) * Sin(_Radian($aB)) ; Law of Sines

EndIf

ElseIf $sB <> "" And $aB <> "" Then

If $sA <> "" Then

$aA = _Degree(ASin($sA / ($sB / Sin(_Radian($aB))))) ; Law of Sines

$aC = 180 - $aB - $aA

$sC = ($sB / Sin(_Radian($aB))) * Sin(_Radian($aC)) ; Law of Sines

ElseIf $sC <> "" Then

$aC = _Degree(ASin($sC / ($sB / Sin(_Radian($aB))))) ; Law of Sines

$aA = 180 - $aB - $aC

$sA = ($sB / Sin(_Radian($aB))) * Sin(_Radian($aA)) ; Law of Sines

EndIf

Else ; No angle and side match, have to do some extra work

If $aA = "" And $aB = "" And $aC = "" Then ; No angles given!!

$aC = 90 ; presumes its right angled.

If $sA <> "" And $sB <> "" Then ; Find side c

$sC = Sqrt (($sA ^ 2) + ($sB ^ 2))

ElseIf $sA <> "" And $sC <> "" Then ; find b

$sB = Sqrt (($sC ^ 2) - ($sA ^ 2))

ElseIf $sB <> "" And $sC <> "" Then ; find a

$sA = Sqrt (($sC ^ 2) - ($sB ^ 2))

EndIf

EndIf

If $sA = "" Then

$sA = Sqrt (-2 * $sB * $sC * Cos (_Radian($aA)) + $sB ^ 2 + $sC ^ 2) ; Law of Cosines

ElseIf $sB = "" Then

$sB = Sqrt (-2 * $sA * $sC * Cos (_Radian($aB)) + $sA ^ 2 + $sC ^ 2) ; Law of Cosines

ElseIf $sC = "" Then

$sC = Sqrt (-2 * $sA * $sB * Cos (_Radian($aC)) + $sA ^ 2 + $sB ^ 2) ; Law of Cosines

EndIf

$aA = Round (_Degree (ACos (($sB ^ 2 + $sC ^ 2 - $sA ^ 2) / (2 * $sC * $sB))), 2) ; Law of Cosines

$aB = Round (_Degree (ACos (($sA ^ 2 + $sC ^ 2 - $sB ^ 2) / (2 * $sC * $sA))), 2) ; Law of Cosines

$aC = 180 - $aB - $aA

EndIf

EndIf

; Set calculated values!

GUICtrlSetData ($sA_Inp, $sA)

GUICtrlSetData ($sB_Inp, $sB)

GUICtrlSetData ($sc_Inp, $sC)

GUICtrlSetData ($aA_Inp, $aA & Chr (176))

GUICtrlSetData ($aB_Inp, $aB & Chr (176))

GUICtrlSetData ($aC_Inp, $aC & Chr (176))

EndFunc ; ==> _ProcessTriangle

Func Close ()

Exit

EndFunc ; ==> close

Func Help ()

MsgBox (64,"Help for Triangle Solver","Left click the triangle when you have entered desired values to solve it." & @CRLF & _

"Right click it to reset all fields" & @CRLF & "The program will not warn you for everything, remember for example " & _

"that one side cannot be greater than the sum of the others. If you are really struggling then consider searching " & _

"the internet to see what the answer should be. If it is definitely a fault with the script then please report a bug.")

EndFunc ; ==> Help

MDiesel

oh and... @cppman:

and how do you suppose its possible to do that? you need to know at least 1 side to return an exact side. you can only get relatives.

edit:

ah: stoopid smileys. ruining my script! - Thanks res, but what happens if I want to use smileys as well? :P

Edited by mdiesel
Link to comment
Share on other sites

Edit:

something is still wrong

a=1 A=30

b=2 B=60

c=? C=90

Returns

a=1 A=30

b=1.73205080756888 B=60

c=2 C=90

Where it should return

a=1 A=30

b=2 B=60

c=2.23606... C=90

Edit: I mean, it's correct - but it shouldn't be changing fields I inputted manually... because sides (1,2,?) and angles (30,60,90) are possible if c is 2.23606...

As I defined it already as a right-triangle with correct angles and sides, we should be able to use:

Sqrt((1^2)+(2^2))=2.23606797749979

Fixed

; Triangle solver, solves every solveable standard triangle
; Author: monoceres
; Thanks to: eviltoaster, weaponx and The Kandie Man for help about degrees and radians

; Triangle laws I used::
; Law of Cosines: a^2=b^2+c^2 -2bcCos(A)
; Law of Sines: a/Sin(A) = b/Sin(B) = c/Sin©
; A+B+C = 180

; Includes, beta safe!
#include <Math.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2)
$Form1 = GUICreate("Triangle Solver", 501, 339, -1, -1)
$menu=GUICtrlCreateMenu("?")
GUICtrlCreateMenuItem("Help!",$menu)
GUICtrlSetOnEvent(-1,"help")
$Pic1 = GUICtrlCreatePic("triangle.gif", 8, 8, 480, 180, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
GUICtrlSetOnEvent(-1, "_ProcessTriangle")
$Group1 = GUICtrlCreateGroup("Sides", 8, 200, 237, 113)
$Label1 = GUICtrlCreateLabel("a", 24, 216, 10, 17)
$cla = GUICtrlCreateInput("", 48, 216, 121, 21)
$Label2 = GUICtrlCreateLabel("b", 24, 248, 10, 17)
$clb = GUICtrlCreateInput("", 48, 248, 121, 21)
$Label3 = GUICtrlCreateLabel("c", 24, 280, 10, 17)
$clc = GUICtrlCreateInput("", 48, 280, 121, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Angles", 256, 200, 237, 113)
$Label4 = GUICtrlCreateLabel("A", 272, 216, 11, 17)
$Label5 = GUICtrlCreateLabel("B", 272, 248, 11, 17)
$Label6 = GUICtrlCreateLabel("C", 272, 280, 11, 17)
$cl_a = GUICtrlCreateInput("", 296, 216, 121, 21)
$cl_b = GUICtrlCreateInput("", 296, 248, 121, 21)
$cl_c = GUICtrlCreateInput("", 296, 280, 121, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetOnEvent(-3, "close")
GUISetState(@SW_SHOW)
TrayTip("Tip", "Left click the triangle to solve it." & @CRLF & "Right click to reset all fields.", 8)

While 1
    Sleep(100)
    If _IsPressed("02") Then ; Reset fields if mouse button 2 is pressed
        $mouse = MouseGetPos()
        If $mouse[0] >= 8 And $mouse[0] <= 488 And $mouse[1] >= 8 And $mouse[1] <= 188 Then
            GUICtrlSetData($cla, "")
            GUICtrlSetData($clb, "")
            GUICtrlSetData($clc, "")
            GUICtrlSetData($cl_a, "")
            GUICtrlSetData($cl_b, "")
            GUICtrlSetData($cl_c, "")
            Sleep(500)
        EndIf
    EndIf
WEnd

Func _ProcessTriangle()
    Local $ifsum, $ifsum2, $bool = False
    $aangle = GUICtrlRead($cl_a)
    $bangle = GUICtrlRead($cl_b)
    $cangle = GUICtrlRead($cl_c)
    $sidea = GUICtrlRead($cla)
    $sideb = GUICtrlRead($clb)
    $sidec = GUICtrlRead($clc)
    If StringRight($aangle,1)=Chr(176) Then $aangle=StringTrimRight($aangle,1)
    If StringRight($bangle,1)=Chr(176) Then $bangle=StringTrimRight($bangle,1)
    If StringRight($cangle,1)=Chr(176) Then $cangle=StringTrimRight($cangle,1)
    ; Info is gathered to see how many things are known
    If $aangle <> "" Then $ifsum += 1
    If $bangle <> "" Then $ifsum += 1
    If $cangle <> "" Then $ifsum += 1
    If $sidea <> "" Then $ifsum2 += 1
    If $sideb <> "" Then $ifsum2 += 1
    If $sidec <> "" Then $ifsum2 += 1

    If ($ifsum < 2 And $ifsum2 < 2) Or $ifsum2 < 1 Then ; 1 angle + 2 sides or 2 angles + 1 side is required to solve triangle or all sides
        MsgBox(16, "Error", "Not enough information entered to solve this triangle")
        Return
    EndIf
    If $ifsum = 2 And $aangle + $cangle + $bangle < 180 Then
        If $aangle = "" Then
            $aangle = 180 - $bangle - $cangle
        ElseIf $bangle = "" Then
            $bangle = 180 - $aangle - $cangle
        Else
            $cangle = 180 - $aangle - $bangle
        EndIf
        $ifsum += 1
    EndIf
    If ($aangle + $bangle + $cangle > 180) Or ($ifsum = 3 And $aangle + $bangle + $cangle < 180) Then ; User tried inputting invalid triangle (Angles not add up to 180)
        MsgBox(16, "Triangle error", "Total sum of the angles must be 180°")
        Return
    EndIf
    If $sidea <> "" And $sideb <> "" And $sidec <> "" And $ifsum <> 3 Then ; If all sides are know, lets calculate angles
        MsgBox(0, "", "2")
        $aangle = Round(_Degree(ACos(($sideb ^ 2 + $sidec ^ 2 - $sidea ^ 2) / (2 * $sidec * $sideb))), 2) ; Law of Cosines
        $bangle = Round(_Degree(ACos(($sidea ^ 2 + $sidec ^ 2 - $sideb ^ 2) / (2 * $sidec * $sidea))), 2) ; Law of Cosines
        $cangle = 180 - $bangle - $aangle
    ElseIf $ifsum > 1 and $ifsum2 = 1 Then ; If we know more than one angle
        MsgBox(0, "", "1")
        If $sidea <> "" Then
            $sideb = ($sidea / Sin(_Radian($aangle))) * Sin(_Radian($bangle)) ; Law of Sines
            $sidec = ($sidea / Sin(_Radian($aangle))) * Sin(_Radian($cangle)) ; Law of Sines
        ElseIf $sideb <> "" Then
            $sidea = ($sideb / Sin(_Radian($bangle))) * Sin(_Radian($aangle)) ; Law of Sines
            $sidec = ($sideb / Sin(_Radian($bangle))) * Sin(_Radian($cangle)) ; Law of Sines
        ElseIf $sidec <> "" Then
            $sidea = ($sidec / Sin(_Radian($cangle))) * Sin(_Radian($aangle)) ; Law of Sines
            $sideb = ($sidec / Sin(_Radian($cangle))) * Sin(_Radian($bangle)) ; Law of Sines
        EndIf
    ElseIf $ifsum2 = 2 and $ifsum <> 3 Then ; We know more than one side
        If $sidea <> "" And $aangle <> "" Then
            If $sideb <> "" Then
                $bangle = _Degree(ASin($sideb / ($sidea / Sin(_Radian($aangle))))) ; Law of Sines
                $cangle = 180 - $bangle - $aangle
                $sidec = ($sidea / Sin(_Radian($aangle))) * Sin(_Radian($cangle)) ; Law of Sines
            ElseIf $sidec <> "" Then
                $bangle = _Degree(ASin($sidec / ($sidea / Sin(_Radian($aangle))))) ; Law of Sines
                $bangle = 180 - $cangle - $aangle
                $sideb = ($sidea / Sin(_Radian($aangle))) * Sin(_Radian($bangle)) ; Law of Sines
            EndIf
        ElseIf $sidec <> "" And $cangle <> "" Then
            If $sideb <> "" Then
                $bangle = _Degree(ASin($sideb / ($sidec / Sin(_Radian($cangle))))) ; Law of Sines
                $aangle = 180 - $bangle - $cangle
                $sidea = ($sidec / Sin(_Radian($cangle))) * Sin(_Radian($aangle)) ; Law of Sines
                
            ElseIf $sidea <> "" Then
                $aangle = _Degree(ASin($sidea / ($sidec / Sin(_Radian($cangle))))) ; Law of Sines
                $bangle = 180 - $aangle - $cangle
                $sideb = ($sidec / Sin(_Radian($cangle))) * Sin(_Radian($bangle)) ; Law of Sines
                
            EndIf
        ElseIf $sideb <> "" And $bangle <> "" Then
            If $sidea <> "" Then
                $aangle = _Degree(ASin($sidea / ($sideb / Sin(_Radian($bangle))))) ; Law of Sines
                $cangle = 180 - $bangle - $aangle
                $sidec = ($sideb / Sin(_Radian($bangle))) * Sin(_Radian($cangle)) ; Law of Sines
                
            ElseIf $sidec <> "" Then
                $cangle = _Degree(ASin($sidec / ($sideb / Sin(_Radian($bangle))))) ; Law of Sines
                $aangle = 180 - $bangle - $cangle
                $sidea = ($sideb / Sin(_Radian($bangle))) * Sin(_Radian($aangle)) ; Law of Sines
            EndIf
        Else ; No angle and side match, have to do some extra work
            If $sidea = "" Then
                $sidea = Sqrt(-2 * $sideb * $sidec * Cos(_Radian($aangle)) + $sideb ^ 2 + $sidec ^ 2) ; Law of Cosines
            ElseIf $sideb = "" Then
                $sideb = Sqrt(-2 * $sidea * $sidec * Cos(_Radian($bangle)) + $sidea ^ 2 + $sidec ^ 2) ; Law of Cosines
            ElseIf $sidec = "" Then
                $sidec = Sqrt(-2 * $sidea * $sideb * Cos(_Radian($cangle)) + $sidea ^ 2 + $sideb ^ 2) ; Law of Cosines
            EndIf
            $aangle = Round(_Degree(ACos(($sideb ^ 2 + $sidec ^ 2 - $sidea ^ 2) / (2 * $sidec * $sideb))), 2) ; Law of Cosines
            $bangle = Round(_Degree(ACos(($sidea ^ 2 + $sidec ^ 2 - $sideb ^ 2) / (2 * $sidec * $sidea))), 2) ; Law of Cosines
            $cangle = 180 - $bangle - $aangle
        EndIf
    Else ;triangle is over defined
        If $sidea = "" Then $sidea = Sqrt(-2 * $sideb * $sidec * Cos(_Radian($aangle)) + $sideb ^ 2 + $sidec ^ 2) ; Law of Cosines
        If $sideb = "" Then $sideb = Sqrt(-2 * $sidea * $sidec * Cos(_Radian($bangle)) + $sidea ^ 2 + $sidec ^ 2) ; Law of Cosines
        If $sidec = "" Then $sidec = Sqrt(-2 * $sidea * $sideb * Cos(_Radian($cangle)) + $sidea ^ 2 + $sideb ^ 2) ; Law of Cosines
        If $aangle = "" AND $bangle <> "" AND $cangle <> "" Then $aangle = 180-$bangle-$cangle
        If $aangle <> "" AND $bangle = "" AND $cangle <> "" Then $bangle = 180-$aangle-$cangle
        If $aangle <> "" AND $bangle <> "" AND $cangle = "" Then $cangle = 180-$aangle-$bangle
    EndIf
    ; Set calculated values!
    GUICtrlSetData($cla, $sidea)
    GUICtrlSetData($clb, $sideb)
    GUICtrlSetData($clc, $sidec)
    GUICtrlSetData($cl_a, $aangle&Chr(176))
    GUICtrlSetData($cl_b, $bangle&Chr(176))
    GUICtrlSetData($cl_c, $cangle&Chr(176))
    
EndFunc   ;==>_ProcessTriangle


Func close()
    Exit
EndFunc   ;==>close

Func help()
    MsgBox(64,"Help for Triangle Solver","Left click the triangle when you have entered desired values to solve it."&@CRLF&"Right click it to reset all fields"&@CRLF&"The program will not warn you for everything, remember for example that one side cannot be greater than the sum of the others")
EndFunc

I don't have the code that assumes 90 deg in here and there seems to be some rounding error problems but this should work with any solvable triangle.

Edited by smstroble

MUHAHAHAHAHA

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...