.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help needed with Triangle solution calculator in VB.NET

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
SteveHeather7776
4130 Views, 15 Replies

Help needed with Triangle solution calculator in VB.NET

Hi Guys,

 

I am a beta tester for AutoCAD, but I have a problem I would like some help with. I am creating a calculator that solves the right angled triangle for my CAD students. I have programmed it in VB.NET, all you have to enter are two known quantities to get the other results. It all works ok apart from entering 'B+a' and 'C+a', with these two I am getting weird results. (see attached image)

 I am using the 3,4,5 triangle to test the software. Any help would be much appreciated.

 

Regards,

 

Steve.

 

Here is the code I have for the application:

 

Imports System.String
Imports System.Math
Public Class Form1
Dim angleA As Single = 90
Dim angleB As Single
Dim angleC As Single
Dim sidea As Single
Dim sideb As Single
Dim sidec As Single

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
Controls.Add(NumericUpDown1)

End Sub

Public Function isItNullString(ByVal value) As Boolean
If value = "" Then
Return True
Else
Return False
End If
End Function

Public Sub pythagoreanTheoremThings()

If sideb = 0 Then
sideb = Math.Sqrt((sidea * sidea) - (sidec * sidec))
TextBoxSideb.Text = sideb

ElseIf sidea = 0 Then
sidea = Math.Sqrt((sideb * sideb) + (sidec * sidec))
TextBoxSidea.Text = sidea

ElseIf sidec = 0 Then
sidec = Math.Sqrt((sidea * sidea) - (sideb * sideb))
TextBoxSidec.Text = sidec
End If

End Sub

Public Sub angleSums()

If angleC = 0 And angleB <> 0 Then
angleC = 180 - angleA - angleB
TextBoxAngleC.Text = angleC
End If

If angleB = 0 And angleC <> 0 Then
angleB = 180 - angleA - angleC
TextBoxAngleB.Text = angleB
End If
End Sub

Public Sub angleBasWorkingAngle()

'Angle B and side b known, find side a
If angleB <> 0 Then
sidea = sideb / Sin(degreesToRadians(angleB))
TextBoxSidea.Text = sidea
End If

'Angle B and side c known, find side b
If angleB <> 0 And sidec <> 0 Then
sideb = sidec * Tan(degreesToRadians(angleB))
TextBoxSideb.Text = sideb
End If

End Sub

Public Sub findangleC()

If angleC = 0 Then
'cos
angleC = radiansToDegrees(Acos(sideb / sidea))
TextBoxAngleC.Text = angleC
End If

End Sub

Public Sub feedValues()

If Not isItNullString(TextBoxSideb.Text) Then
sideb = TextBoxSideb.Text
Else
sideb = 0
End If

If Not isItNullString(TextBoxSidea.Text) Then
sidea = TextBoxSidea.Text
Else
sidea = 0
End If

If Not isItNullString(TextBoxSidec.Text) Then
sidec = TextBoxSidec.Text
Else
sidec = 0
End If

If Not isItNullString(TextBoxAngleC.Text) Then
angleC = TextBoxAngleC.Text
Else
angleC = 0
End If

If Not isItNullString(TextBoxAngleB.Text) Then
angleB = TextBoxAngleB.Text
Else
angleB = 0
End If
End Sub

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 / Math.PI)
Return degrees
End Function


#Region " Calculate all Data "
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
For aaa As Integer = 0 To 2 'I just repeated this 6 times because I have a lot of processing power
'you can do it twice.
feedValues()

pythagoreanTheoremThings()

angleSums()

angleBasWorkingAngle()

findangleC()


Next

End Sub
#End Region


#Region " Clear all input boxes "
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
ClearTextBox(ctrl)
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
#End Region

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClearTextBox(Me)
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

 

End Class

 

15 REPLIES 15
Message 2 of 16
adadnet
in reply to: SteveHeather7776

hi steve

 

how do you ensure conditions a > b and a > c when user inputs?

 

felix

Message 3 of 16
SteveHeather7776
in reply to: adadnet

Well I should have said from the start, I am new to VB.NET, sorry. I am a CAD peron through and through, but when it comes to this I am not so good.

 

So to your answer, I am not sure, sorry.

 

I picked most of the code up from different examples, and sort of figured other bits out myself, I am one of those who learns by trial and error, or seeing examples and modifying from there. So I am relying on you guys to steer me in the right direction.

 

Thanks.

 

Steve

Message 4 of 16
adadnet
in reply to: SteveHeather7776

no worries, i refer to the bit where you take the square root of the 2 variables the user chooses to put in, to calculate the missing 3rd in the triangle: as long as side a is larger than side b and also larger than side c it should work, however, should side a not be the largest side, the code could result in a square root of a negative number. to avoid this risk, restrictions are needed on the respective relative size of a, b and c to enforce that a > b and a > c. (this would explain why you get a 'correct' result for b and c,  as the [assumed] smaller sides, they are being added for the square root.)

 

something else to look out for is that the code doesn't know whether the triangle is in fact a rectengular one, as needed for pythargoras to hold true, i.e. taking the sqare root of the difference in sqares, it simple 'makes' it a rectangular one. (whether that's relevant here, i don't know.)

Message 5 of 16
SteveHeather7776
in reply to: adadnet

Thanks Felix I do understand that, so have you any suggestions how I would code this to correct your suggestions?

 

Steve.

Message 6 of 16
adadnet
in reply to: SteveHeather7776

more specifically, something like:

 

public sub pythagorean theorem()

 

if sideb = 0 then

   if sidea > sidec then sqrt(squarea - sqaurec) ' continue as expected

   else ' swap sides before continue

      storesidea = sidea     

      sidea=sidec

      sidec=storesidea

      sqrt(squarea - sqaurec)

   endif

endif

... analogously for sidea > sideb

 

alternatively, i believe you could mask the input values with the form which sends the values in the 1st place, but that's not really much to do with acad anymore. hope this is closer to what you're looking for

Message 7 of 16
SteveHeather7776
in reply to: adadnet

Hi Felix,

 

I tried your suggestion but it doesn't like it, I am using VB Studio 2010. 

Message 8 of 16

Hi,

 

The first is the first, before to solve a Pythagoras, you should check if the triplet (a,b,c) form a triangle, this is tested using the "Triangle Inequality" : the sum of any pair of sides must be greater than the other side (if equal it's a degenerated triangle, a line in this case):

 

Function IsTriangle(a as double,b as double, c as double) as Boolean

 If (a+b > c) andAlso (a+c > b) andAlso (c+b > a) then

  return True

 Else

  return False

End function

 

Now fix the position of the sides and be coherent with that, use a,b for the sides (or legs) and c for the hypotenuse, and check for that in the imput handler (just check if c > a And c > b) if not, then alert. And now evaluate Pythagoras  to check if the input values conform a stright triangle.

 

Gaston Nunez

 

Message 9 of 16
adadnet
in reply to: SteveHeather7776

to round of my previous comments - and i apologise if this is too basic:

 

in vs, place a breakpoint at the beginning of your code and use F8 to 'step' through each line as you debug. move the cursor over variables in the line that you are on or have just passed to check its value. this should give you an indication of exactly where values are as expected and where they are not what they should be, and eventually where exceptions are thrown and the conclusion why. (i don't know if this applies to the express version of vs.)

 

with regards to autocad and .net, this is the prefered point of 1st contact, if youl like:

 

http://docs.autodesk.com/ACD/2011/ENU/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-4875.htm

 

 

Message 10 of 16

Thanks for all your help guys, but I do not have a clue what it is you are talking about. Sorry.

 

What I have so far:

 

If i use the 3,4,5 as an example, and put in these known factors, this is what I get:

 

I enter side c as 4, side b as 3, press calculate and it gives me; c=4, b=3, a=5, angle B = 36.87, angle C = 53.13. So for a 3,4,5 triangle that is correct.

 

If I enter angle B as 36.87, and side c as 4, press calculate and it gives me all the correct answers. I can do this with all the combinations of two inputs apart from;

 

angle B and side a

angle C and side a

 

Both come out as if you were dividing the sides by the Sine.

 

I hope that is not too confusing, because I think I have just confused myself.

 

Steve.

Message 11 of 16

Hi,

 

I insist that you are confused your self because the wrong positioned a,b,c values over the triangle in your screen grab. If  you have 3,4,5, then the value 5 MUST be over the hypotenuse, and NOT over a leg side. In other words where in the picture says "a" should be "c", and so with "c", given that  yo are solving the Pythagoras  in the standar form a*a+b*b=c*c. Other way just change the order of a,b,c in the formula.

 

Gaston Nunez

 

 

 

 

Message 12 of 16

There is nothing wrong with the pythagoras maths at all, it doesn't matter what you call the sides, they are just labels. It wouldn't matter if you called them side dog or side cat, it is the way in which you use them. If you look at the image I have attached you will see that it works ok for the pythagoras side of it.

Message 13 of 16
jeff
in reply to: SteveHeather7776

I would redo the code to make easier, but looking real quick think about this section of code.

 

 If sideb = 0 Then
            sideb = Math.Sqrt((sidea * sidea) - (sidec * sidec))
            TextBoxSideb.Text = sideb

        ElseIf sidea = 0 Then
            sidea = Math.Sqrt((sideb * sideb) + (sidec * sidec))
            TextBoxSidea.Text = sidea

        ElseIf sidec = 0 Then
            sidec = Math.Sqrt((sidea * sidea) - (sideb * sideb))
            TextBoxSidec.Text = sidec
        End If

You are assuming that 2 sides will always have a value.

The sides really do not equal 0 you are using it as placeholder, might be better to take a different approach

You can also find your answers @ TheSwamp
Message 14 of 16
SteveHeather7776
in reply to: jeff

Thanks for your reply, I think it is very obvious that I don't know very much about VB programming, but I never proffessed too either. So I am asking for help on the solution please, in other words, what is the best way to code this calculator. As I mentioned earlier, I learn things by trial and error, and by seeing correct examples that I can then manipulate into my programmes.

 

Many thanks,

 

Steve

Message 15 of 16

Hi guys,

 

I have changed all the lettering around as per your instructions, and added some extra code, and it works properly.

 

Thanks for all your help.

 

Steve.

 

New code below, and image attached:

 

Imports System.String
Imports System.Math
Public Class Form1
Dim angleA As Single = 90
Dim angleB As Single
Dim angleC As Single
Dim sidea As Single
Dim sideb As Single
Dim sidec As Single

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
Controls.Add(NumericUpDown1)

End Sub

Public Function isItNullString(ByVal value) As Boolean
If value = "" Then
Return True
Else
Return False
End If
End Function

Public Sub pythagoreanTheoremThings()

If sidec <> 0 And sidea <> 0 And sideb = 0 Then
sideb = Math.Sqrt((sidec * sidec) - (sidea * sidea))
TextBoxSideb.Text = sideb

ElseIf sideb <> 0 And sidea <> 0 And sidec = 0 Then
sidec = Math.Sqrt((sideb * sideb) + (sidea * sidea))
TextBoxSidec.Text = sidec

ElseIf sidec <> 0 And sideb <> 0 And sidea = 0 Then
sidea = Math.Sqrt((sidec * sidec) - (sideb * sideb))
TextBoxSidea.Text = sidea
End If
End Sub

Public Sub angleSums()

If angleC = 0 And angleB <> 0 Then
angleC = 180 - angleA - angleB
TextBoxAngleC.Text = angleC
End If

If angleB = 0 And angleC <> 0 Then
angleB = 180 - angleA - angleC
TextBoxAngleB.Text = angleB
End If
End Sub

Public Sub angleBasWorkingAngle()

'Angle B and side b known, find side c
If angleB <> 0 And sideb <> 0 Then
sidec = sideb / Sin(degreesToRadians(angleB))
TextBoxSidec.Text = sidec
End If

'Angle B and side a known, find side b
If angleB <> 0 And sidea <> 0 Then
sideb = sidea * Tan(degreesToRadians(angleB))
TextBoxSideb.Text = sideb
End If

'Angle B and side b known, find side a
If angleB <> 0 And sideb <> 0 Then
sidea = sideb / Tan(degreesToRadians(angleB))
TextBoxSidea.Text = sidea
End If

'Angle B and side b known, find side a
If angleB <> 0 And sidec <> 0 Then
sidea = sidec * Cos(degreesToRadians(angleB))
TextBoxSidea.Text = sidea
End If

End Sub

Public Sub findangleC()

If angleC = 0 Then
'cos
angleC = radiansToDegrees(Acos(sideb / sidec))
TextBoxAngleC.Text = angleC
End If

End Sub


Public Sub feedValues()

If Not isItNullString(TextBoxSideb.Text) Then
sideb = TextBoxSideb.Text
Else
sideb = 0
End If

If Not isItNullString(TextBoxSidea.Text) Then
sidea = TextBoxSidea.Text
Else
sidea = 0
End If

If Not isItNullString(TextBoxSidec.Text) Then
sidec = TextBoxSidec.Text
Else
sidec = 0
End If

If Not isItNullString(TextBoxAngleC.Text) Then
angleC = TextBoxAngleC.Text
Else
angleC = 0
End If

If Not isItNullString(TextBoxAngleB.Text) Then
angleB = TextBoxAngleB.Text
Else
angleB = 0
End If
End Sub

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 / Math.PI)
Return degrees
End Function


#Region " Calculate all Data "
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
For aaa As Integer = 0 To 2 'I just repeated this 6 times because I have a lot of processing power
'you can do it twice.
feedValues()

pythagoreanTheoremThings()

angleSums()

angleBasWorkingAngle()

findangleC()


Next

End Sub
#End Region


#Region " Clear all input boxes "
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
ClearTextBox(ctrl)
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
#End Region

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClearTextBox(Me)
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Message 16 of 16

..how to make triangle solver?where can i create like that one?i really need it because it is our project in trigonometry.thanks for the immediate respond=)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost