Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tree
#1
           
click the image to zoom in

Code:
'---------------------------------------------------------------------
'Tree V2
'
'Steps :
'1.Draw the trunk
'2.At the end of the trunk, split by some angle and draw some branches
'3.Repeat at the end of each branch
'
'REFERENCE
'https://rosettacode.org/wiki/Fractal_tree
'---------------------------------------------------------------------

#win32
set window "Tree V2",600*screenw()/screenh(),600,false
set redraw off

'color definition
green = [0,255,0]
black = [0,0,0]


'---------------
' MAIN PROGRAM
'---------------
counter = 0
do
    'clear screen
    set color black ; cls
    set color green
   
    'Tree(x1, y1, angle, level,enlarge)
    Tree(width()/2, height()-50, 270, 7,18)
   
    set caret 10,10
    wln "Info Box"
    wln "========================"
    wln "Press RETURN to continue"
    wln "Press ESCAPE to exit"
    wln
    wln counter
    counter = counter + 1
     
    redraw
   
    'pause
    do;wait 1;if keydown(KEY_ESCAPE,true) end;until keydown(KEY_RETURN,true)
loop


'-----------
' FUNCTIONS
'-----------
function Tree(x1, y1, angle, level,enlarge)   
    if level > 0 then
        x2 = x1 + cos(rad(angle)) * level * enlarge + rnd(3,4)
        y2 = y1 + sin(rad(angle)) * level * enlarge + rnd(3,4)
       
        'draw trunk/branch
        trunk(x1,y1,x2,y2,level)
       
        'split branch
        if rnd(0,1) then Tree(x2, y2, angle - rnd(15,48), level - 1,enlarge)
        Tree(x2, y2, angle + rnd(15,48), level - 1,enlarge)
        if rnd(0,1) then Tree(x2, y2, angle, level-1, enlarge)
        if rnd(0,1) then Tree(x2, y2, angle + rnd(10,30), level-1, enlarge)
    endif
endfunc


function trunk(x1,y1,x2,y2,t)
    'reduce thickness
    t = t/1.1

    for x = x1 to x2 step 0.01
        'equation of a line passing through 2 points
        y = (y2-y1)/(x2-x1)*(x-x1)+y1
        draw ellipse x,y,t,t,true                         
    next
endfunc
Reply
#2
Cool... The classic Fractal Tree!! The classics never die! Great choice!
Logic is the beginning of wisdom.
Reply
#3
(03-04-2025, 09:36 AM)1micha.elok Wrote: click the image to zoom in

Code:
'---------------------------------------------------------------------
'Tree V2
'
'Steps :
'1.Draw the trunk
'2.At the end of the trunk, split by some angle and draw some branches
'3.Repeat at the end of each branch
'
'REFERENCE
'https://rosettacode.org/wiki/Fractal_tree
'---------------------------------------------------------------------

#win32
set window "Tree V2",600*screenw()/screenh(),600,false
set redraw off

'color definition
green = [0,255,0]
black = [0,0,0]


'---------------
' MAIN PROGRAM
'---------------
counter = 0
do
    'clear screen
    set color black ; cls
    set color green
   
    'Tree(x1, y1, angle, level,enlarge)
    Tree(width()/2, height()-50, 270, 7,18)
   
    set caret 10,10
    wln "Info Box"
    wln "========================"
    wln "Press RETURN to continue"
    wln "Press ESCAPE to exit"
    wln
    wln counter
    counter = counter + 1
     
    redraw
   
    'pause
    do;wait 1;if keydown(KEY_ESCAPE,true) end;until keydown(KEY_RETURN,true)
loop


'-----------
' FUNCTIONS
'-----------
function Tree(x1, y1, angle, level,enlarge)   
    if level > 0 then
        x2 = x1 + cos(rad(angle)) * level * enlarge + rnd(3,4)
        y2 = y1 + sin(rad(angle)) * level * enlarge + rnd(3,4)
       
        'draw trunk/branch
        trunk(x1,y1,x2,y2,level)
       
        'split branch
        if rnd(0,1) then Tree(x2, y2, angle - rnd(15,48), level - 1,enlarge)
        Tree(x2, y2, angle + rnd(15,48), level - 1,enlarge)
        if rnd(0,1) then Tree(x2, y2, angle, level-1, enlarge)
        if rnd(0,1) then Tree(x2, y2, angle + rnd(10,30), level-1, enlarge)
    endif
endfunc


function trunk(x1,y1,x2,y2,t)
    'reduce thickness
    t = t/1.1

    for x = x1 to x2 step 0.01
        'equation of a line passing through 2 points
        y = (y2-y1)/(x2-x1)*(x-x1)+y1
        draw ellipse x,y,t,t,true                         
    next
endfunc

Fractals ❤️
Don’t Count Your Chickens Before They Hatch
Reply
#4
well fractals yes ..but program work way to slow
Reply
#5
(03-05-2025, 02:07 PM)aurel Wrote: well fractals yes ..but program work way to slow

There was some unnecessary drawing going on in the trunk function. I took the liberty and fixed it a bit Smile  It's better to use the parametric line equation when doing things like this.


Code:
'---------------------------------------------------------------------
'Tree V2
'
'Steps :
'1.Draw the trunk
'2.At the end of the trunk, split by some angle and draw some branches
'3.Repeat at the end of each branch
'
'REFERENCE
'https://rosettacode.org/wiki/Fractal_tree
'---------------------------------------------------------------------

#win32
set window "Tree V2",600*screenw()/screenh(),600,false
set redraw off

'color definition
green = [0,255,0]
black = [0,0,0]


'---------------
' MAIN PROGRAM
'---------------
counter = 0
do
    'clear screen
    set color black ; cls
    set color green
 
    'Tree(x1, y1, angle, level,enlarge)
    Tree(width()/2, height()-50, 270, 7,18)
 
    set caret 10,10
    wln "Info Box"
    wln "========================"
    wln "Press RETURN to continue"
    wln "Press ESCAPE to exit"
    wln
    wln counter
    counter = counter + 1
   
    redraw
 
    'pause
    do;wait 1;if keydown(KEY_ESCAPE,true) end;until keydown(KEY_RETURN,true)
loop


'-----------
' FUNCTIONS
'-----------
function Tree(x1, y1, angle, level,enlarge) 
    if level > 0 then
        x2 = x1 + cos(rad(angle)) * level * enlarge + rnd(3,4)
        y2 = y1 + sin(rad(angle)) * level * enlarge + rnd(3,4)
     
        'draw trunk/branch
        trunk(x1,y1,x2,y2,level)
     
        'split branch
        if rnd(0,1) then Tree(x2, y2, angle - rnd(15,48), level - 1,enlarge)
        Tree(x2, y2, angle + rnd(15,48), level - 1,enlarge)
        if rnd(0,1) then Tree(x2, y2, angle, level-1, enlarge)
        if rnd(0,1) then Tree(x2, y2, angle + rnd(10,30), level-1, enlarge)
    endif
endfunc


function trunk(x1,y1,x2,y2,t)
    'reduce thickness
    t = t/1.1

    ' marcus.
    'for x = x1 to x2 step 0.01
    '    'equation of a line passing through 2 points
    '    y = (y2-y1)/(x2-x1)*(x-x1)+y1
    '    draw ellipse x,y,t,t,true                       
    'next
    dx = x2 - x1; dy = y2 - y1
    d = sqr(dx*dx + dy*dy)
    dx = dx/d; dy = dy/d
    for i = 1 to d
        draw ellipse x1, y1, t, t, true
        x1 = x1 + dx; y1 = y1 + dy
    next
endfunc
Reply
#6
(03-06-2025, 04:04 PM)Marcus Wrote: ... I took the liberty and fixed it a bit Smile  It's better to use the parametric line equation when doing things like this.

Code:
...
function trunk(x1,y1,x2,y2,t)
    'reduce thickness
    t = t/1.1

    ' marcus.
    dx = x2 - x1; dy = y2 - y1
    d = sqr(dx*dx + dy*dy)
    dx = dx/d; dy = dy/d
    for i = 1 to d
        draw ellipse x1, y1, t, t, true
        x1 = x1 + dx; y1 = y1 + dy
    next
endfunc
...

Awesome. Thank you, Marcus  Big Grin

Parametric equation in calculus shine
With slopes and integrals, neatly defined
In coding, they help with performance so tight
Solving equations with the speed of light
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)