求 VB6 畫二次函式圖象的方法
- 2021-12-11
‘在Form1窗體中新增一個圖片框和兩個命令按鈕
’貼入下面的程式碼,即可:
‘————————————————————
Const Pi = 3。1415926535 ’定義圓周率
Dim a, wor
‘定義用於在Picture1上的一個位置列印字元函式
Private Function PrintWord(X, Y, Word As String)
With Picture1
。CurrentX = X
。CurrentY = Y
。ForeColor = RGB(0, 0, 255)
End With
Picture1。Print Word
End Function
’定義畫點函式
Private Function DrawDot(Px, Py, Color)
Picture1。PSet (Px, Py), Color
End Function
Sub XY() ‘建立直角座標系
Picture1。DrawWidth = 1 ’設定線條寬度
Picture1。Cls
‘設定使用者座標系,座標原點在Picture1中心
Picture1。Scale (-10, 10)-(10, -10)
Picture1。Line (-10, 0)-(10, 0), RGB(0, 0, 255)
Picture1。Line -(9。5, 0。5), RGB(0, 0, 255)
Picture1。Line (10, 0)-(9。5, -0。5), RGB(0, 0, 255)
Picture1。ForeColor = RGB(0, 0, 255)
Picture1。Print “X”
’畫 X 軸
Picture1。Line (0, -10)-(0, 10), RGB(0, 0, 255)
Picture1。Line -(0。5, 9。5), RGB(0, 0, 255)
Picture1。Line (0, 10)-(-0。5, 9。5), RGB(0, 0, 255)
Picture1。Print “Y”
‘畫 Y 軸
For lin = -9 To 9
Picture1。Line (lin, 0)-(lin, 0。25)
wor = PrintWord(lin - 0。5, -0。5, Str(lin))
Picture1。Line (0, lin)-(-0。25, lin)
If lin <> 0 Then
wor = PrintWord(-0。9, lin, Str(lin))
End If
Next lin
Picture1。DrawWidth = 2
End Sub
Private Sub Command1_Click()
For a = -3 To 3 Step Pi / 6000
Dot = DrawDot(a, a ^ 2, RGB(0, 0, 0))
Next a
wor = PrintWord(4, 9, “二次曲線 y=x^2”)
End Sub
Private Sub Command2_Click() ’清空螢幕
XY
End Sub
Private Sub Form_Load()
Me。Caption = “數學函式作圖?quot;”
Me。Show
Me。AutoRedraw = True
Command1。Caption = “二次曲線”
Command2。Caption = “清空”
XY
End Sub
Private Sub Form_Resize()
Picture1。Width = Me。Width * 0。94
Picture1。Height = Me。Height - (Command1。Height * 3 + 100)
Command1。Top = Me。Height - (Command1。Height * 2 + 100)
Command2。Top = Me。Height - (Command1。Height * 2 + 100)
Command1。Left = Me。Width * 0。01
Command2。Left = Me。Width * 0。21
XY
End Sub
sub form_click ()
dim cx, cy, msg, xpos, ypos ‘ declare variables。
scalemode = 3 ’ 設定 scalemode 為畫素。
drawwidth = 5 ‘ 設定 drawwidth。 forecolor = qbcolor(4) ’ 設定前景為紅色。
fontsize = 24 ‘ 設定點的大小。
cx = scalewidth / 2 ’ 得到水平中點。
cy = scaleheight / 2 ‘ 得到垂直中點。
cls ’ 清窗體。
msg = “happy new year!” currentx = cx - textwidth(msg) / 2 ‘ 水平位置。
currenty = cy - textheight(msg) ’ 垂直位置。
print msg ‘ 列印訊息。
do xpos = rnd * scalewidth ’ 得到水平位置。
ypos = rnd * scaleheight ‘ 得到垂直位置。
pset (xpos, ypos), qbcolor(rnd * 15) ’ 畫五彩碎紙。
doevents ‘ 進行
loop ’ 其它處理。
end sub
可以在支援這個pset的所有控制元件上使用繪圖語句,建議在picture中放置點陣圖座標影象,並在影象中繪製即可有座標
上一篇:機械式快門是什麼