Skip to content

added graphing 2d equations example #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/data/examples/en/08_Math/20_Graphing2DEquations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @name Graphing 2D Equations
* @frame 710, 400
* @description Graphics the following equation: sin(n*cos(r) + 5*theta) where n is a function of horizontal mouse location. Original by Daniel Shiffman
*/
function setup() {
createCanvas(710, 400);
pixelDensity(1);
}

function draw() {
loadPixels();
let n = (mouseX * 10.0) / width;
const w = 16.0; // 2D space width
const h = 16.0; // 2D space height
const dx = w / width; // Increment x this amount per pixel
const dy = h / height; // Increment y this amount per pixel
let x = -w / 2; // Start x at -1 * width / 2
let y;

let r;
let theta;
let val;

let bw; //variable to store grayscale
let i;
let j;
let cols = width;
let rows = height;

for (i = 0; i < cols; i += 1) {
y = -h / 2;
for (j = 0; j < rows; j += 1) {
r = sqrt(x * x + y * y); // Convert cartesian to polar
theta = atan2(y, x); // Convert cartesian to polar
// Compute 2D polar coordinate function
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
//var val = cos(r); // Another simple function
//var val = sin(theta); // Another simple function
bw = color(((val + 1) * 255) / 2);
index = 4 * (i + j * width);
pixels[index] = red(bw);
pixels[index + 1] = green(bw);
pixels[index + 2] = blue(bw);
pixels[index + 3] = alpha(bw);
y += dy;
}
x += dx;
}
updatePixels();
}
54 changes: 54 additions & 0 deletions src/data/examples/es/08_Math/20_Graphing2DEquations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @name Graphing 2D Equations
* @frame 710, 400
* @description Graphics the following equation: sin(n*cos(r) + 5*theta) where n is a function of horizontal mouse location. Original by Daniel Shiffman
*/
function setup() {
createCanvas(710, 400);
pixelDensity(1);
}

function draw() {
loadPixels();
var n = (mouseX * 10.0) / width;
var w = 16.0; // 2D space width
var h = 16.0; // 2D space height
var dx = w / width; // Increment x this amount per pixel
var dy = h / height; // Increment y this amount per pixel
var x = -w / 2; // Start x at -1 * width / 2
var y;

var r;
var theta;
var val;

let bw; //variable to store grayscale
let i;
let j;
let cols = width;
let rows = height;

for (i = 0; i < cols; i += 1) {
y = -h / 2;

for (j = 0; j < rows; j += 1) {
r = sqrt((x * x) + (y * y)); // Convert cartesian to polar
theta = atan2(y, x); // Convert cartesian to polar
// Compute 2D polar coordinate function
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
//var val = cos(r); // Another simple function
//var val = sin(theta); // Another simple function
bw = color(((val + 1) * 255) / 2);
index = 4 * (i + j * width);
pixels[index] = red(bw);
pixels[index + 1] = green(bw);
pixels[index + 2] = blue(bw);
pixels[index + 3] = alpha(bw);

y += dy;
}

x += dx;
}
updatePixels();
}
54 changes: 54 additions & 0 deletions src/data/examples/zh-Hans/08_Math/20_Graphing2DEquations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @name Graphing 2D Equations
* @frame 710, 400
* @description Graphics the following equation: sin(n*cos(r) + 5*theta) where n is a function of horizontal mouse location. Original by Daniel Shiffman
*/
function setup() {
createCanvas(710, 400);
pixelDensity(1);
}

function draw() {
loadPixels();
var n = (mouseX * 10.0) / width;
var w = 16.0; // 2D space width
var h = 16.0; // 2D space height
var dx = w / width; // Increment x this amount per pixel
var dy = h / height; // Increment y this amount per pixel
var x = -w / 2; // Start x at -1 * width / 2
var y;

var r;
var theta;
var val;

let bw; //variable to store grayscale
let i;
let j;
let cols = width;
let rows = height;

for (i = 0; i < cols; i += 1) {
y = -h / 2;

for (j = 0; j < rows; j += 1) {
r = sqrt((x * x) + (y * y)); // Convert cartesian to polar
theta = atan2(y, x); // Convert cartesian to polar
// Compute 2D polar coordinate function
val = sin(n * cos(r) + 5 * theta); // Results in a value between -1 and 1
//var val = cos(r); // Another simple function
//var val = sin(theta); // Another simple function
bw = color(((val + 1) * 255) / 2);
index = 4 * (i + j * width);
pixels[index] = red(bw);
pixels[index + 1] = green(bw);
pixels[index + 2] = blue(bw);
pixels[index + 3] = alpha(bw);

y += dy;
}

x += dx;
}
updatePixels();
}