Skip to content

Commit 9d9a935

Browse files
committed
added graphing 2d example
1 parent 7ca8e23 commit 9d9a935

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
var n = (mouseX * 10.0) / width;
14+
var w = 16.0; // 2D space width
15+
var h = 16.0; // 2D space height
16+
var dx = w / width; // Increment x this amount per pixel
17+
var dy = h / height; // Increment y this amount per pixel
18+
var x = -w / 2; // Start x at -1 * width / 2
19+
var y;
20+
21+
var r;
22+
var theta;
23+
var 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+
34+
for (j = 0; j < rows; j += 1) {
35+
r = sqrt((x * x) + (y * y)); // Convert cartesian to polar
36+
theta = atan2(y, x); // Convert cartesian to polar
37+
// Compute 2D polar coordinate function
38+
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
39+
//var val = cos(r); // Another simple function
40+
//var val = sin(theta); // Another simple function
41+
bw = color(((val + 1) * 255) / 2);
42+
index = 4 * (i + j * width);
43+
pixels[index] = red(bw);
44+
pixels[index + 1] = green(bw);
45+
pixels[index + 2] = blue(bw);
46+
pixels[index + 3] = alpha(bw);
47+
48+
y += dy;
49+
}
50+
51+
x += dx;
52+
}
53+
updatePixels();
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
var n = (mouseX * 10.0) / width;
14+
var w = 16.0; // 2D space width
15+
var h = 16.0; // 2D space height
16+
var dx = w / width; // Increment x this amount per pixel
17+
var dy = h / height; // Increment y this amount per pixel
18+
var x = -w / 2; // Start x at -1 * width / 2
19+
var y;
20+
21+
var r;
22+
var theta;
23+
var 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+
34+
for (j = 0; j < rows; j += 1) {
35+
r = sqrt((x * x) + (y * y)); // Convert cartesian to polar
36+
theta = atan2(y, x); // Convert cartesian to polar
37+
// Compute 2D polar coordinate function
38+
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
39+
//var val = cos(r); // Another simple function
40+
//var val = sin(theta); // Another simple function
41+
bw = color(((val + 1) * 255) / 2);
42+
index = 4 * (i + j * width);
43+
pixels[index] = red(bw);
44+
pixels[index + 1] = green(bw);
45+
pixels[index + 2] = blue(bw);
46+
pixels[index + 3] = alpha(bw);
47+
48+
y += dy;
49+
}
50+
51+
x += dx;
52+
}
53+
updatePixels();
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
var n = (mouseX * 10.0) / width;
14+
var w = 16.0; // 2D space width
15+
var h = 16.0; // 2D space height
16+
var dx = w / width; // Increment x this amount per pixel
17+
var dy = h / height; // Increment y this amount per pixel
18+
var x = -w / 2; // Start x at -1 * width / 2
19+
var y;
20+
21+
var r;
22+
var theta;
23+
var 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+
34+
for (j = 0; j < rows; j += 1) {
35+
r = sqrt((x * x) + (y * y)); // Convert cartesian to polar
36+
theta = atan2(y, x); // Convert cartesian to polar
37+
// Compute 2D polar coordinate function
38+
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
39+
//var val = cos(r); // Another simple function
40+
//var val = sin(theta); // Another simple function
41+
bw = color(((val + 1) * 255) / 2);
42+
index = 4 * (i + j * width);
43+
pixels[index] = red(bw);
44+
pixels[index + 1] = green(bw);
45+
pixels[index + 2] = blue(bw);
46+
pixels[index + 3] = alpha(bw);
47+
48+
y += dy;
49+
}
50+
51+
x += dx;
52+
}
53+
updatePixels();
54+
}

0 commit comments

Comments
 (0)