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
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
Attached File(s)
-
WindowsApplication1.zip (350K)
Number of downloads: 730





MultiQuote




|