VB中的atn函数

 Atn函数返回一个double类型,指定一个数的反正切值。

用法就是:Atn(number)。number参数是一个double或者任何有效的数值表达式。

Atn函数的参数值(number)为直角三角形的两条直角边的比值(对边比邻边)。返回以弧度为单位的角。值的范围是-π/2和π/2之间。

Atn是Tan函数的反三角函数,Tan的参数值为角度,返回直角三角形的两边的比值。

下面介绍个例子:

我要判断两个点的位置并求出两个点组成的线段与水平面之间的夹角,用了一个Function函数,它的声明如下:

 

Public Function getAngel(ByVal x1 As Long, _
                         ByVal y1 As Long, _
                         ByVal x2 As Long, _
                         ByVal y2 As Long) As Long
    Const PI     As Double = 3.1415926
    Dim judge  As Boolean
    'VB中默认boolean值为False,这里如果是默认(即judge=False),结果永远等于0
    judge = True
    Dim Ang As Long
    Select Case judge
        Case x1 < x2 And y1 < y2
            Ang = Atn((y2 - y1) / (x2 - x1)) * 180 / PI
            
        Case x1 < x2 And y1 = y2
            Ang = 0
        Case x1 < x2 And y2 > y2
            Ang = Atn((y2 - y1) / (x2 - x1)) * 180 / PI + 360
        Case x1 = x2 And y1 < y2
            Ang = 90
        Case x1 = x2 And y1 = y2
            MsgBox "两个点重复,请从新输入! "
        Case x1 = x2 And y1 > y2
            Ang = 270
        Case x1 > x2 And y1 < y2
            Ang = Atn((y2 - y1) / (x2 - x1)) * 180 / PI + 180
        Case x1 > x2 And y1 = y2
            Ang = 180
        Case x1 > x2 And y1 > y2
            Ang = Atn((y2 - y1) / (x2 - x1)) * 180 / PI + 180
            
    End Select
    
    getAngel = Ang
    'angel = getAngel(x1, y1, x2, y2)
End Function


下面是调用这个函数的代码:

Private Sub Form_click()
        Text1(4).Text = Format(getAngel(CDbl(Text1(0).Text), CDbl(Text1(1).Text), CDbl(Text1(2).Text), CDbl(Text1(3).Text)), "0.000 ")
End Sub


调用过程代码过于简单,调用的时候只有按照代码的格式调用才能够正常运行。有兴趣的同学自己再改吧。

原先,我总感觉数学问题用Matlab解决很合适,了解原理之后编写也非常简单;但是用VB就很复杂,并且算出来也不精确。

但是我偶然间发现VB中也提供了比较全面的数学函数,很有意思。

看来以后数学方面的编程,我也能用VB编程了。

 

你可能感兴趣的:(编程,function,matlab,vb,360)