Skip to content

Commit 3176b92

Browse files
authored
Merge pull request #953 from two-ticks/main
added graphing 2d equations example
2 parents 8e9f839 + d2fd8da commit 3176b92

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @name Graphing 2D Equations
3+
* @frame 710, 400
4+
* @description Graphics the following equation: sin(n*cos(r) + 5*theta) where n is a function of horizontal mouse location. Original by Daniel Shiffman
5+
*/
6+
function setup() {
7+
createCanvas(710, 400);
8+
pixelDensity(1);
9+
}
10+
11+
function draw() {
12+
loadPixels();
13+
let n = (mouseX * 10.0) / width;
14+
const w = 16.0; // 2D space width
15+
const h = 16.0; // 2D space height
16+
const dx = w / width; // Increment x this amount per pixel
17+
const dy = h / height; // Increment y this amount per pixel
18+
let x = -w / 2; // Start x at -1 * width / 2
19+
let y;
20+
21+
let r;
22+
let theta;
23+
let val;
24+
25+
let bw; //variable to store grayscale
26+
let i;
27+
let j;
28+
let cols = width;
29+
let rows = height;
30+
31+
for (i = 0; i < cols; i += 1) {
32+
y = -h / 2;
33+
for (j = 0; j < rows; j += 1) {
34+
r = sqrt(x * x + y * y); // Convert cartesian to polar
35+
theta = atan2(y, x); // Convert cartesian to polar
36+
// Compute 2D polar coordinate function
37+
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
38+
//var val = cos(r); // Another simple function
39+
//var val = sin(theta); // Another simple function
40+
bw = color(((val + 1) * 255) / 2);
41+
index = 4 * (i + j * width);
42+
pixels[index] = red(bw);
43+
pixels[index + 1] = green(bw);
44+
pixels[index + 2] = blue(bw);
45+
pixels[index + 3] = alpha(bw);
46+
y += dy;
47+
}
48+
x += dx;
49+
}
50+
updatePixels();
51+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @name Graphing 2D Equations
3+
* @frame 710, 400
4+
* @description Graphics the following equation: sin(n*cos(r) + 5*theta) where n is a function of horizontal mouse location. Original by Daniel Shiffman
5+
*/
6+
function setup() {
7+
createCanvas(710, 400);
8+
pixelDensity(1);
9+
}
10+
11+
function draw() {
12+
loadPixels();
13+
let n = (mouseX * 10.0) / width;
14+
const w = 16.0; // 2D space width
15+
const h = 16.0; // 2D space height
16+
const dx = w / width; // Increment x this amount per pixel
17+
const dy = h / height; // Increment y this amount per pixel
18+
let x = -w / 2; // Start x at -1 * width / 2
19+
let y;
20+
21+
let r;
22+
let theta;
23+
let val;
24+
25+
let bw; //variable to store grayscale
26+
let i;
27+
let j;
28+
let cols = width;
29+
let rows = height;
30+
31+
for (i = 0; i < cols; i += 1) {
32+
y = -h / 2;
33+
for (j = 0; j < rows; j += 1) {
34+
r = sqrt(x * x + y * y); // Convert cartesian to polar
35+
theta = atan2(y, x); // Convert cartesian to polar
36+
// Compute 2D polar coordinate function
37+
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
38+
//var val = cos(r); // Another simple function
39+
//var val = sin(theta); // Another simple function
40+
bw = color(((val + 1) * 255) / 2);
41+
index = 4 * (i + j * width);
42+
pixels[index] = red(bw);
43+
pixels[index + 1] = green(bw);
44+
pixels[index + 2] = blue(bw);
45+
pixels[index + 3] = alpha(bw);
46+
47+
y += dy;
48+
}
49+
x += dx;
50+
}
51+
updatePixels();
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @name Graphing 2D Equations
3+
* @frame 710, 400
4+
* @description Graphics the following equation: sin(n*cos(r) + 5*theta) where n is a function of horizontal mouse location. Original by Daniel Shiffman
5+
*/
6+
function setup() {
7+
createCanvas(710, 400);
8+
pixelDensity(1);
9+
}
10+
11+
function draw() {
12+
loadPixels();
13+
let n = (mouseX * 10.0) / width;
14+
const w = 16.0; // 2D space width
15+
const h = 16.0; // 2D space height
16+
const dx = w / width; // Increment x this amount per pixel
17+
const dy = h / height; // Increment y this amount per pixel
18+
let x = -w / 2; // Start x at -1 * width / 2
19+
let y;
20+
21+
let r;
22+
let theta;
23+
let val;
24+
25+
let bw; //variable to store grayscale
26+
let i;
27+
let j;
28+
let cols = width;
29+
let rows = height;
30+
31+
for (i = 0; i < cols; i += 1) {
32+
y = -h / 2;
33+
for (j = 0; j < rows; j += 1) {
34+
r = sqrt(x * x + y * y); // Convert cartesian to polar
35+
theta = atan2(y, x); // Convert cartesian to polar
36+
// Compute 2D polar coordinate function
37+
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
38+
//var val = cos(r); // Another simple function
39+
//var val = sin(theta); // Another simple function
40+
bw = color(((val + 1) * 255) / 2);
41+
index = 4 * (i + j * width);
42+
pixels[index] = red(bw);
43+
pixels[index + 1] = green(bw);
44+
pixels[index + 2] = blue(bw);
45+
pixels[index + 3] = alpha(bw);
46+
47+
y += dy;
48+
}
49+
x += dx;
50+
}
51+
updatePixels();
52+
}

0 commit comments

Comments
 (0)