xxxxxxxxxx
121
function make2DArray(rows, cols) {
var arr = new Array(rows); //like arr[]; but with number of columns hardcoded
for (var i = 0; i < arr.length; i++) {
arr[i] = new Array(cols);
}
return arr;
}
let angle = 0;
let w = 100;
let cols;
let rows;
let curves;
function setup() {
createCanvas(800, 800);
cols = floor(width / w) - 1;
rows = floor(height / w) - 1;
curves = make2DArray(rows, cols);
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
curves[j][i] = new Curve();
}
}
}
function draw() {
background(220);
let d = w - 0.2 * w;
let r = d / 2;
// horizontal circles
stroke(0);
noFill();
for (let i = 0; i < cols; i++) {
// circles
let cx = w + i * w + w / 2;
let cy = w / 2;
strokeWeight(1);
stroke(0);
circle(cx, cy, d);
// points on circles
x = r * cos(angle * (i + 1) - HALF_PI);
y = r * sin(angle * (i + 1) - HALF_PI);
strokeWeight(8);
stroke(0);
point(cx + x, cy + y);
// animated line
//strokeWeight(1);
//stroke(0, 50);
//line(cx + x, 0, cx + x, height);
// add point to curves
for (let j = 0; j < rows; j++) {
curves[j][i].setX(cx + x);
}
}
// vertical circles
stroke(0);
noFill();
for (let j = 0; j < rows; j++) {
// circles
let cx = w / 2;
let cy = w + j * w + w / 2;
strokeWeight(1);
stroke(0);
circle(cx, cy, d);
// points on circles
x = r * cos(angle * (j + 1) - HALF_PI);
y = r * sin(angle * (j + 1) - HALF_PI);
strokeWeight(8);
stroke(0);
point(cx + x, cy + y);
// animated line
//strokeWeight(1);
//stroke(0, 50);
//line(0, cy + y, width, cy + y);
// add point to curves
for (let i = 0; i < rows; i++) {
curves[j][i].setY(cy + y);
}
}
// add point to curve and draw
if(angle < TWO_PI)
{
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
curves[j][i].addPoint();
curves[j][i].show();
}
}
}
// delete point
else if(angle < 2* TWO_PI && angle > TWO_PI)
{
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
curves[j][i].removePoint();
curves[j][i].show();
}
}
}
else
{
angle = 0;
}
angle += 0.02;
}