Fractal Viewer

By | 2002-06-01

A simple fractal viewer.It plots the Mandelbrot Set at the desired size.
All you need to do is open a blank form and paste the code in the editor.

Original Author: Theo Kandiliotis

Inputs

none,but you can play around with some of the constants and variables on the code in order to produce different variations of the fractal.

Assumptions

the Mandelbrot Set is generated by iterating the
function
Zn=Zn-1^2 + C
where Zn,Zn-1,C are complex numbers.

Returns

the dazzling Mandelbrot Set,the most famous fractal.

API Declarations

Private Declare Function SetPixel Lib “gdi32” (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
Private Plotting As Boolean

Code

Private Sub Form_Click()
  'These four constants define the rectangular region
  'of the complex plain that will be iterated.
  'Change the values to zoom in/out.
  Const ComplexPlain_X1 As Currency = -2
  Const ComplexPlain_Y1 As Currency = 2
  Const ComplexPlain_X2 As Currency = 2
  Const ComplexPlain_Y2 As Currency = -2
  
  'These two variables are used to store the
  'ScaleWidth and ScaleHeight values,for
  'faster access.
  Dim ScreenWidth As Integer
  Dim ScreenHeight As Integer
  
  'These two variables reflect the X and Y
  'intervals of the loop that moves from
  '(ComplexPlain_X1,ComplexPlain_Y1) to
  '(ComplexPlain_X2,ComplexPlain_Y2) in
  'the complex plain.
  Dim StepX As Currency
  Dim StepY As Currency
  
  'These two are used in the main loop.
  Dim X As Currency
  Dim Y As Currency
  
  'Cx and Cy are the real and imaginary part
  'respectively of C,in the function
  ' Zv=Zv-1^2 + C
  Dim Cx As Currency
  Dim Cy As Currency
  
  'Zx and Zy are the real and imaginary part
  'respectively of Z,in the function
  ' Zv=Zv-1^2 + C
  Dim Zx As Currency
  Dim Zy As Currency
  
  'This byte variable is assigned a number
  'for each pixel in the form.
  Dim Color As Byte
  
  'Used in the function that we iterate.
  Dim TempX As Currency
  Dim TempY As Currency
  
  ScreenWidth = Me.ScaleWidth
  ScreenHeight = Me.ScaleHeight
  
  'Calculate the intervals of the loop.
  StepX = Abs(ComplexPlain_X2 - ComplexPlain_X1) / ScreenWidth
  StepY = Abs(ComplexPlain_Y2 - ComplexPlain_Y1) / ScreenHeight
  
  'Clear the form.
  Cls
  
  Plotting = True
  
  For X = 0 To ScreenWidth
   For Y = 0 To ScreenHeight
    
     Cx = ComplexPlain_X1 + X * StepX
     Cy = ComplexPlain_Y2 + Y * StepY
     Zx = 0
     Zy = 0
     Color = 0
  
     'If you want more fancy fractals,change the
     '255 to a higher number,but know that the
     'higher you make it,the longer it takes
     'for the fractal to be plotted.
     While (Not (Zx * Zx + Zy * Zy > 4)) And Color < 255 And Plotting
      TempX = Zx
      TempY = Zy
      Zx = TempX * TempX - TempY * TempY + Cx
      Zy = 2 * TempX * TempY + Cy
      Color = Color + 1
     Wend
    
     If Not Plotting Then Exit Sub
    
     'You can change Color*100 to something else
     'in order to get other color schemes in the
     'fractal.The function you aply must always
     'return a value in the range (0 to 16777215)
     SetPixel Me.hdc, X, Y, Color * 100
    
   Next
   Me.Refresh
   DoEvents
  Next
  Plotting = False
End Sub
Private Sub Form_Load()
  
  Me.AutoRedraw = True
  Me.ScaleMode = 3
  Me.Caption = "The Mandelbrot Set"
  
  MsgBox "Resize the form and click on it to get the fractal." & vbCrLf & _
  "Keep in mind that large fractals take longer to appear.", vbInformation, "The Mandelbrot Set"
End Sub
Private Sub Form_Unload(Cancel As Integer)
  
  Plotting = False
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.