Skip to content

Commit 664ad62

Browse files
authored
Merge pull request #408 from ihsavru/dev
Port recursive tree, mandelbrot example
2 parents fff5cd4 + ce6bf0f commit 664ad62

File tree

7 files changed

+424
-0
lines changed

7 files changed

+424
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ node_modules
2121
*.sublime-*
2222
.DS_Store
2323
*/.DS_Store
24+
.idea/
2425

2526
src/templates/pages/examples/*
2627
src/offline-reference
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @name Recursive Tree
3+
* @description Renders a simple tree-like structure via recursion.
4+
* The branching angle is calculated as a function of the horizontal mouse
5+
* location. Move the mouse left and right to change the angle.
6+
* Based on Daniel Shiffman's <a href="https://processing.org/examples/tree.html">Recursive Tree Example</a> for Processing.
7+
*/
8+
let theta;
9+
10+
function setup() {
11+
createCanvas(710, 400);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
frameRate(30);
17+
stroke(255);
18+
// Let's pick an angle 0 to 90 degrees based on the mouse position
19+
let a = (mouseX / width) * 90;
20+
// Convert it to radians
21+
theta = radians(a);
22+
// Start the tree from the bottom of the screen
23+
translate(width/2,height);
24+
// Draw a line 120 pixels
25+
line(0,0,0,-120);
26+
// Move to the end of that line
27+
translate(0,-120);
28+
// Start the recursive branching!
29+
branch(120);
30+
31+
}
32+
33+
function branch(h) {
34+
// Each branch will be 2/3rds the size of the previous one
35+
h *= 0.66;
36+
37+
// All recursive functions must have an exit condition!!!!
38+
// Here, ours is when the length of the branch is 2 pixels or less
39+
if (h > 2) {
40+
push(); // Save the current state of transformation (i.e. where are we now)
41+
rotate(theta); // Rotate by theta
42+
line(0, 0, 0, -h); // Draw the branch
43+
translate(0, -h); // Move to the end of the branch
44+
branch(h); // Ok, now call myself to draw two new branches!!
45+
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
46+
47+
// Repeat the same thing, only branch off to the "left" this time!
48+
push();
49+
rotate(-theta);
50+
line(0, 0, 0, -h);
51+
translate(0, -h);
52+
branch(h);
53+
pop();
54+
}
55+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* @name The Mandelbrot Set
3+
* @description Simple rendering of the Mandelbrot set.
4+
* Based on Daniel Shiffman's <a href="https://processing.org/examples/mandelbrot.html">Mandelbrot Example</a> for Processing.
5+
*/
6+
7+
function setup() {
8+
createCanvas(710, 400);
9+
pixelDensity(1);
10+
noLoop();
11+
}
12+
13+
function draw() {
14+
background(0);
15+
16+
// Establish a range of values on the complex plane
17+
// A different range will allow us to "zoom" in or out on the fractal
18+
19+
// It all starts with the width, try higher or lower values
20+
const w = 4;
21+
const h = (w * height) / width;
22+
23+
// Start at negative half the width and height
24+
const xmin = -w/2;
25+
const ymin = -h/2;
26+
27+
// Make sure we can write to the pixels[] array.
28+
// Only need to do this once since we don't do any other drawing.
29+
loadPixels();
30+
31+
// Maximum number of iterations for each point on the complex plane
32+
const maxiterations = 100;
33+
34+
// x goes from xmin to xmax
35+
const xmax = xmin + w;
36+
// y goes from ymin to ymax
37+
const ymax = ymin + h;
38+
39+
// Calculate amount we increment x,y for each pixel
40+
const dx = (xmax - xmin) / (width);
41+
const dy = (ymax - ymin) / (height);
42+
43+
// Start y
44+
let y = ymin;
45+
for (let j = 0; j < height; j++) {
46+
// Start x
47+
let x = xmin;
48+
for (let i = 0; i < width; i++) {
49+
50+
// Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
51+
let a = x;
52+
let b = y;
53+
let n = 0;
54+
while (n < maxiterations) {
55+
const aa = a * a;
56+
const bb = b * b;
57+
const twoab = 2.0 * a * b;
58+
a = aa - bb + x;
59+
b = twoab + y;
60+
// Infinty in our finite world is simple, let's just consider it 16
61+
if (dist(aa, bb, 0, 0) > 16) {
62+
break; // Bail
63+
}
64+
n++;
65+
}
66+
67+
// We color each pixel based on how long it takes to get to infinity
68+
// If we never got there, let's pick the color black
69+
const pix = (i+j*width)*4;
70+
const norm = map(n, 0, maxiterations, 0, 1);
71+
let bright = map(sqrt(norm), 0, 1, 0, 255);
72+
if (n == maxiterations) {
73+
bright = 0;
74+
} else {
75+
// Gosh, we could make fancy colors here if we wanted
76+
pixels[pix + 0] = bright;
77+
pixels[pix + 1] = bright;
78+
pixels[pix + 2] = bright;
79+
pixels[pix + 3] = 255;
80+
}
81+
x += dx;
82+
}
83+
y += dy;
84+
}
85+
updatePixels();
86+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @name Recursive Tree
3+
* @description Renders a simple tree-like structure via recursion.
4+
* The branching angle is calculated as a function of the horizontal mouse
5+
* location. Move the mouse left and right to change the angle.
6+
* Based on Daniel Shiffman's <a href="https://processing.org/examples/tree.html">Recursive Tree Example</a> for Processing.
7+
*/
8+
let theta;
9+
10+
function setup() {
11+
createCanvas(710, 400);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
frameRate(30);
17+
stroke(255);
18+
// Let's pick an angle 0 to 90 degrees based on the mouse position
19+
let a = (mouseX / width) * 90;
20+
// Convert it to radians
21+
theta = radians(a);
22+
// Start the tree from the bottom of the screen
23+
translate(width/2,height);
24+
// Draw a line 120 pixels
25+
line(0,0,0,-120);
26+
// Move to the end of that line
27+
translate(0,-120);
28+
// Start the recursive branching!
29+
branch(120);
30+
31+
}
32+
33+
function branch(h) {
34+
// Each branch will be 2/3rds the size of the previous one
35+
h *= 0.66;
36+
37+
// All recursive functions must have an exit condition!!!!
38+
// Here, ours is when the length of the branch is 2 pixels or less
39+
if (h > 2) {
40+
push(); // Save the current state of transformation (i.e. where are we now)
41+
rotate(theta); // Rotate by theta
42+
line(0, 0, 0, -h); // Draw the branch
43+
translate(0, -h); // Move to the end of the branch
44+
branch(h); // Ok, now call myself to draw two new branches!!
45+
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
46+
47+
// Repeat the same thing, only branch off to the "left" this time!
48+
push();
49+
rotate(-theta);
50+
line(0, 0, 0, -h);
51+
translate(0, -h);
52+
branch(h);
53+
pop();
54+
}
55+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* @name The Mandelbrot Set
3+
* @description Simple rendering of the Mandelbrot set.
4+
* Based on Daniel Shiffman's <a href="https://processing.org/examples/mandelbrot.html">Mandelbrot Example</a> for Processing.
5+
*/
6+
7+
function setup() {
8+
createCanvas(710, 400);
9+
pixelDensity(1);
10+
noLoop();
11+
}
12+
13+
function draw() {
14+
background(0);
15+
16+
// Establish a range of values on the complex plane
17+
// A different range will allow us to "zoom" in or out on the fractal
18+
19+
// It all starts with the width, try higher or lower values
20+
const w = 4;
21+
const h = (w * height) / width;
22+
23+
// Start at negative half the width and height
24+
const xmin = -w/2;
25+
const ymin = -h/2;
26+
27+
// Make sure we can write to the pixels[] array.
28+
// Only need to do this once since we don't do any other drawing.
29+
loadPixels();
30+
31+
// Maximum number of iterations for each point on the complex plane
32+
const maxiterations = 100;
33+
34+
// x goes from xmin to xmax
35+
const xmax = xmin + w;
36+
// y goes from ymin to ymax
37+
const ymax = ymin + h;
38+
39+
// Calculate amount we increment x,y for each pixel
40+
const dx = (xmax - xmin) / (width);
41+
const dy = (ymax - ymin) / (height);
42+
43+
// Start y
44+
let y = ymin;
45+
for (let j = 0; j < height; j++) {
46+
// Start x
47+
let x = xmin;
48+
for (let i = 0; i < width; i++) {
49+
50+
// Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
51+
let a = x;
52+
let b = y;
53+
let n = 0;
54+
while (n < maxiterations) {
55+
const aa = a * a;
56+
const bb = b * b;
57+
const twoab = 2.0 * a * b;
58+
a = aa - bb + x;
59+
b = twoab + y;
60+
// Infinty in our finite world is simple, let's just consider it 16
61+
if (dist(aa, bb, 0, 0) > 16) {
62+
break; // Bail
63+
}
64+
n++;
65+
}
66+
67+
// We color each pixel based on how long it takes to get to infinity
68+
// If we never got there, let's pick the color black
69+
const pix = (i+j*width)*4;
70+
const norm = map(n, 0, maxiterations, 0, 1);
71+
let bright = map(sqrt(norm), 0, 1, 0, 255);
72+
if (n == maxiterations) {
73+
bright = 0;
74+
} else {
75+
// Gosh, we could make fancy colors here if we wanted
76+
pixels[pix + 0] = bright;
77+
pixels[pix + 1] = bright;
78+
pixels[pix + 2] = bright;
79+
pixels[pix + 3] = 255;
80+
}
81+
x += dx;
82+
}
83+
y += dy;
84+
}
85+
updatePixels();
86+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @name Recursive Tree
3+
* @description Renders a simple tree-like structure via recursion.
4+
* The branching angle is calculated as a function of the horizontal mouse
5+
* location. Move the mouse left and right to change the angle.
6+
* Based on Daniel Shiffman's <a href="https://processing.org/examples/tree.html">Recursive Tree Example</a> for Processing.
7+
*/
8+
let theta;
9+
10+
function setup() {
11+
createCanvas(710, 400);
12+
}
13+
14+
function draw() {
15+
background(0);
16+
frameRate(30);
17+
stroke(255);
18+
// Let's pick an angle 0 to 90 degrees based on the mouse position
19+
let a = (mouseX / width) * 90;
20+
// Convert it to radians
21+
theta = radians(a);
22+
// Start the tree from the bottom of the screen
23+
translate(width/2,height);
24+
// Draw a line 120 pixels
25+
line(0,0,0,-120);
26+
// Move to the end of that line
27+
translate(0,-120);
28+
// Start the recursive branching!
29+
branch(120);
30+
31+
}
32+
33+
function branch(h) {
34+
// Each branch will be 2/3rds the size of the previous one
35+
h *= 0.66;
36+
37+
// All recursive functions must have an exit condition!!!!
38+
// Here, ours is when the length of the branch is 2 pixels or less
39+
if (h > 2) {
40+
push(); // Save the current state of transformation (i.e. where are we now)
41+
rotate(theta); // Rotate by theta
42+
line(0, 0, 0, -h); // Draw the branch
43+
translate(0, -h); // Move to the end of the branch
44+
branch(h); // Ok, now call myself to draw two new branches!!
45+
pop(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
46+
47+
// Repeat the same thing, only branch off to the "left" this time!
48+
push();
49+
rotate(-theta);
50+
line(0, 0, 0, -h);
51+
translate(0, -h);
52+
branch(h);
53+
pop();
54+
}
55+
}

0 commit comments

Comments
 (0)