Posts: 275
Threads: 35
Joined: Nov 2023
Reputation:
0
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
Posts: 409
Threads: 47
Joined: Nov 2023
Reputation:
3
Cool... The classic Fractal Tree!! The classics never die! Great choice!
Logic is the beginning of wisdom.
Posts: 6
Threads: 0
Joined: Mar 2024
Reputation:
0
(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
Posts: 44
Threads: 3
Joined: Feb 2024
Reputation:
2
well fractals yes ..but program work way to slow
Posts: 405
Threads: 53
Joined: Nov 2023
Reputation:
3
03-06-2025, 04:04 PM
(This post was last modified: 03-06-2025, 04:59 PM by Marcus.)
(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  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
Posts: 275
Threads: 35
Joined: Nov 2023
Reputation:
0
(03-06-2025, 04:04 PM)Marcus Wrote: ... I took the liberty and fixed it a bit 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
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
|