How to resolve the algorithm Sierpinski pentagon step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Sierpinski pentagon step by step in the Go programming language

Table of Contents

Problem Statement

Produce a graphical or ASCII-art representation of a Sierpinski pentagon (aka a Pentaflake) of order 5. Your code should also be able to correctly generate representations of lower orders: 1 to 4.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski pentagon step by step in the Go programming language

Go Programming Language

Source Code Overview:

This Go program generates a Sierpinski pentagon fractal, a geometric pattern created by repeatedly subdividing a pentagon into smaller pentagons.

Variables and Constants:

  • red, green, blue, magenta, cyan: RGB color values for the different pentagons.
  • w, h: Image width and height.
  • dc: Image context for drawing.
  • deg72: Angle of 72 degrees in radians, used for drawing the pentagon.
  • scaleFactor: Factor used to scale down the pentagon with each recursive iteration.
  • palette: Array of colors used for the pentagons.
  • colorIndex: Index of the current color in the palette.

Function drawPentagon:

  • This function recursively draws a pentagon at a specified location and side length.
  • It uses the MoveTo, LineTo, and Fill methods of the gg context to draw the lines and fill the shape.
  • The angle variable is used to rotate the pentagon and side to adjust its size.
  • depth represents the recursive depth, and the base case is when depth reaches 0, at which point the pentagon is drawn and filled with a color.

Main Function:

  • Sets the background color to white.
  • Sets the order of the fractal (5 for the Sierpinski pentagon).
  • Calculates the half-width of the image (hw), margin, radius, and side length of the pentagon.
  • Calls drawPentagon to generate the fractal at the center of the image.
  • Saves the generated fractal to a PNG file.

Execution:

When the program is run, it creates a white image with a Sierpinski pentagon fractal in the center. The fractal's order and colors can be adjusted by changing the respective variables.

Source code in the go programming language

package main

import (
    "github.com/fogleman/gg"
    "image/color"
    "math"
)

var (
    red     = color.RGBA{255, 0, 0, 255}
    green   = color.RGBA{0, 255, 0, 255}
    blue    = color.RGBA{0, 0, 255, 255}
    magenta = color.RGBA{255, 0, 255, 255}
    cyan    = color.RGBA{0, 255, 255, 255}
)

var (
    w, h        = 640, 640
    dc          = gg.NewContext(w, h)
    deg72       = gg.Radians(72)
    scaleFactor = 1 / (2 + math.Cos(deg72)*2)
    palette     = [5]color.Color{red, green, blue, magenta, cyan}
    colorIndex  = 0
)

func drawPentagon(x, y, side float64, depth int) {
    angle := 3 * deg72
    if depth == 0 {
        dc.MoveTo(x, y)
        for i := 0; i < 5; i++ {
            x += math.Cos(angle) * side
            y -= math.Sin(angle) * side
            dc.LineTo(x, y)
            angle += deg72
        }
        dc.SetColor(palette[colorIndex])
        dc.Fill()
        colorIndex = (colorIndex + 1) % 5
    } else {
        side *= scaleFactor
        dist := side * (1 + math.Cos(deg72)*2)
        for i := 0; i < 5; i++ {
            x += math.Cos(angle) * dist
            y -= math.Sin(angle) * dist
            drawPentagon(x, y, side, depth-1)
            angle += deg72
        }
    }
}

func main() {
    dc.SetRGB(1, 1, 1) // White background
    dc.Clear()
    order := 5 // Can also set this to 1, 2, 3 or 4
    hw := float64(w / 2)
    margin := 20.0
    radius := hw - 2*margin
    side := radius * math.Sin(math.Pi/5) * 2
    drawPentagon(hw, 3*margin, side, order-1)
    dc.SavePNG("sierpinski_pentagon.png")
}


  

You may also check:How to resolve the algorithm Empty string step by step in the Scala programming language
You may also check:How to resolve the algorithm Execute Brain step by step in the Ada programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Ada programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Oz programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Crystal programming language