How to resolve the algorithm Pythagoras tree step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pythagoras tree step by step in the Wren programming language
Table of Contents
Problem Statement
The Pythagoras tree is a fractal tree constructed from squares. It is named after Pythagoras because each triple of touching squares encloses a right triangle, in a configuration traditionally used to represent the Pythagorean theorem.
Construct a Pythagoras tree of order 7 using only vectors (no rotation or trigonometric functions).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pythagoras tree 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 "./polygon" for Polygon
var DepthLimit = 7
var Hue = 0.15
class PythagorasTree {
construct new(width, height) {
Window.title = "Pythagoras Tree"
Window.resize(width, height)
Canvas.resize(width, height)
}
init() {
Canvas.cls(Color.white)
drawTree(275, 500, 375, 500, 0)
}
drawTree(x1, y1, x2, y2, depth) {
if (depth == DepthLimit) return
var dx = x2 - x1
var dy = y1 - y2
var x3 = x2 - dy
var y3 = y2 - dx
var x4 = x1 - dy
var y4 = y1 - dx
var x5 = x4 + 0.5 * (dx - dy)
var y5 = y4 - 0.5 * (dx + dy)
// draw a square
var col = Color.hsv((Hue + depth * 0.02) * 360, 1, 1)
var square = Polygon.quick([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
square.drawfill(col)
square.draw(Color.lightgray)
// draw a triangle
col = Color.hsv((Hue + depth * 0.035) * 360, 1, 1)
var triangle = Polygon.quick([[x3, y3], [x4, y4], [x5, y5]])
triangle.drawfill(col)
triangle.draw(Color.lightgray)
drawTree(x4, y4, x5, y5, depth + 1)
drawTree(x5, y5, x3, y3, depth + 1)
}
update() {}
draw(alpha) {}
}
var Game = PythagorasTree.new(640, 640)
You may also check:How to resolve the algorithm One-time pad step by step in the Perl programming language
You may also check:How to resolve the algorithm Gauss-Jordan matrix inversion step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Racket programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the C# programming language