Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tree
#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


Messages In This Thread
Tree - by 1micha.elok - 03-04-2025, 09:36 AM
RE: Tree - by johnno56 - 03-04-2025, 10:29 AM
RE: Tree - by indru91112 - 03-04-2025, 01:47 PM
RE: Tree - by aurel - 03-05-2025, 02:07 PM
RE: Tree - by Marcus - 03-06-2025, 04:04 PM
RE: Tree - by 1micha.elok - 03-07-2025, 03:22 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)