May 22, 2024, 05:49:47 AM


Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........

Tree of Pythagoras

Started by GWS, October 16, 2011, 10:07:12 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.



Here's a Creative version of an interesting recursion program I found on the web, with a few tweaks of my own ..   :P

I've never really understood recursion - I always expect it to blow up the computer ..   ;D

' Creative - Tree of Pythagorus
' GWS Oct 2011
' a recursion example ..

def w:window
def wstyle,i:int
def wW,wH,c:int

declare pythTree(ax:int, ay:int, bx:int, by:int, recur:int)

wW = 1024
wH = 768

wstyle = @minbox

window w,-wW,0,wW,wH,wstyle,0,"Creative Basic",messages
setwindowcolor w,rgb(0,0,30)
centerwindow w

control w,"B,Exit,(wW-60)/2+50,wH*0.88,60,25,0,1"
control w,"B,Redo,(wW-60)/2-60,wH*0.88,60,25,0,2"

pythtree((wW/2-wW/12-5), wH-50, (wW/2+wW/12-5), wH-50, 0)

run = 1

waituntil run = 0
closewindow w

SUB messages
select @class
case @idclosewindow
run = 0
case @idcontrol
select @controlID
case 1
run = 0
case 2
setwindowcolor w,rgb(0,0,30)
pythtree((wW/2-wW/12-5), wH-50, (wW/2+wW/12-5), wH-50, 0)

sub pythTree(ax, ay, bx, by, recur)
def red,grn,blu:int
cx = ax-ay+by
cy = ax+ay-bx
dx = bx+by-ay
dy = ax-bx+by
ex = 0.5*(cx-cy+dx+dy)
ey = 0.5*(cx+cy-dx+dy)

red = rnd(200)+55: grn = rnd(100)+155 :blu = rnd(200)+55
frontpen w,rgb(red,grn,blu)

Line w, cx, cy, ax, ay
Line w, ax, ay, bx, by
Line w, bx, by, dx, dy
Line w, dx, dy, cx, cy
Line w, cx, cy, ex, ey
Line w, ex, ey, dx, dy

if recur < 24
    pythTree(cx, cy, ex, ey, recur+rnd(4)+1)
    pythTree(ex, ey, dx, dy, recur+rnd(4)+1)

best wishes, :)

Tomorrow may be too late ..



Is there a way to made this program without recursive calling...


Hi Aurel,

Good question .. and I dunno  ::)

I'm no expert, but I believe an alternative method would involve iteration.

I took a look around, and found this ..

.. but I haven't tried it.

Fractals are amazing things ..

I might pursue a few more of these if I get time ..  ;D

all the best, :)

Tomorrow may be too late ..



I noticed someone on the Forum looking at GWS' code for a Tree of Pythagoras. It was in Creative Basic, so I have converted it to IWB for anyone that is interested