How to resolve the algorithm Pythagoras tree step by step in the Processing programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pythagoras tree step by step in the Processing 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 Processing programming language
Source code in the processing programming language
void tree(float x1, float y1, float x2, float y2, int depth) {
if (depth <= 0) {
return;
}
float dx = (x2 - x1);
float dy = (y1 - y2);
float x3 = (x2 - dy);
float y3 = (y2 - dx);
float x4 = (x1 - dy);
float y4 = (y1 - dx);
float x5 = (x4 + 0.5*(dx - dy));
float y5 = (y4 - 0.5*(dx + dy));
// square
beginShape();
fill(0.0, 255.0/depth, 0.0);
vertex(x1, y1);
vertex(x2, y2);
vertex(x3, y3);
vertex(x4, y4);
vertex(x1, y1);
endShape();
// triangle
beginShape();
fill(0.0, 255.0/depth, 0.0);
vertex(x3, y3);
vertex(x4, y4);
vertex(x5, y5);
vertex(x3, y3);
endShape();
tree(x4, y4, x5, y5, depth-1);
tree(x5, y5, x3, y3, depth-1);
}
void setup() {
size(1920, 1080);
background(255);
stroke(0, 255, 0);
tree(width/2.3, height, width/1.8, height, 10);
}
def setup():
size(800, 400)
background(255)
stroke(0, 255, 0)
tree(width / 2.3, height, width / 1.8, height, 10)
def tree(x1, y1, x2, y2, depth):
if depth <= 0: return
dx = (x2 - x1)
dy = (y1 - y2)
x3 = (x2 - dy)
y3 = (y2 - dx)
x4 = (x1 - dy)
y4 = (y1 - dx)
x5 = (x4 + 0.5 * (dx - dy))
y5 = (y4 - 0.5 * (dx + dy))
# square
beginShape()
fill(0.0, 255.0 / depth, 0.0)
vertex(x1, y1)
vertex(x2, y2)
vertex(x3, y3)
vertex(x4, y4)
vertex(x1, y1)
endShape()
# triangle
beginShape()
fill(0.0, 255.0 / depth, 0.0)
vertex(x3, y3)
vertex(x4, y4)
vertex(x5, y5)
vertex(x3, y3)
endShape()
tree(x4, y4, x5, y5, depth - 1)
tree(x5, y5, x3, y3, depth - 1)
You may also check:How to resolve the algorithm Comma quibbling step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Anadromes step by step in the Phix programming language
You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Population count step by step in the Wren programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Phixmonti programming language