How to resolve the algorithm Chaos game step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Chaos game step by step in the Wren programming language

Table of Contents

Problem Statement

The Chaos Game is a method of generating the attractor of an iterated function system (IFS). One of the best-known and simplest examples creates a fractal, using a polygon and an initial point selected at random.

Play the Chaos Game using the corners of an equilateral triangle as the reference points.   Add a starting point at random (preferably inside the triangle).   Then add the next point halfway between the starting point and one of the reference points.   This reference point is chosen at random. After a sufficient number of iterations, the image of a Sierpinski Triangle should emerge.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Chaos game step by step in the Wren programming language

Source code in the wren programming language

import "dome" for Window
import "graphics" for Canvas, Color
import "math" for Point
import "random" for Random
import "./dynamic" for Tuple
import "./seq" for Stack

var ColoredPoint = Tuple.create("ColoredPoint", ["x", "y", "colorIndex"])

class ChaosGame {
    construct new(width, height) {
        Window.resize(width, height)
        Canvas.resize(width, height)
        Window.title = "Chaos game"
        _width = width
        _height = height
        _stack = Stack.new()
        _points = null
        _colors = [Color.red, Color.green, Color.blue]
        _r = Random.new()
    }

    init() {
        Canvas.cls(Color.white)
        var margin = 60
        var size = _width - 2 * margin
        _points = [
            Point.new((_width/2).floor, margin),
            Point.new(margin, size),
            Point.new(margin + size, size)
        ]
        _stack.push(ColoredPoint.new(-1, -1, 0))
    }

    addPoint() {
        var colorIndex = _r.int(3)
        var p1 = _stack.peek()
        var p2 = _points[colorIndex]
        _stack.push(halfwayPoint(p1, p2, colorIndex))
    }

    drawPoints() {
        for (cp in _stack) {
            var c = _colors[cp.colorIndex]
            Canvas.circlefill(cp.x, cp.y, 1, c)
        }
    }

    halfwayPoint(a, b, idx) { ColoredPoint.new(((a.x + b.x)/2).floor, ((a.y + b.y)/2).floor, idx) }

    update() {
        if (_stack.count < 50000) {
            for (i in 0...25) addPoint()
        }
    }

    draw(alpha) {
        drawPoints()
    }
}

var Game = ChaosGame.new(640, 640)


  

You may also check:How to resolve the algorithm Detect division by zero step by step in the NetLogo programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the AWK programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Scheme programming language
You may also check:How to resolve the algorithm Speech synthesis step by step in the REXX programming language
You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the Ada programming language