Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. 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 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer '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. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button2 = New System.Windows.Forms.Button Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog Me.SuspendLayout() ' 'Button1 ' Me.Button1.BackColor = System.Drawing.Color.Silver Me.Button1.Location = New System.Drawing.Point(552, 88) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(120, 40) Me.Button1.TabIndex = 0 Me.Button1.Text = "Go" ' 'PictureBox1 ' Me.PictureBox1.Location = New System.Drawing.Point(16, 16) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(512, 512) Me.PictureBox1.TabIndex = 1 Me.PictureBox1.TabStop = False ' 'Label1 ' Me.Label1.ForeColor = System.Drawing.Color.White Me.Label1.Location = New System.Drawing.Point(560, 24) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(120, 32) Me.Label1.TabIndex = 2 Me.Label1.Text = "Label1" ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(560, 152) Me.TextBox1.Multiline = True Me.TextBox1.Name = "TextBox1" Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.TextBox1.Size = New System.Drawing.Size(184, 88) Me.TextBox1.TabIndex = 3 Me.TextBox1.Text = "TextBox1" ' 'Button2 ' Me.Button2.BackColor = System.Drawing.Color.LightGray Me.Button2.Location = New System.Drawing.Point(560, 264) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(128, 40) Me.Button2.TabIndex = 4 Me.Button2.Text = "Save Image" ' 'SaveFileDialog1 ' ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.BackColor = System.Drawing.Color.Black Me.ClientSize = New System.Drawing.Size(762, 580) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.PictureBox1) Me.Controls.Add(Me.Button1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Mandelbrot Image Generator" Me.ResumeLayout(False) End Sub #End Region 'Image is HEIGHT by WIDTH pixels Const IMAGE_HEIGHT As Integer = 512 Const IMAGE_WIDTH As Integer = 512 Const MAXITER As Integer = 1024 'Dim mandelbrotBitmap As Bitmap = New Bitmap("red.bmp") 'create a new 512x512 pixel bitmap to store the image Dim mandelbrotBitmap As Bitmap = New Bitmap(IMAGE_HEIGHT, IMAGE_WIDTH) 'rectangle of the complex plane (may change by zooming) 'Dim xmin As Double = -0.5 'Dim xmax As Double = 0.5 'Dim ymin As Double = 0.5 'Dim ymax As Double = 1.5 Dim xmin As Double = -2.0 Dim xmax As Double = 2.0 Dim ymin As Double = -2.0 Dim ymax As Double = 2.0 Dim xdiff, ydiff As Double 'for user zoom rectangle Dim x1, x2, y1, y2 As Integer Dim new_xmin As Double = -2.0 Dim new_xmax As Double = 2.0 Dim new_ymin As Double = -2.0 Dim new_ymax As Double = 2.0 Dim isdragging As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'mandelbrotBitmap.SetPixel(5, 5, Color.Aqua) 'Dim p As Color() = mandelbrotBitmap.Palette.Entries() Dim i, j As Integer PictureBox1.Image = mandelbrotBitmap 'mandelbrotBitmap.SetPixel(5, 5, Color.FromArgb(255, 255, 0, 0)) 'For i = 0 To 255 ' For j = 0 To 255 ' mandelbrotBitmap.SetPixel(i, j, Color.FromArgb(255, i, j, 0)) ' Next j 'Next i TextBox1.Text &= "(" & xmin & ", " & ymin & ") to (" & xmax & ", " & ymax & ")" & Chr(13) & Chr(10) End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint 'Note: this method is called whenever the window becomes _dirty_ (needs to be redrawn) 'Dim g As Graphics = e.Graphics 'Dim myPen As Pen = New Pen(Color.White) 'Dim myRectangle As Rectangle = New Rectangle(400, 400, 200, 200) 'Dim rgn As Region = New Region(myRectangle) 'g.DrawImage(mandelbrotBitmap, 0, 0) PictureBox1.Refresh() 'g.DrawRectangle(myPen, myRectangle) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i, j, k As Integer Dim zRe, zIm, cRe, cIm As Double Dim zMagSquared As Double Dim zReNew, zImNew As Double Dim verticalStep, horizontalStep As Double xmin = new_xmin xmax = new_xmax ymin = new_ymin ymax = new_ymax xdiff = xmax - xmin ydiff = ymax - ymin verticalStep = ydiff / IMAGE_HEIGHT horizontalStep = xdiff / IMAGE_WIDTH 'Note: 0, 0 on screen is in upper left corner, so 'start at xmin, ymax for mandelbrotBitmap(0,0) For i = 0 To IMAGE_HEIGHT - 1 'i is row number cIm = ymax - i * verticalStep 'filling bitmap top to bottom For j = 0 To IMAGE_WIDTH - 1 cRe = xmin + j * horizontalStep 'filling left to right zRe = 0.0 zIm = 0.0 'now start iteration: z_i+1 = (z_i)^2 + c For k = 1 To MAXITER - 1 zReNew = zRe ^ 2 - zIm ^ 2 + cRe zImNew = 2 * zRe * zIm + cIm zRe = zReNew zIm = zImNew zMagSquared = zRe ^ 2 + zIm ^ 2 If zMagSquared > 4 Then Exit For Next k If k = MAXITER Then 'k = 0 mandelbrotBitmap.SetPixel(j, i, Color.FromArgb(255, 0, 255, 255)) Else mandelbrotBitmap.SetPixel(j, i, Color.FromArgb(255, k Mod 256, 0, 16 * (k Mod 16))) End If 'TextBox2.Text = TextBox2.Text & k & Chr(9) Next j Me.Refresh() 'TextBox2.Text = TextBox2.Text & Chr(13) & Chr(10) Next i End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown isDragging = True X1 = e.X Y1 = e.Y End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp Dim tmp, old_xmin, old_xmax, old_ymin, old_ymax As Integer isDragging = False X2 = e.X Y2 = e.Y If (X2 < X1) Then tmp = x1 x1 = x2 x2 = tmp End If If (Y2 < Y1) Then tmp = y1 Y1 = Y2 Y2 = tmp End If 'preserve aspect ratio y2 = y1 + IMAGE_HEIGHT / IMAGE_WIDTH * (x2 - x1) TextBox1.Text &= "x1= " & x1 & " Y1= " & y1 & " X2= " & x2 & " Y2= " & y2 & Chr(13) & Chr(10) 'reset (xmin, ymin) and (xmax, ymax) new_xmin = xmin + xdiff * (x1 / IMAGE_WIDTH) new_xmax = xmin + xdiff * (x2 / IMAGE_WIDTH) new_ymin = ymax - ydiff * (y2 / IMAGE_HEIGHT) new_ymax = ymax - ydiff * (y1 / IMAGE_HEIGHT) TextBox1.Text &= "(" & new_xmin & ", " & new_ymin & ") to (" & new_xmax & ", " & new_ymax & ")" & Chr(13) & Chr(10) End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove Label1.Text = "(" & e.X & ", " & e.Y & ")" End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click mandelbrotBitmap.Save("kgj.bmp") End Sub Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk End Sub End Class