How to resolve the algorithm Barnsley fern step by step in the Haskell programming language
How to resolve the algorithm Barnsley fern step by step in the Haskell programming language
Table of Contents
Problem Statement
A Barnsley fern is a fractal named after British mathematician Michael Barnsley and can be created using an iterated function system (IFS).
Create this fractal fern, using the following transformations: Starting position: x = 0, y = 0
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Barnsley fern step by step in the Haskell programming language
This Haskell code generates a Barnsley fern, a fractal pattern that resembles a plant. It uses a combination of random numbers and affine transformations to create the fern's characteristic shape.
The code begins by importing several modules:
Data.List
for thescanl'
function, which is used to accumulate values in a list.Diagrams.Backend.Rasterific.CmdLine
for themainWith
function, which is used to generate a PNG image of the fern.Diagrams.Prelude
for theframe
anddiagramFrom
functions, which are used to create a Diagram object.System.Random
for thegetStdGen
andrandomRs
functions, which are used to generate random numbers.
The code then defines four affine transformations, f1
, f2
, f3
, and f4
, which are used to transform points on the plane. These transformations are used to create the fern's characteristic shape.
The func
function takes a point and a random number as input and returns a new point that has been transformed by one of the four affine transformations. The function fern
takes a list of random numbers as input and returns a list of points that have been transformed by the func
function.
The drawFern
function takes a list of random numbers and a count as input and returns a Diagram object that represents the fern. The function diagramFrom
takes a list of points and a function that draws a point as input and returns a Diagram object that represents the points. The function dot
is used to draw a point as a small circle.
The main
function generates a list of random numbers and a count and then calls the drawFern
function to create a Diagram object. The mainWith
function is then used to generate a PNG image of the fern.
Here is an example of how to use the code to generate a PNG image of a fern:
fern -o fern.png -w 640 -h 640 50000
This command will generate a PNG image of a fern with a width of 640 pixels, a height of 640 pixels, and 50000 points.
Source code in the haskell programming language
import Data.List (scanl')
import Diagrams.Backend.Rasterific.CmdLine
import Diagrams.Prelude
import System.Random
type Pt = (Double, Double)
-- Four affine transformations used to produce a Barnsley fern.
f1, f2, f3, f4 :: Pt -> Pt
f1 (x, y) = ( 0, 0.16 * y)
f2 (x, y) = ( 0.85 * x + 0.04 * y , -0.04 * x + 0.85 * y + 1.60)
f3 (x, y) = ( 0.20 * x - 0.26 * y , 0.23 * x + 0.22 * y + 1.60)
f4 (x, y) = (-0.15 * x + 0.28 * y , 0.26 * x + 0.24 * y + 0.44)
-- Given a random number in [0, 1) transform an initial point by a randomly
-- chosen function.
func :: Pt -> Double -> Pt
func p r | r < 0.01 = f1 p
| r < 0.86 = f2 p
| r < 0.93 = f3 p
| otherwise = f4 p
-- Using a sequence of uniformly distributed random numbers in [0, 1) return
-- the same number of points in the fern.
fern :: [Double] -> [Pt]
fern = scanl' func (0, 0)
-- Given a supply of random values and a count, generate a diagram of a fern
-- composed of that number of points.
drawFern :: [Double] -> Int -> Diagram B
drawFern rs n = frame 0.5 . diagramFrom . take n $ fern rs
where diagramFrom = flip atPoints (repeat dot) . map p2
dot = circle 0.005 # lc green
-- To generate a PNG image of a fern, call this program like:
--
-- fern -o fern.png -w 640 -h 640 50000
--
-- where the arguments specify the width, height and number of points in the
-- image.
main :: IO ()
main = do
rand <- getStdGen
mainWith $ drawFern (randomRs (0, 1) rand)
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Tcl programming language
You may also check:How to resolve the algorithm Quine step by step in the zkl programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Clojure programming language
You may also check:How to resolve the algorithm Home primes step by step in the Go programming language