Results 1 to 10 of 14

Thread: Tree Recursion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •