Option ExplicitPrivate Sub fMandleBrot() Dim x As Double Dim y As Double Dim ix As Integer Dim iy As Integer Dim nx As Integer Dim ny As Integer Dim MaxIter As Integer Dim xMin As Double Dim yMin As Double Dim xMax As Double Dim yMax As Double Dim bStop As Boolean Dim m
For iy = 0 To ny - 1 y = (yMin + iy * (yMax - yMin) / (ny - 1)) For ix = 0 To nx - 1 If bStop Then Exit Sub x = (xMin + ix * (xMax - xMin) / (nx - 1)) m = VBMandel(x, y, MaxIter) If m = MaxIter Then picWindow.PSet (ix, iy), RGB(ix, iy, 0) Else picWindow.PSet (ix, iy), RGB(0, 0, (ix + iy) / 2) End If Next If iy Mod 5 = 0 Then DoEvents Next
bStop = True
End SubPrivate Function VBMandel(cx As Double, cy As Double, MaxIter As Integer) As Integer Dim x2 As Double Dim y2 As Double Dim Temp As Double Dim x As Double Dim y As Double Dim Iter
x = 0 y = 0 x2 = 0 y2 = 0 Iter = 0
While ((Iter < MaxIter) And ((Abs(x2) + Abs(y2)) < 100000)) Temp = x2 - y2 + cx * 2 y = 2 * x * y + cy / 1.5 x = Temp x2 = x * x y2 = y * y Iter = Iter + 1 Wend VBMandel = Iter
End FunctionPrivate Sub cmdGraf_Click() fMandleBrot End Sub
You need to add a picturebox named picWindow,a command button named cmdGraf into Form1
Dim x As Double
Dim y As Double
Dim ix As Integer
Dim iy As Integer
Dim nx As Integer
Dim ny As Integer
Dim MaxIter As Integer
Dim xMin As Double
Dim yMin As Double
Dim xMax As Double
Dim yMax As Double
Dim bStop As Boolean
Dim m
bStop = False
nx = 256
ny = 256
xMin = -1.5
xMax = 0.5
yMin = -1
yMax = 1
MaxIter = 40
picWindow.Cls
For iy = 0 To ny - 1
y = (yMin + iy * (yMax - yMin) / (ny - 1))
For ix = 0 To nx - 1
If bStop Then Exit Sub
x = (xMin + ix * (xMax - xMin) / (nx - 1))
m = VBMandel(x, y, MaxIter)
If m = MaxIter Then
picWindow.PSet (ix, iy), RGB(ix, iy, 0)
Else
picWindow.PSet (ix, iy), RGB(0, 0, (ix + iy) / 2)
End If
Next
If iy Mod 5 = 0 Then DoEvents
Next
bStop = True
End SubPrivate Function VBMandel(cx As Double, cy As Double, MaxIter As Integer) As Integer
Dim x2 As Double
Dim y2 As Double
Dim Temp As Double
Dim x As Double
Dim y As Double
Dim Iter
x = 0
y = 0
x2 = 0
y2 = 0
Iter = 0
While ((Iter < MaxIter) And ((Abs(x2) + Abs(y2)) < 100000))
Temp = x2 - y2 + cx * 2
y = 2 * x * y + cy / 1.5
x = Temp
x2 = x * x
y2 = y * y
Iter = Iter + 1
Wend
VBMandel = Iter
End FunctionPrivate Sub cmdGraf_Click()
fMandleBrot
End Sub
注意把下载后的*.zip.jpg改名成*.zip