We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Index › A bunch of simple (silly) questions
 Pages: 1
 A bunch of simple (silly?) questions (Read 1794 times)
 zaphod A bunch of simple (silly?) questions Apr 28th, 2005, 2:05am   All this code came from REAS' nonlinear motion example here on Processing.org.  I switched some of it around and got rid of a whole lot of pieces while I was trying to take it apart, but it is his code.  And I know I'm asking some stupid questions, but I'm new to programming in general, so bear with me.int size = 8; int length = 20; int aSize;           // Height of the shape float aPos;          // Position of shape float aSpeed;        // Speed of the shape int aDirection = 1;  // Left or Right void setup() {  size(200, 200); //  noStroke();  framerate(60);  aPos = width/2;  } void draw() {  background(102);  fill(255);  aSpeed = hump(aPos/float(width-size));  aPos = aPos + ((aSpeed+.5) * aDirection );  if (aPos > width-length/2 || aPos < length/2) {    aDirection *= -1; }  ellipse(aPos, width/2, length, length); } float hump(float sa) {  sa = (sa - 0.5) * 2; //scale from -2 to 2  sa = sa*sa;  if(sa > 1) { sa = 1; }  return 1-sa; } I've been trying to understand why this works for a while, and I'm just not getting it.  Can someone describe to me what's happening with the "aSpeed=hump()" and float "hump(float sa)" lines?  I think hump() gets plugged in as "sa" and then the return function tells the program that it's done calculating aSpeed, but I'm confused as to what's actually being calculated.  What does the function "float hump(float sa)" actually do to the speed?  Does (width-size) translate to the width of the display(200) and the previously defined size variable(8 )? My problem there is that the "size" function is also called to define the length and width of the display..And why does the object simply run up against the side of the display and not go anywhere when i remove the +0.5 from ((aSpeed+0.5)*aDirection)? The IF statement in void draw() also uses the operator *= which I've never seen before.  does it make sure only to assign an integer?Yeah, it's a lot of questions, and yeah, it's a simple problem, but I'd appreciate any help you could give me.  Thanks - zaphod
 REAS Re: A bunch of simple (silly?) questions Reply #1 - Apr 28th, 2005, 2:13am   here's part of an answer:"a *= 5" is the same as "a = a * 5"it's a way of condensing the code.
 zaphod Re: A bunch of simple (silly?) questions Reply #2 - Apr 28th, 2005, 2:20am   ok, thanks.  I also just confused myself a bunch more by trying to work through the process on paper.  If that first line of the behaviour: aSpeed=hump(aPos/float(width-size)) translates to hump((200/2)/float(200-8 )), and then the numbers get plugged into hump(float sa) as "sa", then the hump function works out to 0?!  Because sa ends up being less than one and the function returns 1-sa... so... why does this do anything at all?