How to resolve the algorithm Sierpinski square curve step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sierpinski square curve step by step in the Wren programming language
Table of Contents
Problem Statement
Produce a graphical or ASCII-art representation of a Sierpinski square curve of at least order 3.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sierpinski square curve step by step in the Wren programming language
Source code in the wren programming language
import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
import "./lsystem" for LSystem, Rule
var TwoPi = Num.pi * 2
class SierpinskiSquareCurve {
construct new(width, height, back, fore) {
Window.title = "Sierpinski Square Curve"
Window.resize(width, height)
Canvas.resize(width, height)
_w = width
_h = height
_bc = back
_fc = fore
}
init() {
Canvas.cls(_bc)
var cx = 10
var cy = (_h/2).floor + 5
var theta = 0
var h = 6
var lsys = LSystem.new(
["X"], // variables
["F", "+", "-"], // constants
"F+XF+F+XF", // axiom
[Rule.new("X", "XF-F+F-XF+F+XF-F+F-X")], // rules
Num.pi / 2 // angle (90 degrees in radians)
)
var result = lsys.iterate(5)
var operations = {
"F": Fn.new {
var newX = cx + h*Math.sin(theta)
var newY = cy - h*Math.cos(theta)
Canvas.line(cx, cy, newX, newY, _fc, 2)
cx = newX
cy = newY
},
"+": Fn.new {
theta = (theta + lsys.angle) % TwoPi
},
"-": Fn.new {
theta = (theta - lsys.angle) % TwoPi
}
}
LSystem.execute(result, operations)
}
update() {}
draw(alpha) {}
}
var Game = SierpinskiSquareCurve.new(770, 770, Color.blue, Color.yellow)
You may also check:How to resolve the algorithm Mutual recursion step by step in the Draco programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the YAMLScript programming language
You may also check:How to resolve the algorithm Even or odd step by step in the BQN programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Chapel programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the EchoLisp programming language