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 forlist loops need to be retained. Any help is appreciated! Here is my current code:
#lang racket
require racketdraw
define toradians degrees
degrees pi
; Function to draw a single soft leaflike triangle
define drawsoftleaf dc x y width height angle color
letanglerad toradians angle ; Convert angle to radians
; Tip of the triangle
tipx x height cos anglerad
tipy y height sin anglerad
; Left and right base corners
baseleftx x width cos anglerad pi
baselefty y width sin anglerad pi
baserightx x width cos anglerad pi
baserighty y width sin anglerad pi
; Control points for left and right curves
controlleftx baseleftx height cos anglerad pi
controllefty baselefty height sin anglerad pi
controlrightx baserightx height cos anglerad pi
controlrighty baserighty height sin anglerad pi
; Perform drawing actions after defining all variables
send dc setbrush color 'solid
let path new dcpath ; Create a new path for the shape
send path moveto baseleftx baselefty ; Start at the left base
send path curveto baseleftx baselefty controlleftx controllefty tipx tipy ; Curve to the tip
send path curveto tipx tipy controlrightx controlrighty baserightx baserighty ; Curve back to the right base
send path lineto baseleftx baselefty ; Close the shape
send dc drawpath path ; Draw the path
; Generate colors for the polygons
define randomcolor
makeobject colorrandom random random ; Shades of green
; Generate a spiral fractal for polygons
define generatespiralpolygons numpolygons
define goldenangle ; Golden angle for spirals
define maxradius ; Maximum radius for the fractal
define minradius ; Minimum radius for the fractal
forlist i inrange numpolygons
letradius minradius maxradius expt i numpolygons ; Adjusted scaling
angle i goldenangle ; Angle increases with each step
x radius cos toradians angle ; Centered at half of width
y radius sin toradians angle ; Centered at half of height
width radius ; Polygon width
height radius ; Polygon height
list x y width height angle randomcolor ; Properly define and return all values
; Function to draw all fractal polygons
define drawfractal dc polygons
for polygon polygons
apply drawsoftleaf dc polygon ; Pass each polygon as arguments to drawsoftleaf
; Main function to save the fractal image
define savefractalimage
define width
define height
define bitmap makebitmap width height ; Create a blank bitmap
define dc new bitmapdcbitmap bitmap ; Create a drawing context for the bitmap
send dc clear ; Clear the canvas
drawfractal dc generatespiralpolygons ; Draw the fractal
send bitmap savefile "fractal.pngpng ; Save the image to a file
savefractalimage ; Run the function to save the image
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
