November 29, 2021, 11:19:30 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


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.

GWS

Hi,

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
END

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)
endselect
endselect
return

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)
endif
return



best wishes, :)

Graham
Tomorrow may be too late ..

aurelCB

forum for hobby programming:<br />https://aurelsoft.ucoz.com

aurelCB

Is there a way to made this program without recursive calling...
forum for hobby programming:<br />https://aurelsoft.ucoz.com

GWS

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 ..

http://codeunivers.com/source-codes/matlab/pythagoras_tree

.. but I haven't tried it.

Fractals are amazing things ..

http://www.miqel.com/fractals_math_patterns/visual-math-iterative-fractals.html

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

all the best, :)

Graham
Tomorrow may be too late ..

Brian

Hello,

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

Brian