vb编写图形绕一点旋转
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/29 13:25:28
vb编写图形绕一点旋转
vb编写图形绕一点旋转
vb编写图形绕一点旋转
正余弦函数
题目:已知一长轴为len,及另一长轴的端点坐标(x1,y1)和(x2,y2)
画出椭圆.
算法步骤:
1、求a,b: a=len/2 b=sqr((x1-x2)^2+(y1-y2)^2)
2、求旋转角α :求出(y1-y2)/(x1-x2)的反正切值即为α
3、求椭圆中心坐标(x0,y0): x0=(x1+x2)/2 y0=(y1+y2)/2
3、计算椭圆上点的坐标(x,y):
x=acosθ , y=bsinθ ( 0<=θ< 2*π)
4、计算图形绕原点旋转α 弧度后的坐标(xx,yy):
xx=x*cos(-α )+y*sin(-α )
yy=-x*sin(-α )+y*cos(-α )
5、计算椭圆中心从原点平移到(x0,y0)后椭圆上点的坐标(xxx,yyy):
xxx=xx+x0
yyy=yy+y0
6、在坐标(xxx,yyy)处画一各点
7、在( 0<=θ< 2*π)范围内,按一定间隔取值,重复3-7步骤,即得所要求的椭圆.
以下是vb代码,新建一个标准工程,不需做任何参数更改.打开代码窗口把代码粘贴进去,替换原来的所有代码,运行即可看效果.希望对你有用
Option Explicit
Dim X1, Y1, X0, Y0, X2, Y2 As Double
Dim A, B, PI As Double
Dim F As Boolean
Private Sub Form_Load()
PI = 3.14159265358979
F = False
DrawWidth = 2
Width = 10000
Height = 8000
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
X1 = X
Y1 = Y
B = 1000
F = True
End Sub
Public Sub tuoYuan()
Dim Jiao As Double
Dim i, m, n, m1, n1 As Double
Cls
X0 = (X1 + X2) / 2
Y0 = (Y1 + Y2) / 2
A = Sqr((X0 - X2) ^ 2 + (Y0 - Y2) ^ 2)
If X1 <> X2 Then
Jiao = Atn((Y1 - Y2) / (X1 - X2))
Else
Jiao = PI / 2
End If
Form1.PSet (X1, Y1), RGB(255, 0, 0)
Form1.PSet (X0, Y0), RGB(255, 0, 0)
Form1.PSet (X2, Y2), RGB(255, 0, 0)
For i = 0 To PI * 2 Step 0.01
m = A * Cos(i)
n = B * Sin(i)
'Form1.PSet (m + X0, n + Y0), RGB(255, 0, 0)
m1 = m * Cos(-Jiao) + n * Sin(-Jiao)
n1 = -m * Sin(-Jiao) + n * Cos(-Jiao)
Form1.PSet (m1 + X0, n1 + Y0), RGB(0, 255, 0)
Next i
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If F = True Then
X2 = X
Y2 = Y
Call tuoYuan
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
F = False
X2 = X
Y2 = Y
Call tuoYuan
End Sub