-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFernImage.hs
27 lines (26 loc) · 1.36 KB
/
FernImage.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module FernImage where
import qualified Data.Set as Set
import Codec.Picture
import Ferns (Fern, genWeightedTransforms)
import Turtle (TurtleState, TurtleConfig, moveTurtle, turtleToPixels)
-- Create a fern image given an initial turtle state, a turtle config, a
-- fern, a desired number of iterations, an image width and height in pixels,
-- and a background and fern colour
fernImage :: TurtleState -> TurtleConfig -> Fern -> Int
-> (Int, Int) -> (PixelRGBA8, PixelRGBA8) -> Image PixelRGBA8
fernImage turtle turtleConfig fern iterations
(imageWidth, imageHeight) (backgroundColour, fernColour) =
generateImage getColour imageWidth imageHeight
where
-- Create our list of transforms to pick from uniformly at random
transforms = genWeightedTransforms fern 100
--- Create a list of all the turtle states ever
turtleStates = iterate (moveTurtle transforms) turtle
--- Create a set of pixels the turtle visited within our iterations
visitedPixels = Set.fromList
$ take iterations
$ map (turtleToPixels turtleConfig) turtleStates
-- Create a function that maps an xy pixel coord to a colour
getColour x y = if Set.member (x, y) visitedPixels
then fernColour
else backgroundColour