Fractal tree: Difference between revisions

→‎{{header|Haskell}}: Added universal monoidal solution
m (→‎{{header|Sidef}}: minor code simplification)
(→‎{{header|Haskell}}: Added universal monoidal solution)
Line 772:
Use e.g.:
*Main> fractalTree 10
 
----
 
An elegant yet universal monoidal solution using Gloss:
 
<lang haskell>import Graphics.Gloss
 
fractal n model pict = pictures $ take n $ iterate (mconcat model) pict
 
main = animate (InWindow "Tree" (800, 800) (0, 0)) white $ tree1 . (* 60)</lang>
 
The solution gives rise to a variety of fractal geometric structures:
 
static binary tree:
<lang haskell>tree1 _ = fractal 10 branches $ Line [(0,0),(0,100)]
where branches = [ Translate 0 100 . Scale 0.75 0.75 . Rotate 30
, Translate 0 100 . Scale 0.5 0.5 . Rotate (-30) ]</lang>
 
animated tree:
<lang haskell>tree2 t = fractal 8 branches $ Line [(0,0),(0,100)]
where branches = [ Translate 0 100 . Scale 0.75 0.75 . Rotate t
, Translate 0 100 . Scale 0.6 0.6 . Rotate 0
, Translate 0 100 . Scale 0.5 0.5 . Rotate (-2*t) ]</lang>
 
animated fractal clock
<lang haskell>circles t = fractal 10 model $ Circle 100
where model = [ Translate 0 50 . Scale 0.5 0.5 . Rotate t
, Translate 0 (-50) . Scale 0.5 0.5 . Rotate (-2*t) ]</lang>
 
[[Pythagoras tree]]
<lang haskell>pithagor _ = fractal 10 model $ rectangleWire 100 100
where model = [ Translate 50 100 . Scale s s . Rotate 45
, Translate (-50) 100 . Scale s s . Rotate (-45)]
s = 1/sqrt 2</lang>
 
[[Sierpinski pentagon]]
<lang haskell>pentaflake _ = fractal 5 model $ pentagon
where model = map copy [0,72..288]
copy a = Scale s s . Rotate a . Translate 0 x
pentagon = Line [ (sin a, cos a) | a <- [0,2*pi/5..2*pi] ]
x = 2*cos(pi/5)
s = 1/(1+x)</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user