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
    56

    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
    57
    Posts
    8,777
    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 Images Attached Images
    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

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    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 Images Attached Images
    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

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

    Cool Trees! Well done.

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    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 Images Attached Images
    Last edited by ErosOlmi; 01-04-2014 at 14:32.
    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

  6. #6
    Member
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    406
    Rep Power
    56
    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 Images Attached Images
    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
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    maybe it means he can now rent a very small apartment since he doesn't need too much space for houseplants?

    I think there are missing some Forum-sections as beta-testing and support

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

Members who have read this thread: 1

Posting Permissions

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