The following is some Visual Basic code I came up with for drawing the Mandelbrot Set.
fractalmodule.vb:
Imports System.ComponentModel
Imports System.Drawing
Public Class MainForm
Inherits System.Windows.Forms.Form
Private RealMax, RealMin As Double
Private ImaginaryMax, ImaginaryMin As Double
Private MandelPicture As Bitmap
Private IsDragging As Boolean
Private DragRect As Rectangle
Private Distance As Integer
Private MouseTick As Integer
Private CalcTime As Single
Public Sub New()
MyBase.New()
MainForm = Me
InitializeComponent()
RealMax = 0.75
RealMin = -2.25
ImaginaryMax = 1.5
ImaginaryMin = -1.5
Show()
Calc()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub Calc()
Dim p, q As Integer
Dim pMax, qMax As Integer
Dim MandelValue As Integer
Dim MandelColour As Color
Dim ColourMultiplier As Integer
Dim StopWatch As DateTime = DateTime.Now
MandelPicture = New Bitmap(Picture.Width, Picture.Height)
pMax = Picture.Width
qMax = Picture.Height
ColourMultiplier = CInt(256 / Iterations)
For p = 0 To pMax - 1
For q = 0 To qMax - 1
MandelValue = (ColourMultiplier * GetMandel(ScaleIt(pMax, p, _
RealMin, RealMax), ScaleIt(qMax, q, ImaginaryMin, _
ImaginaryMax))) - 1
MandelColour = Color.FromArgb(MandelValue, MandelValue, _
MandelValue)
MandelPicture.SetPixel(p, q, MandelColour)
Next
Status.Text = Int(p / pMax * 100) & "%"
Next
Picture.Image = MandelPicture
CalcTime = CSng(DateDiff(Microsoft.VisualBasic.DateInterval.Second, _
StopWatch, DateTime.Now))
PutViewportInStatus(RealMin, RealMax, ImaginaryMin, ImaginaryMax, _
CalcTime)
End Sub
Private Sub Picture_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Picture.MouseMove
Dim TempPic As Image
Dim MyGfx As Graphics
Dim MyPen As New Pen(Color.FromKnownColor( _
Drawing.KnownColor.IndianRed), 1)
If IsDragging Then
If e.X - DragRect.Left > e.Y - DragRect.Top Then
Distance = e.X - DragRect.Left
Else
Distance = e.Y - DragRect.Top
End If
If MouseTick > 5 Then 'only draw every 5 pixels of movement
MouseTick = 0
TempPic = MandelPicture.Clone( _
System.Drawing.Rectangle.FromLTRB(0, 0, MandelPicture.Width, _
MandelPicture.Height), Drawing.Imaging.PixelFormat.DontCare)
MyGfx = Graphics.FromImage(TempPic)
MyGfx.DrawRectangle(MyPen, DragRect.Left, DragRect.Top, _
Distance, Distance)
Picture.Image = TempPic
End If
MouseTick += 1
PutViewportInStatus(ScaleIt(Picture.Width, DragRect.Left, RealMin, _
RealMax), ScaleIt(Picture.Height, DragRect.Top, ImaginaryMin, _
ImaginaryMax), ScaleIt(Picture.Width, DragRect.Left + Distance, +
RealMin, RealMax), ScaleIt(Picture.Height, DragRect.Top + _
Distance, ImaginaryMin, ImaginaryMax))
Else
Status.Text = "Location: (" & Format(ScaleIt(Picture.Width, e.X, _
RealMin, RealMax), "0.00 ") & Format(ScaleIt(Picture.Height, _
e.Y, ImaginaryMin, ImaginaryMax), "+ 0.00;- 0.00") & ")"
End If
End Sub
Private Sub PutViewportInStatus(ByVal pMin As Double, ByVal qMin As Double, _
ByVal pMax As Double, ByVal qMax As Double, Optional ByVal Time _
As Single = 0)
Status.Text = "Viewport: (" & Format(pMin, "0.00;-0.00") & _
Format(qMin, " + 0.00; - 0.00") & ") - (" & Format(pMax, _
"0.00;-0.00") & Format(qMax, " + 0.00; - 0.00") & ")" & _
Format(Time, "\; (0.00 seconds);;#")
End Sub
Private Sub Picture_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Picture.MouseDown
Select Case e.Button
Case MouseButtons.Left
IsDragging = True
DragRect.X = e.X
DragRect.Y = e.Y
End Select
End Sub
Private Sub Picture_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Picture.MouseUp
Dim Input As String
Select Case e.Button
Case MouseButtons.Left
If IsDragging = True Then
IsDragging = False
Picture.Image = MandelPicture
If Distance > 0 Then
RealMin = ScaleIt(Picture.Width, DragRect.Left, _
RealMin, RealMax)
ImaginaryMin = ScaleIt(Picture.Height, DragRect.Top, _
ImaginaryMin, ImaginaryMax)
RealMax = ScaleIt(Picture.Width, DragRect.Left + _
Distance, RealMin, RealMax)
ImaginaryMax = ScaleIt(Picture.Height, DragRect.Top + _
Distance, ImaginaryMin, ImaginaryMax)
Calc()
End If
End If
Case MouseButtons.Right
IsDragging = False
'Input = InputBox("0 < Iterations <= 256", "64").Trim
'If IsNumeric(Input) Then
'If CInt(Input) > 0 And CInt(Input) <= 256 Then
' Iterations = CInt(Input)
' Calc()
'Else
' Beep()
'End If
'End If
End Select
End Sub
Private Sub Picture_MouseLeave(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Picture.MouseLeave
PutViewportInStatus(RealMin, RealMax, ImaginaryMin, ImaginaryMax, _
CalcTime)
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Private WithEvents Status As System.Windows.Forms.StatusBar
Private WithEvents Picture As System.Windows.Forms.PictureBox
Dim WithEvents MainForm As System.Windows.Forms.Form
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.Picture = New System.Windows.Forms.PictureBox()
Me.Status = New System.Windows.Forms.StatusBar()
Me.SuspendLayout()
'
'Picture
'
Me.Picture.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or _
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.Picture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Picture.Cursor = System.Windows.Forms.Cursors.Cross
Me.Picture.Name = "Picture"
Me.Picture.Size = New System.Drawing.Size(397, 373)
Me.Picture.TabIndex = 0
Me.Picture.TabStop = False
'
'Status
'
Me.Status.Location = New System.Drawing.Point(0, 375)
Me.Status.Name = "Status"
Me.Status.Size = New System.Drawing.Size(397, 20)
Me.Status.TabIndex = 1
Me.Status.Text = "Please wait"
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(397, 395)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Status, _
Me.Picture})
Me.Font = New System.Drawing.Font("Tahoma", 8.0!)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "MainForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "Mandelbrot Set"
Me.ResumeLayout(False)
End Sub
#End Region
End Class
mainform.vb:
Imports System.ComponentModel
Imports System.Drawing
Public Class MainForm
Inherits System.Windows.Forms.Form
Private RealMax, RealMin As Double
Private ImaginaryMax, ImaginaryMin As Double
Private MandelPicture As Bitmap
Private IsDragging As Boolean
Private DragRect As Rectangle
Private Distance As Integer
Private MouseTick As Integer
Private CalcTime As Single
Public Sub New()
MyBase.New()
MainForm = Me
InitializeComponent()
RealMax = 0.75
RealMin = -2.25
ImaginaryMax = 1.5
ImaginaryMin = -1.5
Show()
Calc()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub Calc()
Dim p, q As Integer
Dim pMax, qMax As Integer
Dim MandelValue As Integer
Dim MandelColour As Color
Dim ColourMultiplier As Integer
Dim StopWatch As DateTime = DateTime.Now
MandelPicture = New Bitmap(Picture.Width, Picture.Height)
pMax = Picture.Width
qMax = Picture.Height
ColourMultiplier = CInt(256 / Iterations)
For p = 0 To pMax - 1
For q = 0 To qMax - 1
MandelValue = (ColourMultiplier * GetMandel(ScaleIt(pMax, _
p, RealMin, RealMax), ScaleIt(qMax, q, ImaginaryMin, _
ImaginaryMax))) - 1
MandelColour = Color.FromArgb(MandelValue, MandelValue, _
MandelValue)
MandelPicture.SetPixel(p, q, MandelColour)
Next
Status.Text = Int(p / pMax * 100) & "%"
Next
Picture.Image = MandelPicture
CalcTime = CSng(DateDiff(Microsoft.VisualBasic.DateInterval.Second, _
StopWatch, DateTime.Now))
PutViewportInStatus(RealMin, RealMax, ImaginaryMin, ImaginaryMax, _
CalcTime)
End Sub
Private Sub Picture_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Picture.MouseMove
Dim TempPic As Image
Dim MyGfx As Graphics
Dim MyPen As New Pen(Color.FromKnownColor(Drawing.KnownColor.IndianRed _
), 1)
If IsDragging Then
If e.X - DragRect.Left > e.Y - DragRect.Top Then
Distance = e.X - DragRect.Left
Else
Distance = e.Y - DragRect.Top
End If
If MouseTick > 5 Then 'only draw every 5 pixels of movement
MouseTick = 0
TempPic = MandelPicture.Clone( _
System.Drawing.Rectangle.FromLTRB(0, 0, MandelPicture.Width, _
MandelPicture.Height), Drawing.Imaging.PixelFormat.DontCare)
MyGfx = Graphics.FromImage(TempPic)
MyGfx.DrawRectangle(MyPen, DragRect.Left, DragRect.Top, _
Distance, Distance)
Picture.Image = TempPic
End If
MouseTick += 1
PutViewportInStatus(ScaleIt(Picture.Width, DragRect.Left, RealMin, _
RealMax), ScaleIt(Picture.Height, DragRect.Top, ImaginaryMin, _
ImaginaryMax), ScaleIt(Picture.Width, DragRect.Left + Distance, _
RealMin, RealMax), ScaleIt(Picture.Height, DragRect.Top + _
Distance, ImaginaryMin, ImaginaryMax))
Else
Status.Text = "Location: (" & Format(ScaleIt(Picture.Width, e.X, _
RealMin, RealMax), "0.00 ") & Format(ScaleIt(Picture.Height, _
e.Y, ImaginaryMin, ImaginaryMax), "+ 0.00;- 0.00") & ")"
End If
End Sub
Private Sub PutViewportInStatus(ByVal pMin As Double, ByVal qMin As Double, _
ByVal pMax As Double, ByVal qMax As Double, Optional ByVal Time As _
Single = 0)
Status.Text = "Viewport: (" & Format(pMin, "0.00;-0.00") & _
Format(qMin, " + 0.00; - 0.00") & ") - (" & Format(pMax, _
"0.00;-0.00") & Format(qMax, " + 0.00; - 0.00") & ")" & _
Format(Time, "\; (0.00 seconds);;#")
End Sub
Private Sub Picture_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Picture.MouseDown
Select Case e.Button
Case MouseButtons.Left
IsDragging = True
DragRect.X = e.X
DragRect.Y = e.Y
End Select
End Sub
Private Sub Picture_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Picture.MouseUp
Dim Input As String
Select Case e.Button
Case MouseButtons.Left
If IsDragging = True Then
IsDragging = False
Picture.Image = MandelPicture
If Distance > 0 Then
RealMin = ScaleIt(Picture.Width, DragRect.Left, RealMin, _
RealMax)
ImaginaryMin = ScaleIt(Picture.Height, DragRect.Top, _
ImaginaryMin, ImaginaryMax)
RealMax = ScaleIt(Picture.Width, DragRect.Left + _
Distance, RealMin, RealMax)
ImaginaryMax = ScaleIt(Picture.Height, DragRect.Top + _
Distance, ImaginaryMin, ImaginaryMax)
Calc()
End If
End If
Case MouseButtons.Right
IsDragging = False
'Input = InputBox("0 < Iterations <= 256", "64").Trim
'If IsNumeric(Input) Then
'If CInt(Input) > 0 And CInt(Input) <= 256 Then
' Iterations = CInt(Input)
' Calc()
'Else
' Beep()
'End If
'End If
End Select
End Sub
Private Sub Picture_MouseLeave(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Picture.MouseLeave
PutViewportInStatus(RealMin, RealMax, ImaginaryMin, ImaginaryMax, _
CalcTime)
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Private WithEvents Status As System.Windows.Forms.StatusBar
Private WithEvents Picture As System.Windows.Forms.PictureBox
Dim WithEvents MainForm As System.Windows.Forms.Form
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.Picture = New System.Windows.Forms.PictureBox()
Me.Status = New System.Windows.Forms.StatusBar()
Me.SuspendLayout()
'
'Picture
'
Me.Picture.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or _
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.Picture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Picture.Cursor = System.Windows.Forms.Cursors.Cross
Me.Picture.Name = "Picture"
Me.Picture.Size = New System.Drawing.Size(397, 373)
Me.Picture.TabIndex = 0
Me.Picture.TabStop = False
'
'Status
'
Me.Status.Location = New System.Drawing.Point(0, 375)
Me.Status.Name = "Status"
Me.Status.Size = New System.Drawing.Size(397, 20)
Me.Status.TabIndex = 1
Me.Status.Text = "Please wait"
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(397, 395)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Status, Me.Picture})
Me.Font = New System.Drawing.Font("Tahoma", 8.0!)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "MainForm"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "Mandelbrot Set"
Me.ResumeLayout(False)
End Sub
#End Region
End Class