Hello, friends. I have a small P5 sketch that I want to convert to Processing. I’ve tried, I’ve tried but can’t bring it home. Getting frustrated. The problem happens mainly in one function that converts a string of 12 characters to an array of coefficients. P5 sketch:
// Converts a string of 12 characters to a coefficient which we do stuff with
// Declare global variables
var code = "CVQKGHQTPHTE";
var xmin = -4;
var xmax = 1;
var ymin = -0.8;
var ymax = 1.8;
var x0 = 0;
var y0 = 0;
var a;
function setup() {
createCanvas(600, 600);
background(255);
noLoop();
}
function draw() {
a = c2n(code); //call function to convert code to array of numbers
for (i = 0; i < 100; i++) {
x0 = random(xmin, xmax);
y0 = random(ymin, ymax);
makeCircles(10);
}
} //draw
function c2n(c) {
var coeffs = [];
var ab = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ];
for (i = 0; i < c.length; i++) {
for (j = 0; j < ab.length; j++) {
if (c[i] === ab[j]) {
coeffs.push(round(j - 12)/10);
}
}
}
return coeffs; // returns the array of coefficients from the code
}
function makeCircles(iters) {
var x = x0;
var y = y0;
for (j = 0; j < iters; j++) {
var xt = x;
x = a[0] + a[1]*x + a[2]*x*x + a[3]*x*y + a[4]*y + a[5]*y*y;
y = a[6] + a[7]*xt + a[8]*xt*xt + a[9]*xt*y + a[10]*y + a[11]*y*y;
ellipse(map(x, xmin, xmax, 0, width), map(y, ymin, ymax, height, 0), 15+j*5, 15+j*5);
}
}
Here is the Processing code so far:
// Converts a string of 12 characters to a coefficient which we do stuff with
// Declare global variables
String code = "CVQKGHQTPHTE";
float xmin = -4;
float xmax = 1;
float ymin = -0.8;
float ymax = 1.8;
float x0 = 0;
float y0 = 0;
float a;
void setup() {
size(600, 600);
background(255);
noLoop();
}
void draw() {
a = c2n(code); //call function to convert code to array of numbers
for (int i = 0; i < 100; i++) {
x0 = random(xmin, xmax);
y0 = random(ymin, ymax);
makeCircles(10);
}
} //draw
String c2n(String c) {
float [] coeffs;
String [] ab = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < ab.length; j++) {
if (c[i] == ab[j]) {
coeffs.push(round(j - 12)/10);
}
}
}
return coeffs; // returns the array of coefficients from the code
}
void makeCircles(int iters) {
float x = x0;
float y = y0;
for (int j = 0; j < iters; j++) {
float xt = x;
x = a[0] + a[1]*x + a[2]*x*x + a[3]*x*y + a[4]*y + a[5]*y*y;
y = a[6] + a[7]*xt + a[8]*xt*xt + a[9]*xt*y + a[10]*y + a[11]*y*y;
ellipse(map(x, xmin, xmax, 0, width), map(y, ymin, ymax, height, 0), 15+j*5, 15+j*5);
}
}
Thanks in advance. My head hurts.