Sierpinski pentagon: Difference between revisions

Content deleted Content added
Line 14: Line 14:
pentaflake order = iterate transformation pentagon !! order
pentaflake order = iterate transformation pentagon !! order
where
where
transformation = Scale s s . foldMap copy [0..4]
transformation = Scale s s . foldMap copy [0,72..288]
copy i = Rotate (72*i) . Translate 0 x
copy a = Rotate a . Translate 0 x
pentagon = Polygon [ (sin a, cos a) | a <- [0,2*pi/5..2*pi] ]
pentagon = Polygon [ (sin a, cos a) | a <- [0,2*pi/5..2*pi] ]
x = 2*cos(pi/5)
x = 2*cos(pi/5)
Line 23: Line 23:
where dc = InWindow "Pentaflake" (400, 400) (0, 0)</lang>
where dc = InWindow "Pentaflake" (400, 400) (0, 0)</lang>


'''Explanation''': Since <tt>Picture</tt> forms a monoid with image overlaying as multiplication, so do functions having type <tt>Picture -> Picture</tt>:
See also implementation using [http://projects.haskell.org/diagrams/gallery/Pentaflake.html Diagrams]

f,g :: Picture -> Picture
f <> g = \p -> f p <> g p

Function <code>copy</code> for an angle returns transformation, which shifts and rotates given picture, therefore <code>foldMap copy</code> for a list of angles returns a transformation, which shifts and rotates initial image five times. After that the resulting image is scaled to fit the inital size, so that it is ready for next iteration.

If one wants to get all intermediate pentaflakes <code>transformation</code> shoud be changed as follows:
<lang haskell>transformation = Scale s s . (Rotate 36 <> foldMap copy [0,72..288])</lang>

See also the implementation using [http://projects.haskell.org/diagrams/gallery/Pentaflake.html Diagrams]


=={{header|Java}}==
=={{header|Java}}==