.NET

.NET

Active Member
Posts: 8
Registered: ‎03-19-2009
Message 1 of 16 (1,086 Views)

Help needed with Triangle solution calculator in VB.NET

1086 Views, 15 Replies
02-10-2012 05:52 AM

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

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
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

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

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
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

Valued Contributor
Posts: 95
Registered: ‎11-15-2011
Message 2 of 16 (1,081 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 06:12 AM in reply to: SteveHeather7776

hi steve

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

felix

Active Member
Posts: 8
Registered: ‎03-19-2009
Message 3 of 16 (1,079 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 06:17 AM in reply to: FFlix

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

Valued Contributor
Posts: 95
Registered: ‎11-15-2011
Message 4 of 16 (1,072 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 06:41 AM 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.)

Active Member
Posts: 8
Registered: ‎03-19-2009
Message 5 of 16 (1,065 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 06:52 AM in reply to: FFlix

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

Steve.

Valued Contributor
Posts: 95
Registered: ‎11-15-2011
Message 6 of 16 (1,051 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 08:44 AM 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

Active Member
Posts: 8
Registered: ‎03-19-2009
Message 7 of 16 (1,049 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 09:23 AM in reply to: FFlix

Hi Felix,

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

Distinguished Mentor
Posts: 582
Registered: ‎04-11-2010
Message 8 of 16 (1,043 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 10:41 AM in reply to: SteveHeather7776

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

Valued Contributor
Posts: 95
Registered: ‎11-15-2011
Message 9 of 16 (1,040 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 11:06 AM 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

Active Member
Posts: 8
Registered: ‎03-19-2009
Message 10 of 16 (1,039 Views)

Re: Help needed with Triangle solution calculator in VB.NET

02-10-2012 11:10 AM in reply to: gasty1001

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.

Post to the Community

Have questions about Autodesk products? Ask the community.