有几个地方要注意:
1、VB中的三角函数的角度全部是用弧度制表示的,如果是度数的话,应先乘以180再除以π转成弧度,再用函数计算结果。
2、VB中直接支持的三角函数有:Sin()、Cos()、Tan(),如果涉及到其它三角函数,可以从下面列出的代码中自己选择相应的函数:
Function
Sec(X)
As
Double
'正割
Sec
=
1
/
Cos(Angle)
End
Function
Function
Csc(X)
As
Double
'余割
Csc
=
1
/
Sin(Angle)
End
Function
Function
Cot(X)
As
Double
'余切
Cot
=
1
/
Tan(Angle)
End
Function
Function
ArcSin(X)
As
Double
'反正弦
ArcSin
=
Atn(X
/
Sqr(-X
X
+
1))
End
Function
Function
ArcCos(X)
As
Double
'反余弦
ArcCos
=
Atn(-X
/
Sqr(-X
X
+
1))
+
2
Atn(1)
End
Function
Function
ArcSec(X)
As
Double
'反正割
ArcSec
=
Atn(X
/
Sqr(X
X
-
1))
+
Sgn((X)
-
1)
(2
Atn(1))
End
Function
Function
ArcCsc(X)
As
Double
'反余割
ArcCsc
=
Atn(X
/
Sqr(X
X
-
1))
+
(Sgn(X)
-
1)
(2
Atn(1))
End
Function
Function
ArcCot(X)
As
Double
'反余切
ArcCot
=
Atn(X)
+
2
Atn(1)
End
Function
Function
HSin(X)
As
Double
'双曲正弦
HSin
=
(Exp(X)
-
Exp(-X))
/
2
End
Function
Function
HCos(X)
As
Double
'双曲余弦
HCos
=
(Exp(X)
+
Exp(-X))
/
2
End
Function
Function
HTan(X)
As
Double
'双曲正切
HTan
=
(Exp(X)
-
Exp(-X))
/
(Exp(X)
+
Exp(-X))
End
Function
Function
HSec(X)
As
Double
'双曲正割
HSec
=
2
/
(Exp(X)
+
Exp(-X))
End
Function
Function
HCsc(X)
As
Double
'双曲余割
HCsc
=
2
/
(Exp(X)
-
Exp(-X))
End
Function
Function
HCot(X)
As
Double
'双曲余切
HCot
=
(Exp(X)
+
Exp(-X))
/
(Exp(X)
-
Exp(-X))
End
Function
Function
HArcsin(X)
As
Double
'反双曲正弦
HArcsin
=
Log(X
+
Sqr(X
X
+
1))
End
Function
Function
HArccos(X)
As
Double
'反双曲余弦
HArccos
=
Log(X
+
Sqr(X
X
-
1))
End
Function
Function
HArctan(X)
As
Double
'反双曲正切
HArctan
=
Log((1
+
X)
/
(1
-
X))
/
2
End
Function
Function
HArcsec(X)
As
Double
'反双曲正割
HArcsec
=
Log((Sqr(-X
X
+
1)
+
1)
/
X)
End
Function
Function
HArccsc(X)
As
Double
'反双曲余割
HArccsc
=
Log((Sgn(X)
Sqr(X
X
+
1)
+
1)
/
X)
End
Function
Function
HArccot(X)
As
Double
'反双曲余切
HArccot
=
Log((X
+
1)
/
(X
-
1))
/
2
End
Function
sin(角度值)同理cos,tan
Atn()是VB反正切函数,VB不提供其他反三角函数,不过可以用下列公式导出:
Inverse
Sine
(反正弦):
Arcsin(X)
=
Atn(X
/
Sqr(-X
X
+
1))
Inverse
Cosine
(反余弦):
Arccos(X)
=
Atn(-X
/
Sqr(-X
X
+
1))
+
2
Atn(1)
Inverse
Secant
(反正割):
Arcsec(X)
=
Atn(X
/
Sqr(X
X
-
1))
+
Sgn((X)
-
1)
(2
Atn(1))
Inverse
Cosecant
(反余割):
Arccosec(X)
=
Atn(X
/
Sqr(X
X
-
1))
+
(Sgn(X)
-
1)
(2
Atn(1))
以上这些公式,其实就是应用了数学中的各类三角函数和正切函数之间的关系导出的。其中需要注意的是其中的
X
不能等于正负1。(引用-_-)
x都是弧度制
Function ArcSin(X) As Double '反正弦
ArcSin = Atn(X / Sqr(-X X + 1))
End Function
Function ArcCos(X) As Double '反余弦
ArcCos = Atn(-X / Sqr(-X X + 1)) + 2 Atn(1)
End Function
Function ArcSec(X) As Double '反正割
ArcSec = Atn(X / Sqr(X X - 1)) + Sgn((X) - 1) (2 Atn(1))
End Function
Function ArcCsc(X) As Double '反余割
ArcCsc = Atn(X / Sqr(X X - 1)) + (Sgn(X) - 1) (2 Atn(1))
End Function
Function ArcCot(X) As Double '反余切
ArcCot = Atn(X) + 2 Atn(1)
End Function
Function HSin(X) As Double '双曲正弦
HSin = (Exp(X) - Exp(-X)) / 2
End Function
Function HCos(X) As Double '双曲余弦
HCos = (Exp(X) + Exp(-X)) / 2
End Function
Function HTan(X) As Double '双曲正切
HTan = (Exp(X) - Exp(-X)) / (Exp(X) + Exp(-X))
End Function
扩展资料:
Visual Basic 开发的程序只能运行在Microsoft Windows中;且运行时还需要 VB 运行库。
Visual Basic 不能很好的综合Windows的基础 API,很多时候要使用低级运算的“小伎俩”来进行编程。而C 语言的低级内存运算比 VB 的要简单得多。
VB语言作为计算机语言的入门,是很多学生学习编程的入门级语言,大学生利用VB语言开始了他们学习计算机的世界。
Visual Basic的强大在于能够快速开发企业级软件,使程序员有更多的时间和精力考虑用户的需求,生产出满足用户需要的软件,特别适合大型企业级软件的开发,比如MIS、MRP、MRP II、ERP等重量级的软件,而不是花很长时间去制作一个只有几K的DLL。
参考资料来源:百度百科-vb
参考资料来源:百度百科-三角函数
工程—添加模块
代码:
Option Explicit
Const pi = 31419
Dim s As Single, r As Integer, str1 As String
Sub main()
r = 5
s = pi r ^ 2
str1 = "半径为" & Str(r) & "的圆面积为:" & Str(s) & " "
MsgBox str1, 32, "计算圆的面积"
End Sub
工程—属性—启动对象—Sub Main
没有,只有一个Atn()是VB反正切函数,VB不提供其他反三角函数,不过可以用下列公式导出:
反正弦
Arcsin(X)
=
Atn(X
/
Sqr(-X
X
+
1))
反余弦
Arccos(X)
=
Atn(-X
/
Sqr(-X
X
+
1))
+
2
Atn(1)
反正割
Arcsec(X)
=
Atn(X
/
Sqr(X
X
-
1))
+
Sgn((X)
-
1)
(2
Atn(1))
反余割
Arccosec(X)
=
Atn(X
/
Sqr(X
X
-
1))
+
(Sgn(X)
-
1)
(2
Atn(1))
Dim
A1
As
Integer,X
as
Integer'定义变量
X=Inputbox("请输入X的值","提示信息")
A1
=
Atn(X
/
Sqr(-X
X
+
1))
314
/
180
'可以先输入一个数
X
然后算出结果后显示出A1的值
A1
因为这里只能上传一张,所以写了一个单张的。装载系列的我也写了,只是窗体初始化会很麻烦。以下代码是实现单张的随意飞舞程序,能做到:
1、碰到窗体边缘时,会自动改变飞舞的方向(题意中要求的“返回”,只能是在一条线上来回移动,本程序实现蝴蝶出发位置随意,能在窗体内四处飞舞),而且头部也会跟着改变方向
2、窗体改变大小时,蝴蝶不会消失不见,会立即在新的窗体内继续飞舞
'建一个Picture1控件,在它的属性对话框内设置Appearance=0 AutoRedraw=True AutoSize=True BorderStyle=0,然后在它的Picture中加载蝴蝶
'再建一个Picture2控件,在它的属性对话框内设置Appearance=0 AutoRedraw=False AutoSize=False BorderStyle=0,其他默认属性都不得改变
'Form的BackColor设为与蝴蝶的背景色一致,本程序中,即&H80000009&
'再加一个Timer1控件
Private Const Pi = 31415926535
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Dim x0!, y0!, x1!, x2!, y1!, y2!, foot!, zt% 'x0和y0是框长高,(x1,y1)-(x2,y2)是窗体坐标系,foot是蝴蝶移动步长,zt是蝴蝶飞舞的线路标记
Dim k! 'k或1/k是飞行轨迹的斜率
Dim flag As Boolean 'flag判断是顺时针还是逆时针飞舞
Private Sub bmp_rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal theta As Single) '自定义角度旋转
Dim c1x As Integer, c1y As Integer
Dim c2x As Integer, c2y As Integer
Dim a As Single
Dim p1x As Integer, p1y As Integer
Dim p2x As Integer, p2y As Integer
Dim n As Integer, r As Integer
c1x = pic1ScaleWidth / 2
c1y = pic1ScaleHeight / 2
c2x = pic2ScaleWidth / 2
c2y = pic2ScaleHeight / 2
If c2x < c2y Then n = c2y Else n = c2x
n = n - 1
pic1hDC& = pic1hdc
pic2hDC& = pic2hdc
For p2x = 0 To n
For p2y = 0 To n
If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
r = Sqr(1& p2x p2x + 1& p2y p2y)
p1x = r Cos(a + theta)
p1y = r Sin(a + theta)
c0& = GetPixel(pic1hDC, c1x + p1x, c1y + p1y)
c1& = GetPixel(pic1hDC, c1x - p1x, c1y - p1y)
c2& = GetPixel(pic1hDC, c1x + p1y, c1y - p1x)
c3& = GetPixel(pic1hDC, c1x - p1y, c1y + p1x)
If c0& <> -1 Then xret& = SetPixel(pic2hDC, c2x + p2x, c2y + p2y, c0&)
If c1& <> -1 Then xret& = SetPixel(pic2hDC, c2x - p2x, c2y - p2y, c1&)
If c2& <> -1 Then xret& = SetPixel(pic2hDC, c2x + p2y, c2y - p2x, c2&)
If c3& <> -1 Then xret& = SetPixel(pic2hDC, c2x - p2y, c2y + p2x, c3&)
Next
t% = DoEvents()
Next
End Sub
Private Sub Place(ByVal picnum As Integer, ByVal x As Single, ByVal y As Single) '控制两个框的位置
If picnum = 1 Then
Picture1Left = x
Picture1Top = y
Else
Picture2Left = x
Picture2Top = y
End If
End Sub
Private Sub Direct(ByVal angle As Integer) '按指定角度旋转,angle是角度
angle = angle Mod 360
hudu = (Pi angle 1#) / (180 1#) '弧度
bmp_rotate Picture1, Picture2, hudu
End Sub
Private Sub Form_Activate()
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
MeScale (x1, y1)-(x2, y2)
Randomize
Picture1ScaleMode = 3
Picture2ScaleMode = 3
x0 = Picture1Width
Picture2Width = x0
y0 = Picture1Height
Picture2Height = y0
Picture1Visible = True
Picture2Visible = False
x = Int(1000 + (x2 - x1 - x0 - 1000) Rnd) '蝴蝶出发处的横坐标
y = Int(1000 + (y2 - y1 - y0 - 1000) Rnd) '蝴蝶出发处的纵坐标
k = y / x 'k为撞向水平边框或离开竖直边框的斜率,1/k则为撞向竖直边框或离开水平边框的斜率
Place 1, -x2, y2 - y0 '将框的复制源移出窗体的可见范围,但Visible属性又必须是True,否则复制会失败
Place 2, x, y '初始化蝴蝶的出发位置
Timer1Enabled = True
Timer1Interval = 100 '定义移动周期
zt = 0
End Sub
Private Sub Form_Click()
Timer1Enabled = Not Timer1Enabled
End Sub
Private Sub Form_Resize() '窗体改变大小时,坐标系需要重新定义
x1 = 0!
x2 = 10000!
y1 = 0!
y2 = 10000!
MeScale (x1, y1)-(x2, y2)
Cls
x0 = Picture1Width
Picture2Width = x0
y0 = Picture1Height
Picture2Height = y0
'如果蝴蝶在窗体外,则立即召回
If Picture2Left < x1 Then Place 2, x1, Picture2Top
If Picture2Top < y1 Then Place 2, Picture2Left, y1
If Picture2Left > x2 - x1 - x0 Then Place 2, x2 - x1 - x0, Picture2Top
If Picture2Top > y2 - y1 - y0 Then Place 2, Picture2Left, y2 - y1 - y0
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
'zt=0,1,2,3时,分别表示顺时针向右上(简称顺右上)或逆左下,顺右下或逆左上,顺左下或逆右上,顺左上或逆右下
Private Sub Timer1_Timer()
foot = 100
x = Picture2Left: y = Picture2Top
Select Case True
Case (flag = True And zt = 0) Or (flag = False And zt = 2)
Place 2, x + foot, y - k foot
'Line (x, y)-(x + foot, y - k foot)
Direct 360
If Picture2Left > x2 - x1 - x0 - foot Then zt = 1: flag = False '撞到垂直边框,将逆左上
If Picture2Top < foot Then zt = 1: flag = True '撞到水平边框,将顺右下
Case (flag = True And zt = 1) Or (flag = False And zt = 3)
Place 2, x + foot, y + foot / k
'Line (x, y)-(x + foot, y + foot / k)
Direct 270
If Picture2Left > x2 - x1 - x0 - foot Then zt = 2: flag = True '撞到垂直边框,将顺左下
If Picture2Top > y2 - y1 - y0 - foot Then zt = 2: flag = False '撞到水平边框,将逆右上
Case (flag = True And zt = 2) Or (flag = False And zt = 0)
Place 2, x - foot, y + k foot
'Line (x, y)-(x - foot, y + k foot)
Direct 180
If Picture2Left < foot Then zt = 3: flag = False '撞到垂直边框,将逆右下
If Picture2Top > y2 - y1 - y0 - foot Then zt = 3: flag = True '撞到水平边框,将顺左上
Case (flag = True And zt = 3) Or (flag = False And zt = 1)
Place 2, x - foot, y - foot / k
'Line (x, y)-(x - foot, y - foot / k)
Direct 90
If Picture2Left < foot Then zt = 0: flag = True '撞到垂直边框,将顺右上
If Picture2Top < foot Then zt = 0: flag = False '撞到水平边框,将逆左下
End Select
Picture2Visible = True
End Sub
以上就是关于在vb中。如何进行三角函数的程序代码编写全部的内容,包括:在vb中。如何进行三角函数的程序代码编写、有谁知道VB编程中运算sin、cos、tan、cot的程序怎么写、VB中的三角函数程序怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)