# Conditions to make a circle grow and shrink

**URL:** https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698
**Category:** Beginners
**Created:** [January 15, 2024, 9:42pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698 "2024-01-15T21:42:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)
#### Post date: [January 15, 2024, 9:42pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698/1 "2024-01-15T21:42:28Z")

</div>

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:

```auto
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--;
    }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 15, 2024, 9:59pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698/2 "2024-01-15T21:59:42Z")

</div>

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;`

---

<div class="post-metadata">

### Author: ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)
#### Post date: [January 15, 2024, 10:07pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698/3 "2024-01-15T22:07:46Z")

</div>

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

This would be the final code?

```auto
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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 15, 2024, 10:19pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698/4 "2024-01-15T22:19:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Karrajo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/karrajo/32/14745_2.png) [@Karrajo](https://discourse.processing.org/u/Karrajo)
#### Post date: [January 15, 2024, 10:30pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698/5 "2024-01-15T22:30:02Z")

</div>

Yeah, sorry.

So this is the solution:

```auto
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

---

<div class="post-metadata">

### Author: ![theapplesguy2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/theapplesguy2/32/19754_2.png) [@theapplesguy2](https://discourse.processing.org/u/theapplesguy2)
#### Post date: [July 3, 2024, 1:45pm UTC](https://discourse.processing.org/t/conditions-to-make-a-circle-grow-and-shrink/43698/6 "2024-07-03T13:45:32Z")

</div>

you could do something like this

```auto
void setup(){
  size(400,400);
  float g = 0;
}

float g = 0;
void draw(){
  float f = (50+(sin(g)*5));
  g=g+0.1;
  background(56);
  ellipse(200,200,f,f);
}

```
