aUCBLogo Demos and Tests / ifs
to IFS ;I_terative F_unction S_ystem
maxpa=1000
dragon
; LevyDragon
; SierpinskiTriangle
; fern
; hexagon
; tree
; leaf
demos=
[ koch2
smiley
am2
tm30
feather
oriental
quadrat
quadrat2
rosette
rosette2
star
star1
star3
star4
t
t1
t6
t8
universe
]
pr [+ - chooses demo, ESC stops]
i=1;
forever
[ demo i
ch=readChar
if ch==char 27 [stop]
if ch=="+ [if i<count demos [i=i+1]]
if ch=="- [if i>1 [i=i-1]]
]
end
to demo nr
IFSLoad word demos.nr ".ifs
(print word nr ") demos.nr)
initProbabilites
initGraph
IFSLoop
end
to initProbabilites
pa=Array maxpa
k=1
for [i 1 n]
[ for [j k p.i]
[
;(show j i)
pa.j=i
]
k=p.i
]
;show p
;show pa
end
to IFSLoop
until [key?]
[ IFSiterate
]
end
to init
n=count p
p0=0
for [i 1 n]
[ p.i=p0+p.i
p0=p.i
]
p=p*maxpa/(max p)
initProbabilites
initGraph
IFSLoop
end
to dragon
p={1 1}
d= { { 0.5 -0.5 0.5 0.5 0 0 0.5}
{-0.5 -0.5 0.5 -0.5 1 0 0.5}
}
sk=300
x0=0
y0=0
init
end
to LevyDragon
p={1 1}
d= { {0.5 -0.5 0.5 0.5 0 0 }
{0.5 0.5 -0.5 0.5 0.5 0.5}
}
sk=350
x0=-170
y0=-120
init
end
to SierpinskiTriangle
p={1 1 1}
d= { {0.5 0 0 0.5 0 0 }
{0.5 0 0 0.5 0.5 0 }
{0.5 0 0 0.5 0.25 0.433}
}
sk=600
x0=-300
y0=-300*(sqrt 3)/2
init
end
to fern
p={10 70 70 850}
d= { { 0.00 0.00 0.00 0.16 0.00 0.00}
{ 0.20 -0.26 0.23 0.22 0.00 1.60}
{-0.15 0.28 0.26 0.24 0.00 0.44}
{ 0.85 0.04 -0.04 0.85 0.00 1.60}
}
sk=50
x0=0
y0=-250
init
end
to hexagon
p={200 100 100 200 1000}
d= { {0.333333 0 0 0.333333 -0.333333 0}
{0.166667 -0.288675 0.288675 0.166667 -0.0833333 0.144337}
{0.166666 0.288675 -0.288675 0.166666 0.0833333 0.144337}
{0.333333 0 0 0.333333 0.333333 0}
{0.506667 -0.859357 0.859357 0.506667 0.25 -0.433012}
}
sk=300
x0=-150
y0=0
init
end
to tree
p={400 100 1000 1000}
d= { {0 0 0 0.5 0 0}
{0.1 0 0 0.1 0 0.2}
{0.42 -0.42 0.42 0.42 0 0.2}
{0.42 0.42 -0.42 0.42 0 0.2}
}
sk=1200
x0=0
y0=-300
init
end
to leaf
p={150 150 150 500}
d= { {0.95 -0.1 0.1 0.95 0.45 0.075}
{0.35 -0.5 0.5 0.35 0 0.45}
{0.55 0.43 -0.43 0.55 0 0.44}
{0.85 0.05 -0.05 0.85 0 1.6}
}
sk=40
x0=0
y0=-200
init
end
to readNumber
local "w
w=readWord
if (first w)=="
[ w=bf w
]
output w+0
end
to ifsLoad filename
openRead filename
setReader filename
n=readNumber+1
p=array n
d=array n
for [i 1 n]
[ di=array 6
for [j 1 6]
[ di.j=readNumber
]
d.i=di
p.i=readNumber
]
sk=readNumber
x0=readNumber-400
y0=300-readNumber
setReader []
close filename
p=round p*maxpa/(max p)
(pr "p= p)
(pr "d= d)
(pr "sk= sk)
(pr "x0= x0)
(pr "y0= y0)
end
to IFSLoad2 filename
openRead filename
setReader filename
repeat 3 [ignore readWord]
x=readList
y=readList
sk=600*(x.2-x.1)
x0=x.1+x.2
y0=y.1+y.2
ignore readWord
n=readNumber
repeat 2 [ignore readWord]
d=array n
for [i 1 n]
[ l=readList
d.i=listToArray (list l.1 l.4 l.3 l.2 l.5 l.6)
]
repeat 2 [ignore readWord]
p=listToArray readList
setReader []
close filename
init
(pr "p= p)
(pr "d= d)
(pr "sk= sk)
(pr "x0= x0)
(pr "y0= y0)
end
to initGraph
x=0
y=0
cs ht
; noRefresh
end
to IFSiterate
repeat 1000
[ z=1+random maxpa
dk=d.(pa.z)
xx= dk.1*x + dk.2*y + dk.5
yy= dk.3*x + dk.4*y + dk.6
xi= x0+xx*sk
yi= y0+yy*sk
; if (xi >-400) and2 (xi < 400)
; and2 (yi >-300) and2 (yi < 300)
; [
setPixel (list xi yi) pa.z
x=xx
y=yy
; ]
]
updateGraph
end