Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

  1. #1
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Ok...here is my first pathetic effort...

    I did use the iComplex module, so that's a start!

    If you run the script, be patient. It takes a couple of minutes for the set to appear, depending on your machine.

    Improvements and enhancements are MUCH MUCH appreciated!

    Cheers... and I'm learning,

    Stan
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Hi Stan,

    very nice example.
    Here is slightly modified version:
    ' Example: Section 7.7 (page 222-224), Explorations with the Mandlebrot Set 
     
    ' From Stan Blank's Book:
    ' "Python Programming in OpenGL
    ' "A Graphical Approach to Programming
                   
    ' Converted by Stan Blank, this version is slightly downgraded in detail by Petr Schreiber               
    ' Using the Predator-Prey framework by Michael Clease
    ' Last modified: February 28, 2010  
     
    ' thinBasic does not use GLUT, we use instead tbgl
    Uses "TBGL"    
    
    ' insert Eros Olmi's new iComplex module
    Uses "iComplex"
    
    
    Function TBMain()
     
     ' Handle for our window
     Local hWnd  As DWord
     Local Width As Long
     Local Height As Long  
     Local n   As Long
     Local x,y, zz  As Double
     
     ' Declare iComplex variables
     Local z, c  As tComplex
     
     '# Initial values of width And height
     width = 400
     height = 400
     ' 
     ' Create and show window
     hWnd = TBGL_CreateWindowEx("Mandelbrot Set", Width, Height, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX Or %TBGL_WS_DONTSIZE) 
     TBGL_ShowWindow 
     
     ' Set Foreground from default White to Green
     TBGL_Color(76,152,51) 
     ' Set background from default black to white
     TBGL_BackColor(0,0,0) 
     ' Init OpenGl, like gluOrtho2D in example code
     TBGL_RenderMatrix2D( -2, -1.5, 1, 1.5 ) 
     
     TBGL_PointSize 1 
        
     ' Resets status of all keys 
     TBGL_ResetKeyState() 
     
     Local percent As Double 
     
     TBGL_NewList 1
     
      ' Choose each pixel in the graphics window  
      TBGL_BeginPoly(%GL_POINTS) 
      
       For x = -2 To 1 Step 0.0075
        For y = -1.5 To 1.5 Step 0.0075  
        
         ' Set complex points
         c = iComplex_Set(x,y)
         z = iComplex_Set(x,y) 
         n = 0
         
         For n = 1 To 10
          ' Equation is z = z*z + c
          z = iComplex_Mul(z,z)
          z = iComplex_Add(z,c)  
          
          ' Distance from origin to complex point
          zz = iComplex_Abs(z)
          
          ' if distance is > 2.0, then point is NOT in the M-Set
          ' so plot it... the M-Set will be black                   
          If zz > 2.0 Then      
           TBGL_Color zz*100,0,0
           TBGL_Vertex(x,y)     
          End If 
         Next
        Next                   
       
        ' ESCAPE key to exit application + status
        percent = (x-(-2))/3*100
        TBGL_SetWindowTitle(hWnd, Format$(percent, "#.00")+"%")
        If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit For
        
       Next  
      TBGL_EndPoly
      
     TBGL_EndList
     
     TBGL_SetWindowTitle(hWnd, "Behold - the Mandlebrot! :)")     
     
     ' Main loop
     While TBGL_IsWindow(hWnd)
      
      TBGL_ClearFrame ' glClear(GL_COLOR_BUFFER_BIT)
       TBGL_CallList 1
      TBGL_DrawFrame                      
      
      If TBGL_GetWindowKeyOnce(hWnd, %VK_ESCAPE) Then Exit While
      
     Wend 
     
     TBGL_DestroyWindow
    End Function 
     
    'Python code omitted due to length... see text.
    
    With new release of thinBASIC, there will be possibility to avoid usage of display lists, resulting in slightly faster render, and much lower memory consumption.

    Regarding processing speed ... I think Charles could have idea how.

    The modified example above adds more color detail, and possibility to abort calculation to see at least part of the image.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Stan,

    I'm using your coefficients but calculating with plain floats, and I've got one working with Petr's test TBGL (to be released with the next ThinBasic beta.)

    The fractal is rendered to a texture which is then mapped to a vertex array instead of an Opengl display list.
    Using Oxygen compiled code, it renders in a a fraction of second.

    Here is the code for generating the texture. Anybody want to try it in a TBGL prog

    Charles.

     
     uses "TBGL","Oxygen"
     '.......
     '
     '
     ' test texture  
     
     Dim Texture(65536*4) As tbgl_tRGBA
     '--------------------------------------------
     'OXYGEN COMPILE & EXEC TEXTURE GENERATION
     '--------------------------------------------
     dim src as string
     src="
     basic
     dim xman,yman,xcor,ycor, xtemp,xstp,ystp,lmt as double
     dim i,p,maxit, xpix, ypix, color as long
     dim tex at p as long : p=#Texture
     '
     xstp=3/512
     ystp=3/512
     lmt=4
     '
     ycor=-1.5
     for ypix=1 to 512
      xcor=-2
      for xpix=1 to 512
       xman=0
       yman=0
       i=0
       maxit=50
       do
        xtemp=xman*xman + yman*yman
        if xtemp>lmt then exit do
        if i>=maxit then exit do
        xtemp = xman*xman - yman*yman + xcor 'REAL PART
        yman = 2*xman*yman + ycor      'IMAGINARY PART
        xman = xtemp
        i+=1
       end do
       if i>=maxit then color = 200*65536 else color = i*40
       tex=color : p+=4
       xcor+=xstp
       next
       ycor+=ystp
     next
    
     "
     'msgbox 0,o2_prep src
     o2_asmo src
     if len(o2_error) then
      msgbox 0,o2_error
      stop
     else
      o2_exec
     end if
     '--------------------------------------------
     
     Local TexString As String = Peek$(VarPtr(Texture(1)), 65536*4*SizeOf(tbgl_tRGBA))
    

  4. #4
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    @Stan That is great that you are able to do something so complex already Stan with thinBasic. The teacher is a good student too, we see

    @PetrYou are always there to help us learn how to use your modules in better ways. Another great example, thanks. It looks beautiful.

    @CharlesWhen I was rendering Miller's Madness in Super Function Plotter, I thought... I bet if Charles wrote parts of this in Oxygen it would render very fast. So in the meantime I cheated. I cut the step size in the for loop and used line strips instead of points to get a faster render. If you have the time, can you convert plotFunc and plotFunc2 to oxygen, then super plotter function can be named super function plotter turbo! You can find the source here:http://community.thinbasic.com/index...=3261.msg24445
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  5. #5

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set


    Hi Kent, If we precompile using the the function strings as source, it should speed things up quite a lot.



    Here is a quicky Example using the Mandelbrot texture above. I have borrowed one of Petr's standard TBGL examples for this.
    Attached Files Attached Files

  6. #6

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set


    Instead of using the iteration tally to determine colour, there are other possibilities to produce a wide range of imagery from the Mandelbrot algorithm.


    Attached Files Attached Files

  7. #7
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Charles those are super fast and cool! Thanks just in time. I working on plotter program 2 which is a lot more advanced and hopefully studying your code I can make it render really fast. Thanks!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  8. #8
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Hi Petr, Charles, and Kent...

    Very, very nice!

    Thanks Petr... and another "thanks" to Charles for the speedup. I knew that I would learn something from the experts. And Kent, your function plotter just keeps getting better!

    Charles, would the new iComplex library benefit from Oxygen? The reason I'm asking is that one of the nice things about having a complex number library is to ease the mathematical manipulation of complex numbers a bit. The Mandelbrot Set is fairly easy to convert to floats (which you have done) as long as we use z = z^2 + c, but when we try z = z^n + c (where n > 2) or some of the transcendental functions, it becomes more difficult. Calculating the classic Newton's Method in the complex plane using z = z - (z^3 - 1)/(3*z^2) is much more convoluted using floats.

    Just curious... and I am REALLY impressed with your rotating M-Set! Just beautiful!

    Cheers,

    Stan

  9. #9
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Stan,

    if possible I would like to know how much slower is thinBasic compared to Python in Mandelbrot example.
    As soon as I will release next version (that will come with about other 20 new function working on complex numbers) I would like to work a bit on execution speed optimization.

    Thanks a lot
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  10. #10
    Member sblank's Avatar
    Join Date
    Feb 2010
    Location
    Wayne City, Illinois
    Posts
    80
    Rep Power
    23

    Re: Example: Section 7.7 (pages 222-224) Explorations with the Mandelbrot Set

    Hi Eros,

    Using Petr's version above and trying to match the algorithm and coloration exactly, Python takes just under 5 seconds on my computer. ThinBasic using iComplex takes about 12 seconds.

    One big difference in rendering is that Python renders line by line (at least in my code) so that you can see the M-Set being built. Petr added a nice % progress indicator in the caption bar, but it would be nice to see the "real-time" rendering take place. The other issue... and it may not be fixable at this point... is that in Python you can simply type: z = z**2 + c for the equation and as long as z and c are complex number types, the math manipulation is handled correctly. This is probably an operator overloading "thing" that may not be possible in ThinBasic?

    I'm thinking that when I use an equation such as: z = z - (z^3 - 1)/(3*z^2) it will be a bit more complicated to use the correct steps in your iComplex library... I'll work on that!

    Please don't view this as a complaint... it isn't! I'm thrilled to see the current library and the upcoming modifications and additions! Interesting times lay ahead, I think!

    Cheers... and thanks,

    Stan

    Quote Originally Posted by Eros Olmi
    Stan,

    if possible I would like to know how much slower is thinBasic compared to Python in Mandelbrot example.
    As soon as I will release next version (that will come with about other 20 new function working on complex numbers) I would like to work a bit on execution speed optimization.

    Thanks a lot
    Eros

Page 1 of 4 123 ... LastLast

Similar Threads

  1. 3D Mandelbrot Set
    By zak in forum General
    Replies: 11
    Last Post: 29-11-2010, 06:40
  2. Benoit Mandelbrot, RIP
    By LanceGary in forum Shout Box Area
    Replies: 6
    Last Post: 05-11-2010, 03:35

Members who have read this thread: 35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •