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

Thread: Tree Recursion

  1. #1
    Member
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    406
    Rep Power
    50

    Tree Recursion

    Hi Eros,

    There is still another problem with recursion.
    It seems that Mathematics in Thinbasic is a little bit differently.

    This recursion tree only shows one branch.
    You can compare with oxygenbasic tree, to see the difference.
    Uses "ui", "math"
    #INCLUDE "abc.inc"
    
    OpenWindow 600, 500
    SetHandleDC hdc, hwnd
    Canvas_Clear &HFFFFFF
    
    Dim x2, y2, depth As Long
    depth = 9
    
    Sub MainDraw(x1, y1 As Long, angle As Single, d As Long) 
        If d >0 Then
           x2 = x1 + Cos(DegToRad(angle)) * d * 10
           y2 = y1 + Sin(DegToRad(angle)) * d * 10
           DrawLine x1, y1, x2, y2, d, Rgb(255-d*10, 100-d, 70)
           MainDraw(x2, y2, angle - 20, d-1)
           MainDraw(x2, y2, angle + 20, d-1)
        End If
    End Sub    
    
    MainDraw(300, 460, -90, depth)
    
    Canvas_Redraw
    Canvas_WaitKey
    Canvas_Window End hwnd
    
    OxygenBasic code:
    #include "sw.inc"
    Window 600,500,1
    
    sys depth=9
    
    Sub Draw(sys x1, y1, single angle, sys depth) 
        iF depth > 0
           x2 = x1 + cos(rad(angle)) * depth * 10
           y2 = y1 + sin(rad(angle)) * depth * 10
           Line x1, y1, x2, y2, depth, RGB(255-depth*10, 100-depth, 70)
           Draw(x2, y2, angle - 20, depth - 1)
           Draw(x2, y2, angle + 20, depth - 1)
        End iF
    End Sub    
    
    Draw(300, 460, -90, depth)
    
    WaitKey
    CloseWindow
    
    Attached Files Attached Files
    Last edited by peter; 30-01-2015 at 17:11.

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,133
    Blog Entries
    2
    Rep Power
    10
    Ciao Peter,

    sorry for the delay but I was on a mission
    Please define X2 and Y2 as Local inside MainDraw function and all should work as expected.
    Uses "ui", "math"#INCLUDE "abc.inc"
     
    OpenWindow 600, 500
    SetHandleDC hdc, hwnd
    Canvas_Clear &HFFFFFF
    
    
    Dim depth As Long = 9
     
    Sub MainDraw(x1, y1 As Long, angle As Single, d As Long)
      Dim x2, y2 As Long
      
      If d >0 Then
        x2 = x1 + Cos(DegToRad(angle)) * d * 10
        y2 = y1 + Sin(DegToRad(angle)) * d * 10
        DrawLine x1, y1, x2, y2, d, Rgb(255-d*10, 100-d, 70)
        MainDraw(x2, y2, angle - 20, d-1)
        MainDraw(x2, y2, angle + 20, d-1)
      End If
    End Sub   
     
    MainDraw(300, 460, -90, depth)
     
    Canvas_Redraw
    Canvas_WaitKey
    Canvas_Window End hwnd
    
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Capture.PNG 
Views:	81 
Size:	54.4 KB 
ID:	8973  
    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
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,133
    Blog Entries
    2
    Rep Power
    10
    A little bit of random variations.

    Uses "ui", "math"#INCLUDE "abc.inc"
     
    OpenWindow 600, 500
    SetHandleDC hdc, hwnd
    
    
    Dim depth As Long
    
    
    Sub MainDraw(x1, y1 As Long, angle As Single, d As Long)
      Dim x2, y2 As Long
     
      If d > 0 Then
        x2 = x1 + Cos(DegToRad(angle)) * d * Rnd(5, 12)
        y2 = y1 + Sin(DegToRad(angle)) * d * Rnd(5, 12)
    
    
        DrawLine x1, y1, x2, y2, d, Rgb(255-d*10, 100-d, 70)
        MainDraw(x2, y2, angle - 20, d-1)
        MainDraw(x2, y2, angle + 20, d-1)
      End If
    End Sub   
    
    
    Randomize
    Do
      depth = Rnd(8, 10)
      Canvas_Clear &HFFFFFF
      MainDraw(300, 460, -90, depth)
      Canvas_Redraw
    Loop While Asc(Canvas_WaitKey) <> 27
    
    
    Canvas_Window End hwnd
    
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Capture_01.PNG 
Views:	79 
Size:	45.8 KB 
ID:	8974   Click image for larger version. 

Name:	Capture_02.PNG 
Views:	77 
Size:	50.9 KB 
ID:	8975   Click image for larger version. 

Name:	Capture_03.PNG 
Views:	81 
Size:	58.7 KB 
ID:	8976   Click image for larger version. 

Name:	Capture_04.PNG 
Views:	79 
Size:	60.1 KB 
ID:	8977  
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #4
    Member
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    406
    Rep Power
    50
    Thank you Eros,

    Cool Trees! Well done.

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,133
    Blog Entries
    2
    Rep Power
    10
    This evening I was really tired after a hard day at work
    I just wanted a little bit of ... nature.
    It always surprise me how few lines of code are able to create such natural figures.
    Nature is math and math is nature.

    Uses "ui", "math"
    #INCLUDE "abc.inc"
     
    OpenWindow 1024, 768
    SetHandleDC hdc, hwnd
    
    
    Dim depth As Long
    
    
    Sub MainDraw(x1, y1 As Long, angle As Single, d As Long)
      Dim x2, y2 As Long
     
      If d > 0 Then
        x2 = x1 + Cos(DegToRad(angle)) * d * Rnd(2, 12)
        y2 = y1 + Sin(DegToRad(angle)) * d * Rnd(2, 12)
    
    
        DrawLine x1, y1, x2, y2, d, Rgb(d*10, 255-d*Rnd(20, 25), 0)
        MainDraw(x2, y2, angle - Rnd(13, 18), d-1)
        MainDraw(x2, y2, angle + Rnd(13, 18), d-1)
      End If
    End Sub   
    
    
    Dim Counter As Long
    Randomize
    Do
      depth = Rnd(6, 12)
      '---Clear screen every 50 trees
      If Mod(Counter, 50) = 0 Or Counter = 0 Then Canvas_Clear Rgb(0, 230, 255)  
    
    
      MainDraw(Rnd(100, 985), 768, Rnd(-85, -95), depth)
      Canvas_Redraw
    
    
      Incr Counter
    Loop While (Asc(Canvas_WaitKey) <> 27) And IsWindow(hwnd)
    
    
    Canvas_Window End hwnd
    
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Nature.jpg 
Views:	109 
Size:	196.3 KB 
ID:	8986  
    Last edited by ErosOlmi; 01-04-2014 at 14:32.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  6. #6
    Member
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    406
    Rep Power
    50
    Hi Eros,

    Looks good this nature. Better than the original nature!
    There are no cars, no people, is a quiet and peaceful nature forest.

  7. #7
    Hi Eros,

    A beautiful example how nature regenerates itself in a recursive way.
    (and generating "natural images ;-)

    I'm somewhat back to Lisp for the moment, but .. (while it is a byte code compiler with native JIT , it lacks the speed of ThinBasic + O2 ) , so I use Thinbasic for writing the "glue-code" and O2 to rewrite time critical things ... somewhat a miracle this works , because Lisp uses exact and inexact numbers ( in Lisp p.e. something as 1/7 is an exact number because it stores it as a rational number (both the 1 and the 7 in this case and knows how to process such numbers ).
    As you mention few code is needed (I attached an image, and this is all the code : )

    --------------------------------------

    (require ffi/unsafe)


    (define o2toR (ffi-lib "o2toRJulia")) ; get a handle for the C library (well, oxygen of course )

    (define orbO2 (get-ffi-obj 'orbO2 corb (_fun _float _float _float _float _int -> _float )
    (lambda () orbO2 )))


    (define zijde 1200)
    (define target (make-bitmap zijde zijde))
    (define dc (new bitmap-dc% [bitmap target]))

    (send dc set-background "DarkOliveGreen")
    (send dc clear)





    (define-values (cr ci info x y xo yo dz ro ro2 ) (values 0.143 0.6 '() 0 0 0 0 0 0 0))

    (define (make-int n)
    (inexact->exact (round (* (sqrt n) 7 )))) ; ----------------------------- parabolic gradient conversion of inexact numbers !!!




    (define (draw-julia it)
    (set! xo -1.5)
    (set! yo -1.5)
    (set! dz (/ 3 zijde))
    (for ( [ i (range 0 zijde) ] )
    (for ( [ j (range 0 zijde) ] )
    (set! x (+ xo (* i dz)))
    (set! y (+ yo (* j dz)))
    (set! ro (orbO2 x y cr ci it))
    (if (> 250 ro)
    (send dc set-pixel i j (make-color 0 (make-int ro) 0 ))
    #f)
    )))



    (define (init)
    (displayln "System Ready")

    (draw-julia 5

    (display "Image generated as JuliaO2.png ")
    (send target save-file "JuliaO2.png" 'png)

    )

    (init)

    ---------------------------------------------------------------------------

    & the ThinBasic script to generate the dll :


    -----------------------------------------------------------

    Uses "oxygen"

    Dim o2 As String




    O2=
    "
    % filename "o2toRJulia.dll"
    % dll
    include "RTL32.inc"


    dim dup , orb as single
    dim i as long


    function corb (byval x as single , byval y as single ,byval cr as single ,
    byval ci as single , byval it as integer ) as single export

    for i=1 to it
    dup=x
    x=x*x-y*y+cr
    y=2*dup*y+ci
    orb=abs(x*y)
    if orb > 3 then
    exit for
    endif
    next
    return orb
    end function



    "



    O2_Basic o2
    If O2_Errno Then
    MsgBox 0, O2_Error
    Stop
    Else
    O2_Exec
    End If

    MsgBox ,"DLL created"

    -----------------------------------------------------------


    result
    below ;-) (oops, with a serious degradation )


    best Rob
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	JuliaO2 - Lisp.jpg 
Views:	105 
Size:	78.5 KB 
ID:	8987  
    Last edited by RobbeK; 02-04-2014 at 01:47.

  8. #8
    Hey, I'd like to thank you for all the information you've given. I'm looking for an apartment and this is helping me a lot!

  9. #9
    I'm looking for an apartment and this is helping me a lot!
    I would be willing to bet that this new member is a bot.
    Last edited by John Spikowski; 08-11-2014 at 03:05.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  10. #10
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Philippines
    Age
    47
    Posts
    1,218
    Blog Entries
    1
    Rep Power
    132
    maybe it means he can now rent a very small apartment since he doesn't need too much space for houseplants?

    [TABLE="class: tblf1 tblf-fullwidth tblf-alternate"]
    [TR]
    [TD][COLOR=#b22222][B][SIZE=1]The limit is[/SIZE][/B][/COLOR][/TD]
    [TD="class: isRelinked"] [COLOR=#0000cd][B][SIZE=1]Imagination[/SIZE][/B][/COLOR][/TD]
    [/TR]
    [/TABLE]

Page 1 of 2 12 LastLast

Similar Threads

  1. A nice (verbal) example of recursion
    By RobbeK in forum General
    Replies: 2
    Last Post: 25-03-2014, 02:05
  2. Replies: 1
    Last Post: 22-12-2010, 22:41
  3. Various types of recursion
    By Petr Schreiber in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 6
    Last Post: 25-05-2010, 06:38
  4. Expanded tree
    By kryton9 in forum thinAir General
    Replies: 1
    Last Post: 21-02-2008, 22:44
  5. tree view?
    By sandyrepope in forum UI (User Interface)
    Replies: 3
    Last Post: 16-07-2007, 18:18

Posting Permissions

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