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

Published on 12 May 2024 09:40 PM
#Go

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

Table of Contents

Problem Statement

Produce a graphical or ASCII-art representation of a  Sierpinski arrowhead curve  of at least order  3.

Let's start with the solution:

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

Explanation of the Go source code

The Go source code provided creates a Sierpinski Arrowhead curve using the gg package, which is a 2D graphics library for Go. The code uses a combination of line segments and curves to create the fractal pattern.

Breaking down the code:

  1. Import Statements:

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

    These statements import the necessary libraries for creating the graphics and performing mathematical operations.

  2. Global Variables:

    var (
       width  = 770.0
       height = 770.0
       dc     = gg.NewContext(int(width), int(height))
       iy     = 1.0
       theta  = 0
    )

    These variables define the width and height of the image, create a new drawing context for the graphics, and initialize other parameters for the fractal curve.

  3. Functions: a. arrowhead: This function draws the arrowhead shape by alternating between curves and line segments. b. drawLine: This function draws a line segment of the desired length at the current position. c. turn: This function rotates the angle by the specified degrees. d. curve: This function recursively draws the Sierpinski curve of the specified order.

  4. Main Function: a. It sets the background color to black and clears the drawing context. b. Initializes the order of the curve, and adjusts the iy variable for the curve's orientation. c. Sets the initial position and size of the curve, and draws the arrowhead. d. Sets the color and line width for the curve. e. Draws the curve and saves the image as a PNG file.

How the program works:

  1. The program starts by setting up the drawing context and initializing the parameters for the fractal curve.
  2. The arrowhead function is called to draw the first part of the curve, which consists of a series of line segments and curves.
  3. The curve function is then called recursively to generate the remaining parts of the Sierpinski curve.
  4. The program draws the curve using the gg library, setting the color and line width according to the specified parameters.
  5. Finally, it saves the image of the curve as a PNG file.

This program demonstrates the use of Go's graphics library to create interesting fractal patterns.

Source code in the go programming language

package main

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

var (
    width  = 770.0
    height = 770.0
    dc     = gg.NewContext(int(width), int(height))
    iy     = 1.0
    theta  = 0
)

var cx, cy, h float64

func arrowhead(order int, length float64) {
    // if order is even, we can just draw the curve
    if order&1 == 0 {
        curve(order, length, 60)
    } else {
        turn(60)
        curve(order, length, -60)
    }
    drawLine(length) // needed to make base symmetric
}

func drawLine(length float64) {
    dc.LineTo(cx-width/2+h, (height-cy)*iy+2*h)
    rads := gg.Radians(float64(theta))
    cx += length * math.Cos(rads)
    cy += length * math.Sin(rads)
}

func turn(angle int) {
    theta = (theta + angle) % 360
}

func curve(order int, length float64, angle int) {
    if order == 0 {
        drawLine(length)
    } else {
        curve(order-1, length/2, -angle)
        turn(angle)
        curve(order-1, length/2, angle)
        turn(angle)
        curve(order-1, length/2, -angle)
    }
}

func main() {
    dc.SetRGB(0, 0, 0) // black background
    dc.Clear()
    order := 6
    if order&1 == 0 {
        iy = -1 // apex will point upwards
    }
    cx, cy = width/2, height
    h = cx / 2
    arrowhead(order, cx)
    dc.SetRGB255(255, 0, 255) // magenta curve
    dc.SetLineWidth(2)
    dc.Stroke()
    dc.SavePNG("sierpinski_arrowhead_curve.png")
}


  

You may also check:How to resolve the algorithm Caesar cipher step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Lua programming language
You may also check:How to resolve the algorithm Function definition step by step in the BlitzMax programming language
You may also check:How to resolve the algorithm Append a record to the end of a text file step by step in the Common Lisp programming language