Page 1 of 1

## Triangle Dimensions/Angles Calculator Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=145014&amp;s=45dd831ae9519bf479a7acb071fcfc13&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 LeisureProgrammer

• N00by VB.NET Developer

Reputation: 110
• Posts: 588
• Joined: 06-September 09

Posted 11 December 2009 - 08:18 PM

This tutorial will show you how to build a nice "triangle calculator" that will calculate all the sides and angles in a right triangle - provided that you give it atleast two angles and a side length, or an angle and two side lengths. It does this using sin, cos, tan, the Pythagorean theorem, and the fact that all angles in any triangle add up to 180 degrees.

This will only work for right angle triangles.

This is what my suggested UI looks like...

You will have to be very careful about getting the names of the variables right because a slight referral to the wrong variable can be disastrous. I suggest that you set your textboxes up this way - so they match up with the code I gave.

Quote

textbox1, angle2
textbox2, angle3
textbox3, side2
textbox5, side1
textbox4, side 3

I'm going to explain my program "bottom-up" because it's easier that way.

Here are the main variables you'll need:
```Dim angle1 As Double = 90
Dim angle2 As Double
Dim angle3 As Double
Dim side1 As Double
Dim side2 As Double
Dim side3 As Double

```

This is a function I made to check if there was nothing in the textbox. WARNING: Do not use this method for school assignments, I was being really lazy and didn't think about this - I'm sure there is a MUCH more efficient way of doing this.
```Public Function isItNullString(ByVal value) As Boolean
If value = "" Then
Return True
Else
Return False
End If
End Function

```

This sub feeds the values to the right vars, again, highly inefficient.
``` Public Sub feedValues()
If Not isItNullString(TextBox5.Text) Then
side1 = TextBox5.Text
Else
side1 = 0
End If

If Not isItNullString(TextBox3.Text) Then
side2 = TextBox3.Text
Else
side2 = 0
End If

If Not isItNullString(TextBox4.Text) Then
side3 = TextBox4.Text
Else
side3 = 0
End If

If Not isItNullString(TextBox1.Text) Then
angle2 = TextBox1.Text
Else
angle2 = 0
End If

If Not isItNullString(TextBox2.Text) Then
angle3 = TextBox2.Text
Else
angle3 = 0
End If
End Sub

```

You'll need these two functions to convert back and forth from degrees and radians, because the built in things we're going to use only take in and output results in radians.
```Public Function degreesToRadians(ByVal degrees As Double) As Double
Dim radians As Double
radians = (Math.PI * degrees) / 180
Return radians
End Function

Public Function radiansToDegrees(ByVal radians As Double) As Double
Dim degrees As Double
degrees = radians * (180 / PI)
Return degrees
End Function

```

Sometimes, a "NaN" appears in the textbox after certain calculations, that I'm guessing that either means 0 or an error, so we're going to use this sub to get rid of them.

``` Public Sub noMoreNaNs()
For Each blah As Control In Me.Controls
If blah.Text = "NaN" Then blah.Text = 0
Next
End Sub

```

This sub finds the remaining side length if two already exist, using the Pythagorean theorem. ((a^2) + (b^2) = (c ^ 2)) Oh, and import the System.Math namespace now
```
Public Sub pythagoreanTheoremThings()

If side1 = 0 Then
side1 = Math.Sqrt((side2 ^ 2) - (side3 ^ 2))
TextBox5.Text = side1

ElseIf side2 = 0 Then
side2 = Math.Sqrt((side1 ^ 2) + (side3 ^ 2))
TextBox3.Text = side2

ElseIf side3 = 0 Then
side3 = Math.Sqrt((side2 ^ 2) - (side1 ^ 2))
TextBox4.Text = side3
End If

End Sub

```

This code figures out missing angles if two of them already exsist... It does this by using the fact that all of the angles in any triangle add up to 180.
```Public Sub angleSums()
If angle2 = 0 And angle3 <> 0 Then
angle2 = 180 - angle1 - angle3
TextBox1.Text = angle2
End If

If angle3 = 0 And angle2 <> 0 Then
angle3 = 180 - angle1 - angle2
TextBox2.Text = angle3
End If
End Sub

```

Now that the easy part is done, here comes the beast. This will find any missing side lengths providing that we know either angle 3 or 2, and we know atleast one side length.

``` Public Sub angle3asWorkingAngle()
'Cosine
If angle3 <> 0 And side2 = 0 Then
side2 = side3 / Cos(degreesToRadians(angle3))
TextBox3.Text = side2
End If

If angle3 <> 0 And side3 = 0 Then
side3 = side2 * Cos(degreesToRadians(angle3))
TextBox4.Text = side3
End If
'Sine
If angle3 <> 0 And side1 = 0 Then
side1 = side2 / Sin(degreesToRadians(angle3))
TextBox5.Text = side1
End If

If angle3 <> 0 And side2 = 0 Then
side2 = side1 * (Sin(degreesToRadians(angle3)))
TextBox3.Text = side2
End If
'Tangence
If angle3 <> 0 And side1 = 0 Then
side1 = side3 / Tan(degreesToRadians(angle3))
TextBox5.Text = side1
End If

If angle3 <> 0 And side3 = 0 Then
side3 = side1 * (Tan(degreesToRadians(angle3)))
TextBox4.Text = side3
End If

End Sub

Public Sub angle2asWorkingAngle()

'Cosine
If angle2 <> 0 And side2 = 0 Then
side2 = side1 / Cos(degreesToRadians(angle2))
TextBox3.Text = side2
End If

If angle2 <> 0 And side1 = 0 Then
side1 = side2 / Cos(degreesToRadians(angle2))
TextBox5.Text = side1
End If
'Sine
If angle2 <> 0 And side2 = 0 Then
side2 = side3 / Sin(degreesToRadians(angle2))
TextBox3.Text = side2
End If

If angle2 <> 0 And side3 = 0 Then
side3 = side2 / Sin(degreesToRadians(angle2))
TextBox4.Text = side3
End If
'Tangence
If angle2 <> 0 And side3 = 0 Then
side3 = side1 / Tan(degreesToRadians(angle2))
TextBox4.Text = side3
End If

If angle2 <> 0 And side1 = 0 Then
side1 = side3 / Tan(degreesToRadians(angle2))
TextBox5.Text = side1
End If

End Sub

```

If we know two of the sidelengths, my code below will find angles two and three:

```Public Sub findAngle3()
If angle3 = 0 And side3 <> 0 And side2 <> 0 Then
'Cos
angle3 = radiansToDegrees(Acos(side3 / side2))
TextBox2.Text = angle3
End If

If angle3 = 0 And side1 <> 0 And side2 <> 0 Then
'Sin
angle3 = radiansToDegrees(Asin(side1 / side2))
TextBox2.Text = angle3
End If

If angle3 = 0 And side3 <> 0 And side1 <> 0 Then
'Tan
angle3 = radiansToDegrees(Atan(side1 / side3))
TextBox2.Text = angle3
End If
End Sub

Public Sub findAngle2()

If angle2 = 0 And side1 <> 0 And side2 <> 0 Then
'cos
angle2 = radiansToDegrees(Acos(side1 / side2))
TextBox1.Text = angle2
End If

If angle2 = 0 And side1 <> 0 And side3 <> 0 Then
'tan
angle2 = radiansToDegrees(Atan(side3 / side1))
TextBox1.Text = angle2
End If

If angle2 = 0 And side2 <> 0 And side3 <> 0 Then
'sin
angle2 = radiansToDegrees(Asin(side3 / side2))
TextBox1.Text = angle2
End If

End Sub

```

And finally, the code for the calculate button:

``` Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For aaa As Integer = 0 To 5 'I just repeated this 6 times because I have a lot of processing power... you can do it twice.
feedValues()

pythagoreanTheoremThings()
noMoreNaNs()

angleSums()
noMoreNaNs()

angle3asWorkingAngle()
noMoreNaNs()

angle2asWorkingAngle()
noMoreNaNs()

findAngle3()
noMoreNaNs()

findAngle2()
noMoreNaNs()

Next

End Sub

```

Enjoy!
Feel free to look at the attached project to get an idea of how it is set-up. I'm sure there are some minor mistakes in here . I just learnt about sin, cos, and tan yesterday in my math class - so I made this program to help me understand it better, and to help me complete my math homework. I'm sure I'll find some glitches in here when I actually use it for 50+ questions

#### Attached File(s)

Is This A Good Question/Topic? 0

## Replies To: Triangle Dimensions/Angles Calculator

### #2 Vermiculus

• D.I.C Regular

Reputation: 10
• Posts: 314
• Joined: 26-February 09

Posted 25 March 2010 - 07:52 PM

Glad to see I'm not the only one who writes full-fledged programs for the sole purpose of a non-CS assignment

edit: I guess its a good thing we're not working with non-Euclidean geometry here

This post has been edited by Vermiculus: 25 March 2010 - 07:54 PM

Was This Post Helpful? 0

### #3 Steveheather

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 28-January 12

Posted 28 January 2012 - 09:12 AM

I have been looking for the code to this solution for ages, is there a way adding or changing the code so that if I just put in one angle and one side, it will give me all the correct answers? At the moment, if I enter angle3 and side1, it gives the wrong solution. I am a beginner at VB but I would appreciate it if someone can come up with the solution.

Many thanks,

Steve
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }