xxxxxxxxxx
// p5.func examples - arrayeval2_curve
// I<3DM rld
/*
eval(evalstr, l1), eval1d(evalstr, l1):
evaluate a evalstr into a one-dimensional Array.
If evalstr is an Array, each element in the Array will fill with more than one value.
l1 is the length of the Array to return.
The keywords u, su, cu, and du will be expanded to a normal map, a signed normal map, a cell position (0 to l1-1), and the length of the array (l1).
*/
var e = new p5.ArrayEval();
var l = 2000; // length of the array returned
var wstep, hstep; //
var t; // array
var z = 55; // zoom factor in pixels
// array to store all the equations
var s = [];
// some equations from paul bourke
s[0] = [ '-Math.sinh(2*su*HALF_PI) / (cos(2*4*su*HALF_PI) - Math.cosh(2*su*HALF_PI))',
'sin(2*4*su*HALF_PI) / (cos(2*4*su*HALF_PI) - Math.cosh(2*su*HALF_PI))'
]; // http://paulbourke.net/geometry/cothspiral/
s[1] = [ '9*0.25*cos(u*TWO_PI) + 0.25*cos(9*u*TWO_PI)',
'9*0.25*sin(u*TWO_PI) + 0.25*sin(9*u*TWO_PI)'
]; // http://paulbourke.net/geometry/hypocycloid/
s[2] = [ 'cos(u*PI*24.) * (exp(cos(u*PI*24.)) - 2 * cos(4 * u*PI*24.) - pow(sin(u*PI*24. / 12),5.0))',
'sin(u*PI*24.) * (exp(cos(u*PI*24.)) - 2 * cos(4 * u*PI*24.) - pow(sin(u*PI*24. / 12),5.0))'
]; // http://paulbourke.net/geometry/butterfly/
s[3] = [ '2*cos(u*TWO_PI)+cos((3-1)*u*TWO_PI)',
'2*sin(u*TWO_PI)+sin((3-1)*u*TWO_PI)'
]; // http://paulbourke.net/geometry/cardioid/
s[4] = [ '(5.*(1. + sin(11. * u*21.*PI / 5.)) - \
4. *\
sin4(17. * u*21.*PI / 3.) *\
sin8(2. * cos(3. * u*21.*PI) - 28. * u*21.*PI)) *\
cos(u*21.*PI)*0.3',
'(5.*(1. + sin(11. * u*21.*PI / 5.)) - \
4. *\
sin4(17. * u*21.*PI / 3.) *\
sin8(2. * cos(3. * u*21.*PI) - 28. * u*21.*PI)) *\
sin(u*21.*PI)*0.3'
]; // http://paulbourke.net/geometry/chrysanthemum/
var current = 0;
function setup()
{
createCanvas(windowWidth, windowHeight);
textSize(12);
noLoop();
}
function draw()
{
t = e.eval(s[current], l);
resetMatrix();
background(255);
stroke(128, 128, 255);
noFill();
translate(width/2, height/2); // put 0, 0 at center
beginShape();
for(var i = 0;i<t.length;i++)
{
vertex(t[i][0]*z-z/2, t[i][1]*z-z/2);
ellipse(t[i][0]*z-z/2, t[i][1]*z-z/2, 5, 5);
}
endShape();
resetMatrix();
fill(0);
noStroke();
text('var e = new p5.ArrayEval()', width/6, height/3);
text('var s = \'' + s[current] + '\';', width/6, height/3+50);
text('var t = e.eval(s, ' + l + ');', width/6, height/3+100);
text('press any key to switch equations.', width/6, height/3+150);
current = (current+1)%s.length;
}
function keyTyped()
{
redraw();
}
// sin4 function
sin4 = function(_x) {
return(sin(_x)*sin(_x)*sin(_x)*sin(_x));
}
// sin8 function
sin8 = function(_x) {
return(sin(_x)*sin(_x)*sin(_x)*sin(_x)*sin(_x)*sin(_x)*sin(_x)*sin(_x));
}