Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: canvas_setpixel (mandelbrot)

  1. #1

    canvas_setpixel (mandelbrot)

    hello. I wanted to explore some UI/Canvas functions and started this mandelbrot-looks-like example. the problem zones I have marked. perhaps anybody can check this code example. the aim of this example I show as image my results shows only a black creating pixel canvas window, but that's not ok.

    use at own risk.

    ' Empty GUI script created on 10-25-2011 12:48:25 by largo_winch (ThinAIR)
    
    Uses "console", "ui"
    
    Randomize Timer
    
    Function TBMain () As Long
        Local hwin As DWord
        Local tmr, xmin, xmax, ymin, ymax, cx, cy, dcx, dcy, x, y, zr, zi As Single
        Local xi, j,k, numiter, numxpix, numypix, te, ts As Long
        Local color_map(216) As Long
        Local stuff_done As Long
        Local s As String  
     
        hWin = Canvas_Window ("Canvas_colors_testing - kind of mandelbrot", 50, 50,  800, 600)     
            
        Canvas_Attach hwin, 0, Redraw
        Canvas_Scale Pixels
               
        xmin = -.65 : xmax = -.45 : ymin = .52 : ymax = .72 : numiter = 215
        numxpix = 800 
        numypix = 600 
        dcx = (xmax - xmin)/(numxpix - 1)
        dcy = (ymin - ymax)/(numypix - 1)
        
        '-----------------------> 1 --------------------------->
        ReDim color_map (216) '(6*6*6) 
        For xi = 1 To 5
            For j = 1 To 5
                For k = 1 To 5
                    color_map(xi + 6*j + 36*k) = Rgb(51*j,51*xi,51*k)                
                Next
            Next
        Next
        ts = Timer
        tmr = Timer
        cx = xmin
        
        '-----------------------> 2 --------------------------->      
        ReDim color_map(216)
        For xi = 1 To numxpix
            cy = ymax
            For j = 1 To numypix
                x = cx
                y = cy
                For k = 1 To numiter
                   zr = x*x - y*y + cx 
                    zi = 2*x*y + cy                                
                    '-----------> important ------------->
                    If (zr*zr + zi*zi) > 4 Then 'Exit                 
                    x = zr : y = zi
                    End If
                Next           
                cy += dcy                                                          
                '-------------------> problem zone 2 --------------------------------->
                Canvas_SetPixel(xi, j, color_map(numiter+2-k) )
                
                'Canvas_SetPixel(xi, j, color_map(numiter + 1 - k)) ' +1 doesn't work, out of range!            
                'Canvas_SetPixel(xi, j, color_map(numiter+2-k) ) '-> result black painting!            
                'Canvas_SetPixel(x, y, Rgb(color_map(numiter+1), color_map(numiter+1), color_map(numiter+1))) ' testing ok
                'Canvas_SetPixel(x, y, Rgb(0, 255, 0)) ' testing ok
                '-------------------> problem zone 2 --------------------------------->                        
            Next        
            cx += dcx 
        Next                         
            
        Canvas_Color(Rgb(255,10,10))
        Canvas_SetPos(20,420)  
        s = Format$(("Took ##.## seconds ") + Format$(Timer - tmr) )
        'Format$(w)+"x"+Format$(h)+","+Str$(Len(s))+" bytes total"
        Canvas_Print s  
          MsgBox 0, "testing ok?"
          
        Canvas_Redraw
        te = Timer
        Canvas_WaitKey(27)    
      
      Canvas_Window End
     
    End Function
    
    bye, largo
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	math-test-canvas1.jpg 
Views:	72 
Size:	36.9 KB 
ID:	7553  
    Attached Files Attached Files

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,335
    Blog Entries
    2
    Rep Power
    10
    I am by car and I'm posting from iPhone so I cannot test but looking at data I just realize inner loop is 800x600x215=103200000 loops and it can take a while to show results for an interpreted programming language like thinbasic

    I will check better when home
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3
    I am back at home too thanks eros. I am very close to solve the problem zone.

    '-----------> that's the important thingy !------------->                
                    If (zr*zr + zi*zi) > 4 Then Exit                 
                    x = zr : y = zi                                
                    'End If                                                                
                    '-----------> that's the important thingy !------------->
    
    how I can convert

    If (zr*zr + zi*zi) > 4 Then Exit
    
    to thinbasic? "Then Exit" thinbasic doesn't like.

    error message: error code 37:
    EXIT: exit from a non supported block
    then everything will be fine. The black pixel window is ok after my further testings

    I've tested with lower resolution

    numxpix = 400 '800 
        numypix = 300 '600
    
    bye largo and thanks for fast feedback
    Last edited by largo_winch; 25-10-2011 at 18:13.

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,335
    Blog Entries
    2
    Rep Power
    10
    Here again from car an iPhone (quite complex but not impossible)

    In thinbasic EXIT must be followed by the name of the construct you want to exit.
    So: exit for, exit while, Exit function, ...
    See help at http://www.thinbasic.com/public/prod....html?exit.htm

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #5

    close to the solution

    now I am very close to the solution. thanks for the hint with loop/exit eros! There will be some more tests, but perhaps the problem caused a) by missing loops, I am astonished and b) canvas_setpixel formula. I have used "do.. loop.. exit for" for my new example.

    use again at own risk!

    ' Empty GUI script created on 10-25-2011 12:48:25 by largo_winch (ThinAIR)
    
    Uses "console", "ui"
    
    Randomize Timer
    
    Function TBMain () As Long
        Local hwin As DWord
        Local tmr, xmin, xmax, ymin, ymax, cx, cy, dcx, dcy, x, y, zr, zi As Single
        Local xi, j,k, numiter, numxpix, numypix, te, ts As Long
        Local color_map(216) As Long
        Local stuff_done As Long
        Local s As String  
                                                                                     '800,600
        hWin = Canvas_Window ("Canvas_colors_testing - kind of mandelbrot", 50, 50,  400, 300)     
            
        Canvas_Attach hwin, 0, Redraw
        Canvas_Scale Pixels
               
        xmin = -.65 : xmax = -.45 : ymin = .52 : ymax = .72 : numiter = 215
        numxpix = 400 '800 
        numypix = 300 '600 
        dcx = (xmax - xmin)/(numxpix - 1)
        dcy = (ymin - ymax)/(numypix - 1)
    
    Do  
       '-----------------------> 1 --------------------------->
        ReDim color_map(216) '(6*6*6) 216
        For xi = 1 To 5
            For j = 1 To 5
                For k = 1 To 5
                    color_map(xi + 6*j + 36*k) = Rgb(51*j,51*xi,51*k)                                                
                Next
            Next
        Next
        ts  = Timer
        tmr = Timer
        cx  = xmin
        
        '-----------------------> 2 --------------------------->      
        ReDim color_map(216) 
    
        For xi = 1 To numxpix
            cy = ymax
            For j = 1 To numypix
                x = cx
                y = cy            
                For k = 1 To numiter
                   zr = x*x - y*y + cx 
                    zi = 2*x*y + cy                                                
    
                    '-----------> that's the important thingy !------------->                                
                    If (zr*zr + zi*zi) > 4 Then Exit For 'Do 'Exit 
                    x = zr : y = zi                                
         '           Console_WriteLine "FOR/NEXT zr=" + Format$(zr)
         '           Console_WriteLine "FOR/NEXT zi=" + Format$(zi)                
         
                    '-----------> that's the important thingy !------------->                
                Next           
                cy += dcy                                                            
                '-------------------> problem zone nearly solved! --------------------------------->                        
                Canvas_SetPixel(xi, j, Rgb(numiter+2-k, 255*zi, 255*zr)) ' testing ok
                '-------------------> problem zone  --------------------------------->                        
            Next        
            cx += dcx 
        Next                         
    Loop
    
        Canvas_Color(Rgb(255,10,10))
        Canvas_SetPos(20,420)  
          s = Format$(("Took ##.## seconds ") + Format$(Timer - tmr) )
        Canvas_Print s  
          MsgBox 0, "testing ok?" ' example works
              
        Canvas_Redraw
        te = Timer
        Canvas_WaitKey(27)    
      
      Canvas_Window End
     
    End Function
    
    here's the problem zone for clearing better performance

     Canvas_SetPixel(xi, j, Rgb(numiter+2-k, 255*zi, 255*zr)) ' testing ok
    
    only the chaotic background (not linear) is still a problem I will see to solve it for next time.

    pic and tbasic example as attachement.

    bye, largo
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	math-test-better2.jpg 
Views:	65 
Size:	57.9 KB 
ID:	7558  
    Attached Files Attached Files
    Last edited by largo_winch; 25-10-2011 at 19:46. Reason: *.tbasic attachement

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,335
    Blog Entries
    2
    Rep Power
    10
    Dear largo,

    the do/loop is creating and endless loop because there is no condition to exit from it and (as far as I can understand) looping seems having no meaning for the program. Why doing what you have already done?

    Another strange pont is second
    ReDim color_map(216)
    
    You have dimensioned and filled color_map() array just few lines above. If you redim it again you are in practice destroying it and allocating another array.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,335
    Blog Entries
    2
    Rep Power
    10
    Regarding colors, you filled color_map() arra but never used.
    Maybe you would like to change
                Canvas_SetPixel(xi, j, Rgb(color_map(numiter+2-k), 255*zi, 216*zr)) ' testing ok
    
    with something like
                Canvas_SetPixel(xi, j, color_map(numiter+2-k) ) ' testing ok
    
    color_map() already contains a RGB color.

    Just guessing.

    Attached my image after that change
    Ciao
    Eros
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	largo_test.png 
Views:	59 
Size:	66.7 KB 
ID:	7559  
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,335
    Blog Entries
    2
    Rep Power
    10
    Again guessing ...
    The last problem may be related to how you fill your color_map() array. Your code
        ReDim color_map(216) '(6*6*6) 216
        For xi = 1 To 5
            For j = 1 To 5
                For k = 1 To 5
                    color_map(xi + 6*j + 36*k) = Rgb(51*j,51*xi,51*k)                                                
                Next
            Next
        Next
    
    seems filling 5*5*5 (125) buckets while your array seems to be 6*6*6

    Maybe you have to change it with something like that (or something more clever)
        ReDim color_map(216) '(6*6*6) 216
        For xi = 1 To 6
            For j = 1 To 6
                For k = 1 To 6
                    color_map(xi + 6*(j-1) + 36*(k-1)) = Rgb(51*j,51*xi,51*k)                                                
                Next
            Next
        Next
    
    See image after this change
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	largo_test_2.png 
Views:	61 
Size:	67.3 KB 
ID:	7560  
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #9

    working example (I am happy!) :)

    thank you eros, you've shown and focussed for me the dilemma I have had! thinbasic doesn't work with 0 to 5 arrays (1 to 6), then I've doubled used rgb values, therefore I've got these hypnotical backgrounds, but if you like some joints or other people I have created an armada of examples last hour you can be sure they are nice little artworks for (digital) exhibitions

    my son (8 years old) was coming back at late afternoon from school and noticed my examples and wanted to know what's teacher of his school is able to create such beautiful paintings, so we have had a lot of fun together to do more than 20 examples with chaotical structures for background and madelbrot patterns too. thanks for pointing me in right direction! if you like you can manipulate data for different colors and change window size for bigger one.

    1) working examples: (one of hundred possible examples)
    ' Empty GUI script created on 10-25-2011 12:48:25 by largo_winch (ThinAIR)
    
    Uses "console", "ui"
    
    Randomize Timer
    
    Function TBMain () As Long
        Local hwin As DWord
        Local tmr, xmin, xmax, ymin, ymax, cx, cy, dcx, dcy, x, y, zr, zi As Single
        Local xi, j,k, numiter, numxpix, numypix, te, ts As Long
        Local color_map(216) As Long
        Local stuff_done As Long
        Local s As String  
                                                                                  '800,600
        hWin = Canvas_Window ("Canvas_colors_testing 1e_go : mandelbrot", 50, 50, 400, 300)     
            
        Canvas_Attach hwin, 0, Redraw
        Canvas_Scale Pixels
               
        xmin = -.65 : xmax = -.45 : ymin = .52 : ymax = .72 : numiter = 215
        numxpix = 400 '800 
        numypix = 300 '600 
        dcx = (xmax - xmin)/(numxpix - 1)
        dcy = (ymin - ymax)/(numypix - 1)
    
    Do  
       '-----------------------> problem 1 solved array dimension ! --------------------------->
        ReDim color_map(216) '(6*6*6) 216
        For xi = 1 To 6
            For j = 1 To 6
                For k = 1 To 6     'but "6*j + 36*k" doesn't work here (I don't grasp it!)                 
                    color_map(xi + 5*j + 30*k) = Rgb(51*j,51*xi,51*k)                                                
                Next
            Next
        Next
        '-----------------------> problem 1 solved array dimension ! --------------------------->
        ts  = Timer
        tmr = Timer
        cx  = xmin    
        '-----------------------> 2 --------------------------->          
        For xi = 1 To numxpix
            cy = ymax
            For j = 1 To numypix
                x = cx
                y = cy            
                For k = 1 To numiter
                   zr = x*x - y*y + cx 
                    zi = 2*x*y + cy                                                
    
                    '-----------> important thingy solved !-------------> 
                    If (zr*zr + zi*zi) > 4 Then Exit For 
                    x = zr : y = zi                                
                    'Console_WriteLine "FOR/NEXT zr=" + Format$(zr)
                    'Console_WriteLine "FOR/NEXT zi=" + Format$(zi)                
                    '-----------> important thingy solved !------------->                 
                Next           
                cy += dcy                                                            
                '-------------------> problem zone 2 solved! --------------------------------->                                   
                Canvas_SetPixel(xi, j, color_map(numiter+2-k) ) ' testing ok            
                
                '-------------------> problem zone 2 solved --------------------------------->                        
            Next        
            cx += dcx 
        Next                         
        'Canvas_Clear(color_map(numiter+2-k)) 'starts rendering again ;)
            
        Canvas_Color(Rgb(255,10,10))
        Canvas_SetPos(20,120)  
          s = Format$(("Took ##.## seconds ") + Format$(Timer - tmr) )
        Canvas_Print s  
          'MsgBox 0, "testing ok?" ' example works
              
        Canvas_Redraw
    Loop
        te = Timer
        Canvas_WaitKey(27)    
    
      
      Canvas_Window End
     
    End Function
    
    2)
    btw:
                For k = 1 To 6     'but "6*j + 36*k" doesn't work here (I don't grasp it!)                 
                    color_map(xi + 5*j + 30*k) = Rgb(51*j,51*xi,51*k)
    
    3) don't use console printing (I have deactivated these lines) because it decrease heavy rendering speed. and if you like you can "Canvas_Clear(color_map(numiter+2-k)) 'starts rendering again " activate so rendering starts again. all in all that was a real funny work! my son is absolutely happy that all works here and will ask next days his friends how to build such structures with their laptops at math. courses.

    4) example and pic as attachement

    bye, largo
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	canvas-mandelbrot-ok.jpg 
Views:	69 
Size:	44.9 KB 
ID:	7562  
    Attached Files Attached Files
    Last edited by largo_winch; 25-10-2011 at 22:01.

  10. #10
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,335
    Blog Entries
    2
    Rep Power
    10
    Great largo.
    I'm happy I could help.

    Your example is also very useful for me because I can use it as a stress test when sometime I go into thinBasic optimization phase
    Your example is perfect because it involves big loops with some math inside.

    Have fun with your son. I can understand you: it is great moment when you can share some time with your son making things that are fun for both of you.

    Ciao
    Eros

    PS: you can get a little speed improvement not using SINGLE data type but using DOUBLE instead. SINGLE data type is always internally truncated to correct SINGLE "imprecision"
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Page 1 of 2 12 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

Posting Permissions

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