aUCBLogo Demos and Tests / menger3
to menger3 ;will draw a 3D Menger sponge fractal
make "max_ 300 ;max_. side length of the sponge
make "min_ max_/3/3/3 ;min_. side length
; make "min_ max_/3/3/3/3/3 ;try this, if you have patience
make "cf 0.5/(max_/2) ;color factor
clearScreen
hideTurtle ;for speeding it up
setScreenColor 0
perspective
penup
sponge max_ 0 0 0 ;draw the sponge without saving anything
(rotateScene 2)
erase [[menger3 sponge cube square][min_ max_ cf][]]
end
to sponge length x y z
ifelse length > min_
[
local [i_x i_y i_z l]
repeat 3
[ make "i_x repcount-2
repeat 3
[ make "i_y repcount-2
repeat 3
[ make "i_z repcount-2
if not (or
(and i_x==0 i_y==0)
(and i_y==0 i_z==0)
(and i_z==0 i_x==0) )
[ make "l length/3
sponge l x+i_x*l y+i_y*l z+i_z*l
]
]
]
]
]~
[
cube (list x y z) min_
]
end
to cube s l
setPenColor rgb
0.5+(first s)*cf
0.5+(first bf s)*cf
0.5+(last s)*cf
setPosXYZ s
repeat 4
[ square l
down 90
fd l
up 90
leftRoll 90
]
down 90
square l
up 90
fd l
down 90
square l
up 90
back l
home
end
to square l
pd
PolyStart
repeat 4 [fd l rt 90]
PolyEnd
pu
end