Question: I am running a racket program. I need to make a polygon fractal. I am not quite able to code it to look like a

I am running a racket program. I need to make a polygon fractal. I am not quite able to code it to look like a cactus- the fractal I am tasked to make. The frame, amount of polygons, and for/list loops need to be retained. Any help is appreciated! Here is my current code:
#lang racket
(require racket/draw)
(define (to-radians degrees)
(* degrees (/ pi 180)))
; Function to draw a single soft leaf-like triangle
(define (draw-soft-leaf dc x y width height angle color)
(let*([angle-rad (to-radians angle)] ; Convert angle to radians
; Tip of the triangle
[tip-x (+ x (* height (cos angle-rad)))]
[tip-y (+ y (* height (sin angle-rad)))]
; Left and right base corners
[base-left-x (+ x (* width (cos (+ angle-rad (/ pi 2)))))]
[base-left-y (+ y (* width (sin (+ angle-rad (/ pi 2)))))]
[base-right-x (+ x (* width (cos (- angle-rad (/ pi 2)))))]
[base-right-y (+ y (* width (sin (- angle-rad (/ pi 2)))))]
; Control points for left and right curves
[control-left-x (+ base-left-x (* height 0.3(cos (+ angle-rad (/ pi 3)))))]
[control-left-y (+ base-left-y (* height 0.3(sin (+ angle-rad (/ pi 3)))))]
[control-right-x (+ base-right-x (* height 0.3(cos (- angle-rad (/ pi 3)))))]
[control-right-y (+ base-right-y (* height 0.3(sin (- angle-rad (/ pi 3)))))])
; Perform drawing actions after defining all variables
(send dc set-brush color 'solid)
(let ([path (new dc-path%)]) ; Create a new path for the shape
(send path move-to base-left-x base-left-y) ; Start at the left base
(send path curve-to base-left-x base-left-y control-left-x control-left-y tip-x tip-y) ; Curve to the tip
(send path curve-to tip-x tip-y control-right-x control-right-y base-right-x base-right-y) ; Curve back to the right base
(send path line-to base-left-x base-left-y) ; Close the shape
(send dc draw-path path)))) ; Draw the path
; Generate colors for the polygons
(define (random-color)
(make-object color%(random 50150)(random 150200)(random 50100))) ; Shades of green
; Generate a spiral fractal for polygons
(define (generate-spiral-polygons num-polygons)
(define golden-angle 137.5) ; Golden angle for spirals
(define max-radius 450) ; Maximum radius for the fractal
(define min-radius 0.005) ; Minimum radius for the fractal
(for/list ([i (in-range num-polygons)])
(let*([radius (+ min-radius (* max-radius (expt 10(-12(* i (/15.0 num-polygons))))))] ; Adjusted scaling
[angle (* i golden-angle)] ; Angle increases with each step
[x (+960(* radius (cos (to-radians angle))))] ; Centered at 960(half of 1920 width)
[y (+540(* radius (sin (to-radians angle))))] ; Centered at 540(half of 1080 height)
[width (* radius 0.02)] ; Polygon width
[height (* radius 0.05)]) ; Polygon height
(list x y width height angle (random-color))))) ; Properly define and return all values
; Function to draw all fractal polygons
(define (draw-fractal dc polygons)
(for ([polygon polygons])
(apply draw-soft-leaf dc polygon))) ; Pass each polygon as arguments to draw-soft-leaf
; Main function to save the fractal image
(define (save-fractal-image)
(define width 1920)
(define height 1080)
(define bitmap (make-bitmap width height)) ; Create a blank bitmap
(define dc (new bitmap-dc%[bitmap bitmap])) ; Create a drawing context for the bitmap
(send dc clear) ; Clear the canvas
(draw-fractal dc (generate-spiral-polygons 50000)) ; Draw the fractal
(send bitmap save-file "fractal.png"'png)) ; Save the image to a file
(save-fractal-image) ; Run the function to save the image
I am running a racket program. I need to make a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!