Technical Help & Discussion > Website Design & Programming

Math.Cos

<< < (3/3)

Dack:
Right idea, using the System.Math.Atan function, but did you remember to convert the returned angle from radians to degrees again?

i.e. multiply by 180 and divide by PI?

Hiatus:
I have no clue, lol....I added in the * 180 / PI, but it doesn't seem to work. Maybe its just me.

This is what I have as my code:

Public Class Form1
    Inherits System.Windows.Forms.Form

' General Declarations
Dim Alpha, A, B, C, R, Tan, Theta, AngleA, AngleB, AngleC, XCompA, XCompB, XCompC, YCompA, YCompB, YCompC, XCompTotal, YCompTotal As Double

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Clear the Listbox
        lstOutput.Items.Clear()

        ' Declarations

        Dim fmtStr As String = &quot;{0, 0} {1, 14} {2, 15}&quot;
        ' Declare PI
        Const PI = 3.14159265358979

        ' Add text to Variables
        A = CDbl(Val(txtA.Text))
        B = CDbl(Val(txtB.Text))
        C = CDbl(Val(txtC.Text))
        AngleA = CDbl(Val(txtAngleA.Text))
        AngleB = CDbl(Val(txtAngleB.Text))
        AngleC = CDbl(Val(txtAngleC.Text))

        ' Add titles to Chart/Listbox
        lstOutput.Items.Add(String.Format(&quot;{0, 0} {1, 15} {2, 15}&quot;, &quot;&quot;, &quot;X Comp.&quot;, &quot;Y Comp.&quot;))

        ' Find Cosine of Angle A...multiply by A
        XCompA = FormatNumber(Math.Cos((AngleA / 180.0) * PI), 2) * A
        ' Find Sine of Angle A...multiply by A
        YCompA = FormatNumber(Math.Sin((AngleA / 180.0) * PI), 2) * A

        ' Find Cosine of Angle B...multiply by B
        XCompB = FormatNumber(Math.Cos((AngleB / 180.0) * PI), 2) * B
        ' Find Sine of Angle B...multiply by B
        YCompB = FormatNumber(Math.Sin((AngleB / 180.0) * PI), 2) * B

        ' Find Cosine of Angle C...multiply by C
        XCompC = FormatNumber(Math.Cos((AngleC / 180.0) * PI), 2) * C
        ' Find Sine of Angle C...multiply by C
        YCompC = FormatNumber(Math.Sin((AngleC / 180.0) * PI), 2) * C

        ' Add all XComp and YComp's together into more variables
        XCompTotal = FormatNumber(XCompA + XCompB + XCompC, 2)
        YCompTotal = FormatNumber(YCompA + YCompB + YCompC, 2)

        ' Find &quot;R&quot;
        R = FormatNumber(Math.Sqrt((XCompTotal ^ 2) + (YCompTotal ^ 2)), 2)

        ' Find &quot;Tan&quot;
        Tan = FormatNumber((YCompTotal / XCompTotal), 2)

        ' Find &quot;Theta&quot;
        Theta = FormatNumber(Math.Tan((Tan * 180.0) / PI), 2)

        ' Find Alpha
        Alpha = FormatNumber(Math.Atan((Theta * 180.0) / PI), 2)

        ' Add each answer to the Listbox
        With lstOutput.Items

            .Add(String.Format(fmtStr, &quot;&quot;, XCompA, YCompA))
            .Add(String.Format(fmtStr, &quot;&quot;, XCompB, YCompB))
            .Add(String.Format(fmtStr, &quot;&quot;, XCompC, YCompC))
            .Add(String.Format(&quot;{0, 5} {1, 8} {2, 15}&quot;, &quot;Total:&quot;, XCompTotal, YCompTotal))
            .Add(String.Format(&quot;&quot;))
            .Add(String.Format(&quot;{0, 5} {1, 0} {2, 11}&quot;, &quot;R =&quot;, &quot;&quot;, R))
            .Add(String.Format(&quot;{0, 0} {1, 0} {2, 1}&quot;, &quot;Tan Alpha =&quot;, &quot;&quot;, Tan))
            .Add(String.Format(&quot;{0, 5} {1, 0} {2, 8}&quot;, &quot;Alpha =&quot;, &quot;&quot;, Alpha))
            .Add(String.Format(&quot;{0, 5} {1, 2} {2, 4}&quot;, &quot;Theta R =&quot;, &quot;&quot;, Theta))
            .Add(String.Format(&quot;&quot;))
            .Add(String.Format(&quot;{0, 5} {1, 0} {2, 2}&quot;, &quot;R =&quot;, R &amp; &quot;N&quot;, Alpha &amp; &quot; Degrees&quot;))

        End With
    End Sub
End Class

Hiatus:
Ah, excellent...I got it sorted out!  ;D

The proper format was:

Alpha = FormatNumber((Math.Atan(Tan) * 180.0 / PI), 2)

I messed up on a couple things, but its all good now. Thanx for ur help Dack!

Dack:
I did wonder why you were taking the tan of a tangent :)

BTW you should use math.PI instead of defining PI - it was just a quick way of doing it.

Navigation

[0] Message Index

[*] Previous page

Go to full version