Conditions to make a circle grow and shrink

Hello,

Sorry because I know this is a very simple question but I’m stuck on this and I need help.

I’m trying to create the conditions to grow a circle up to a certain point, and when it reaches that point, make it smaller (also up to a certain point, and so on). I see is similar to other topics but I think this is a different way to make it.

“tamanyo” is the variable for the original size of the circle, and “a” is the variable I am using to grow and then shrink the circle.

This is my code:

float a=0;
float tamanyo=50;

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

void draw(){
  background(0);
  noFill();
  stroke(255);
  circle(width/2,height/2,tamanyo);
   tamanyo +=a;
    if (a<=0) {
      a++;
    }
   if (a == 20){
    a--;
    }
}

1st define global constants for max & min diameters of your circle():
static final int MIN_DIAM = 3, MAX_DIAM = 100;

2nd declare current diameter & growth speed variables:
float diam = MIN_DIAM, growth = .5;

Then when updating diam w/ growth, check if diam has reached either min or max diameters; if so negate the current growth speed:
if ( (diam += growth) <= MIN_DIAM || diam >= MAX_DIAM ) growth *= -1;

1 Like

Thank you, but I don’t understand the method or it’s not working:

This would be the final code?

float a=0;
float diam=50;
static final int MIN_DIAM = 3, MAX_DIAM = 100;
void setup(){
size(400,400);
}

void draw(){
  background(0);
  noFill();
  stroke(255);
  
  float diam = MIN_DIAM, growth = .5;
  
  circle(width/2,height/2,diam);
   diam +=a;
   if ( (diam += growth) <= MIN_DIAM || diam >= MAX_DIAM ) growth += -1;
}

I use 3.5.3. version of processing

Why are you re-declaring global variable diam as a local variable in draw()?

And in the same vein, why growth is a local variable in draw()?

Both diam & growth need to be global variables, so their current value is remembered while draw() is called back during the sketch execution.

Yeah, sorry.

So this is the solution:

float diam = MIN_DIAM, growth = .5;
static final int MIN_DIAM = 3, MAX_DIAM = 100;
void setup(){
size(400,400);
}

void draw(){
  background(0);
  noFill();
  stroke(255);
  
  circle(width/2,height/2,diam);
    if ( (diam += growth) <= MIN_DIAM || diam >= MAX_DIAM ) growth *= -1;
}

Thank you very much

2 Likes