rotating circles

I'm trying to make a function which can be called to draw multiple circles rotating at different directions and with variable radii. The problem is that the circle's X and Y positions don't change, I've tried removing the code from the function and just placing it in the main and it works fine. (after i declare the variables of course). side question: why is the radius of rotation not equal the variable r? i'm assuming it has something to with the rate of change of the cos(inc)/sin(inc), but i'm not sure what exactly the relation is. here it is:

void setup ()
{size(500,500);
  }

void draw ()
{ 
  background(255); 
  circle(250,20,10,0.1,0.1);
}
void circle(float x,float y, float r, float pos, float step)
{
  fill(0);
  ellipse(x,y,25,25);
  x+=r*cos(pos);
  y+=r*sin(pos);
  pos+=step;
}

Answers

  • edited May 2014 Answer ✓

    draw() runs 60 times per second

    you give x and y as a parameter to the function and thus the old x and y are overwritten (or in fact forgotten since they only exist while the function circle runs: They are as parameters only locale variables)

    you could define those variables with a global scope (at sketch level). Thus the changes would remain permanent.

    [EDITED] you also mixed up the formula a bit - I corrected that now. (making what I said before mainly invalid except for pos)

    // circle center 
    float x=250;
    float y=250;
    
    // that is added to the angle
    float pos=0.1;
    
    void setup ()
    {
      size(500, 500);
    }
    
    void draw ()
    { 
      background(255); 
      circle(100, 0.1);
    }
    void circle( float r, float step)
    {
      fill(0);
    
      line (x, y, x+r, y+0);
    
      float x1 = x +  r*cos(pos);
      float y1 = y +  r*sin(pos);
    
      ellipse(x1, y1, 25, 25);
    
    
      stroke(255, 2, 2);
    
      pos+=step;
    }
    

    ;-)

  • edited June 2014

    I think I got it :D, thank you.

Sign In or Register to comment.