We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Simple example of using equations for animating.
/**
*
* Some pulsing equations
*
**/
int eqIndex = 0;
float x;
String[] equations = {
"sin(t)",
"cos(t)",
"cos(t)*sin(t)",
"sin(t)*sin(t*1.5)",
"sin(tan(cos(t)*1.2))",
"sin(tan(t)*0.05)",
"cos(sin(t*3))*sin(t*0.2)",
"sin(pow(8,sin(t)))",
"sin(exp(cos(t*0.8))*2)",
"sin(t-PI*tan(t)*0.01)",
"pow(sin(t*PI),12)",
"cos(sin(t)*tan(t*PI)*PI/8)",
"sin(tan(t)*pow(sin(t),10))",
"cos(sin(t*3)+t*3)",
"pow(abs(sin(t*2))*0.6,sin(t*2))*0.6"
};
PFont f;
void setup() {
size(500, 500, P2D);
smooth();
f = createFont("arial", 20);
textAlign(LEFT);
frameRate(300);
background(0);
}
void draw() {
noStroke();
fill(0, 10);
rect(0, 0, width, height -150);
fill(0);
rect(0, height - 50, width, height);
float t = millis() * 0.15;
textFont(f, 15);
fill(200);
float sizeChange = getEq(eqIndex, t, 200);
fill(220);
ellipse(34.5, height - 37, 20, 20);
fill(0);
text(nf((eqIndex + 1), 2), 26, height -32);
fill(150);
textSize(16);
String txt = "press any key for next equation ";
text(txt, width - textWidth(txt), height -12);
fill(200);
textSize(20);
text(equations[eqIndex], 54, height -31);
fill(255);
ellipse(width/2, height/2 - 100, sizeChange, sizeChange);
stroke(220, 0, 0);
point((x+=0.1), 400 + sizeChange/5);
stroke(80, 0, 0);
line(0,400,width, 400);
if (x >= width) {
noStroke();
fill(0);
rect(0, height-150, width, height);
x=0;
}
}
void keyReleased()
{
eqIndex = (eqIndex+1)%15;
noStroke();
fill(0);
rect(0, height -250, width, height);
x=0;
//println(eqIndex);
}
float getEq(int eqIndex, float value, float factor)
{
float result = -1;
//float factor = 100;
float t = radians(value);
switch (eqIndex)
{
case 0:
result = sin(t)*factor;
break;
case 1:
result = cos(t)*factor;
break;
case 2:
result = cos(t)*sin(t)*factor;
break;
case 3:
result = sin(t)*sin(t*1.5)*factor;//
break;
case 4:
result = sin(tan(cos(t)*1.2))*factor;
break;
case 5:
result = sin(tan(t)*0.05)*factor;
break;
case 6:
result = cos(sin(t*3))*sin(t*0.2)*factor;
break;
case 7:
result = sin(pow(8, sin(t)))*factor;
break;
case 8:
result = sin(exp(cos(t*0.8))*2)*factor;
break;
case 9:
result = sin(t-PI*tan(t)*0.01)*factor;
break;
case 10:
result = pow(sin(t*PI), 12)*factor;
break;
case 11:
result = cos(sin(t)*tan(t*PI)*PI/8)*factor;
break;
case 12:
result = sin(t-PI*tan(t)*0.01)*factor;
break;
case 13:
result = cos(sin(t*3)+t*3)*factor;
break;
case 14:
result = pow(abs(sin(t*2))*0.6, sin(t*2))*0.6*factor;
break;
}
return result;
}
Comments
I think there is a script lib outthere by quark which could deliver the same...
I think it delivers much more : ), my point is more the visualisation of some equations that delivers cool effects :)
I was confused...
thanks for sharing!
;-)
Really nice, just shows you can have fun with maths.
I think my favourite was number 6 (case 5).
You don't need QScript for this, but if used it would enable users to try out their own functions without changing the source code.
Good idea, I'll give it a try.
If you intend to use QScript I strongly recommend you look at my website because the library and how to use it is fully explained there.
A more compact
switch/case
: =:)ScriptEngine "javascript" version replacing
switch/case
: (*)Cool! ;)
Made the conversion for p5js: http://p5js.org/reference/
Had some bugs though:
angleMode(DEGREES)
is buggy and doesn't work w/ the same precisionas explicitly converting to radians()!
0xff0080F0
or-1
for example. Gotta be 3 arguments or an array!In order to run it, just go to some of those reference functions and click "edit" and paste there! :bz
Well, nothing to do... made the CS version too: ~:>
That's really cool. Thanks for sharing. It's a shame that there is no syntax highlight for CS mode...
hey @GoToLoop good finds with the bugs, do you mind posting them here for any that aren't already logged? will try to fix as soon as we can. https://github.com/lmccart/p5.js/issues
Just to let every1 know that the mystery surrounding the discrepancy between angleMode() RADIANS & DEGREES was solved here: https://github.com/lmccart/p5.js/issues/383
Thanks for the sharing and the improvement.
Hey there, I'm pretty new to processing but is there any possibility of getting one of the disered animations at it's own to put it into another sketch? That would be great.
Comment out Java Mode's line #108 within keyTyped():
idx = (idx+1) % EQUATIONS.length;
Then @ line #21, initialize idx to a chosen equation's index.
Since this discussion was first posted I have released the Jasmine library and this would be better than Qscript in this scenario, at least for Java mode.
Would js.eval be able to do rect(.....?
@gotoloop ?
From: https://forum.Processing.org/two/discussion/15151/how-to-convert-string-to-a-line-of-code
Indeed, that's a good question, given that in order to invoke rect() we need sketch's PApplet reference!
But I guess if we use method put() the same way I did for variable t:
js.put("t", radians(value));
http://docs.Oracle.com/javase/8/docs/api/javax/script/ScriptEngine.html#put-java.lang.String-java.lang.Object-
We can also pass sketch's reference to the ScriptEngine: *-:)
WARNING: Not tested yet!!!
Thanks....
Gonna re-post my Nashorn JS + P5 Integration in this thread as well: :-j
https://forum.Processing.org/two/discussion/15151/how-to-convert-string-to-a-line-of-code