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.
IndexProgramming Questions & HelpSyntax Questions › Animating an ellipse()
Page Index Toggle Pages: 1
Animating an ellipse() (Read 839 times)
Animating an ellipse()
Sep 6th, 2007, 1:57pm
 
Hi,

I've written a short piece of code which checks when a mouse button is pressed and is supposed to animate an ellipse growing from the mouseX and mouseY positions.

To do this I've used a recursive function that should draw an ellipse every 100 milliseconds.  Unforunately I'm getting stack overflows and I'm not sure why.  Can anyone suggest where I'm going wrong?  The code is printed below:

void setup() {
 background(0);
 fill(255);
 stroke(255);
}

void draw(){
}

void mousePressed(){
 generateCircle(mouseX,mouseY,millis(),50,0,5);
}

void generateCircle(int x, int y, int clickedTime, int maxSize, int startSize, int intervalSize){
 // check if the ellipse has reached its maximum size
 if(startSize < maxSize){
   // check if 100 milliseconds have passed, if they haven't
   // then throw the same vars back into the function
   if(millis() < clickedTime + 100){
     generateCircle(x, y, clickedTime, maxSize, startSize, intervalSize);
   }
   // if 100 ms have passed then draw an ellipse
   // of given size and then recall the function
   else{
     ellipse(x, y, startSize + intervalSize, startSize + intervalSize);
     int newSize = startSize + intervalSize;
     generateCircle(x, y, millis(), maxSize, newSize, intervalSize);
   }
 }
}

Thanks!
Re: Animating an ellipse()
Reply #1 - Sep 6th, 2007, 2:39pm
 
stack overflow means that the call(s) get sooo deep (many) that the call-stack is full. you are generating a tight loop there which almost instantly filles the stack (far faster than 100 millis).

anyway, your design is wrong. you should give processing (papplet / your sketch) some time draw things and not make it be cought in a thight loop.

here's how i'd do it:
Code:


Vector ellipses; // Vector is an array that can grow easily

void setup() {
background(0);
fill(255);
stroke(255);
ellipses = new Vector();
}

void draw()
{
for ( int i = 0; i<ellipses.size(); i++ )
{
((Ellipse)ellipses.get(i)).draw();
}
}

void mousePressed()
{
ellipses.add( new Ellipse( mouseX,mouseY,millis(),50,0,5 ) );
}

class Ellipse
{
int x,y;
int clickedTime;
int maxSize;
int startSize;
int intervalSize;
int fColor;

Ellipse (int _x, int _y, int _clickedTime, int _maxSize, int _startSize, int _intervalSize)
{
x = _x; y = _y;
clickedTime = _clickedTime;
maxSize = _maxSize;
startSize = _startSize;
intervalSize = _intervalSize;

fColor = color( random(255), random(255), random(255) );
}

void draw()
{
if(millis() < clickedTime + 100) return; // do nothing

fill( fColor );
ellipse(x, y, startSize + intervalSize, startSize + intervalSize);
startSize += intervalSize;
clickedTime = millis();
}
}


F
Re: Animating an ellipse()
Reply #2 - Sep 6th, 2007, 4:41pm
 
Thats excellent fjen, thankyou.

I understand the basic premise of what you've done with the code, which is to say, created an Ellipse class which has a function to draw the ellipse on the screen.  I understand why this achieves my goal better than my original idea.

I have a couple of questions, though.

First, do you know where I can find out more about the Vector array you create at the beginning of the code?  I cannot find it in the reference documentation.

Secondly, why is it that you are able to have two draw() functions in the code?  The processing reference is quite explicit that draw() can only exist once per sketch.

Thanks again.
Re: Animating an ellipse()
Reply #3 - Sep 6th, 2007, 5:16pm
 
(1)

Vector is standard java:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

since Processing inherits from java you can use that.

(2)

yes, only one draw() method (without arguments) allowed per sketch. since the "second" draw function is embedded into a class (Ellipse) it's legal. i could have called it drawEllipse() instead to make it clearer that it's not "the draw()" of papplet (sketch class).
note that the coloring of Ellipse.draw() is wrong. Processing's coloring code is not context-aware and therefore (wrongly) thinks it's the standard draw() function.

i could have gotten the same results from code not using any additional classes (using multiple arrays for example). but in this case it makes sense to group the variables and the drawEllipse() together. maybe look for some introduction to object oriented programming ..

F
Re: Animating an ellipse()
Reply #4 - Oct 9th, 2007, 4:23am
 
Thanks fjen,
this is just what I needed!!!
Re: Animating an ellipse()
Reply #5 - Oct 9th, 2007, 7:52pm
 
BTW, if performance is an issue you should use ArrayList instead of Vector, because the Vector class is synchronized and takes a speed hit as a result.  I'm pretty sure that as far as methods and all that, ArrayList is identical to Vector, but it should run a little faster (maybe this will change with the next generation of JVMs, which I've heard might be able to automagically optimize away unnecessary synchronizations, but I'll believe it when I see it...).
Page Index Toggle Pages: 1