How to resolve the algorithm Barnsley fern step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Barnsley fern step by step in the Racket 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 Racket programming language

Source code in the racket programming language

#lang racket

(require racket/draw)

(define fern-green (make-color #x32 #xCD #x32 0.66))

(define (fern dc n-iterations w h)
  (for/fold ((x #i0) (y #i0))
            ((i n-iterations))
    (define-values (x′ y′)
      (let ((r (random)))
        (cond
          [(<= r 0.01) (values 0
                               (* y 16/100))]
          [(<= r 0.08) (values (+ (* x 20/100) (* y -26/100))
                               (+ (* x 23/100) (* y 22/100) 16/10))]
          [(<= r 0.15) (values (+ (* x -15/100) (* y 28/100))
                               (+ (* x 26/100) (* y 24/100) 44/100))]
          [else (values (+ (* x 85/100) (* y 4/100))
                        (+ (* x -4/100) (* y 85/100) 16/10))])))

    (define px (+ (/ w 2) (* x w 1/11)))
    (define py (- h (* y h 1/11)))
    (send dc set-pixel (exact-round px) (exact-round py) fern-green)                   
    (values x′ y′)))


(define bmp (make-object bitmap% 640 640 #f #t 2))

(fern (new bitmap-dc% [bitmap bmp]) 200000 640 640)

bmp
(send bmp save-file "images/racket-barnsley-fern.png" 'png)


  

You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Swift programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Tcl programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the PicoLisp programming language