2017年9月15日 星期五

輸入角度計算各種三角函數

輸入角度計算各種三角函數的VB程式

數學上的三角函數,如sin30,用VB要如何寫程式碼?
 剛開始,我都以為是 Math.Sin30
但怎麼算都錯。
後來才發現,Sin後面跟的數字不是角度,而是弧度。所以程式碼為
 Math.Sin(30 * (Math.PI / 180))

1) 角度與徑度的轉
// Calculate the tangent of 30 degrees. angle = 30; radians = angle * (Math.PI/180); result = Math.Tan(radians); Console.WriteLine("The tangent of 30 degrees is {0}.", result); // Calculate the arctangent of the previous tangent. radians = Math.Atan(result); angle = radians * (180/Math.PI); Console.WriteLine("The previous tangent is equivalent to {0} degrees.", angle);

2) 程式

Public Class Form1
    Private Sub Label14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click

        Dim A1 As Double = Val(TextBox1.Text)
        Dim A As Double = A1 * (Math.PI / 180)

        Dim b1 As Double = Math.Sin(A)
        Dim b2 As Double = Math.Cos(A)
        Dim b3 As Double = Math.Tan(A)
        Dim b4 As Double = 1 / b1
        Dim b5 As Double = 1 / b2
        Dim b6 As Double = 1 / b3

        Label2.Text = b1.ToString
        Label3.Text = b2.ToString
        Label5.Text = b3.ToString
        Label7.Text = b4.ToString
        Label10.Text = b5.ToString
        Label12.Text = b6.ToString


    End Sub

   
End Class

3) 執行畫面

沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...