# Create bugs inside of an ellipse

**URL:** <https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599>\
**Category:** Coding Questions\
**Created:** [June 1, 2018, 1:24pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599 "2018-06-01T13:24:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 1, 2018, 1:24pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/1 "2018-06-01T13:24:34Z")

</div>

So, i have an array of “bugs” and want to make them appear inside of an ellipse and constrain them there until i release them (mouse click, probably).

My struggle right now is to make them appear inside of the ellipse.

Main Code:

```auto
// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;

PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[5];

float raioCircunferencia;
float x;
float y;

Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(2000, 2000);
  smooth();
  raioCircunferencia = 1000;
  
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = width/2+cos(a)*random(raioCircunferencia);
    x = height/2+sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  
  stroke(1);
  fill(255);
  ellipse(width/2,height/2,raioCircunferencia,raioCircunferencia);
  
  float som = in.mix.level();
  
  fill(255,30);
  rect(0,0,width,height);
  
  
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].move(som);
    bugs[i].display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

```

Class Bug

```auto
class Bug {

  color c;
  color bugColor;
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    if(tx <= 0 || tx >= width || ty <= 0 || ty >= height){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    if (x <= width/2) {
      bugColor = color(255, 0, 0);
    } 
    else {
      bugColor = color(0, 0, 255);
    }
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 1, 2018, 1:39pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/2 "2018-06-01T13:39:20Z")

</div>

It doesn’t work ?

```auto
for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = width/2+cos(a)*random(raioCircunferencia);
    x = height/2+sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }

```

By the way you cloud say :

```auto
for(Bug b : bugs){
  b.move(som);
  b.display();
}

```

instead of :

```auto
for (int i = 0; i < bugs.length; i++) {
    bugs[i].move(som);
    bugs[i].display();
}

```

---

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 1, 2018, 4:18pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/3 "2018-06-01T16:18:40Z")

</div>

That’s my code, right? It should, shouln’t it?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 1, 2018, 4:39pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/4 "2018-06-01T16:39:37Z")

</div>

Do bugs appear inside of the ellipse?

---

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 1, 2018, 4:43pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/5 "2018-06-01T16:43:13Z")

</div>

Nop… 😕  
Maybe it’s something wrong with the Bug Class?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 1, 2018, 5:00pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/6 "2018-06-01T17:00:10Z")

</div>

Maybe this ? :

```auto
// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;

PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[5];

float raioCircunferencia = 500;
float x;
float y;

Minim minim;
AudioInput in;

Capture cam;

void setup() {
  fullScreen();
  smooth();
  
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = cos(a)*random(raioCircunferencia);
    y = sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  background(255,30);
  translate(width/2,height/2);
  stroke(1);
  fill(255);
  ellipse(0,0,raioCircunferencia*2,raioCircunferencia*2);
  
  float som = in.mix.level();
  
  
  for (Bug b : bugs) {
    b.move(som);
    b.display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

class Bug {

  color c;
  color bugColor;
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    if(tx <= -raioCircunferencia || tx >= raioCircunferencia || ty <= -raioCircunferencia || ty >= raioCircunferencia){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    if (x <= 0) {
      bugColor = color(255, 0, 0);
    } 
    else {
      bugColor = color(0, 0, 255);
    }
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

```

I added `translate(width/2,height/2);` and raioCircunferencia in the parameters.

---

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 1, 2018, 5:40pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/7 "2018-06-01T17:40:33Z")

</div>

It kinda worked, but i change the values like this it stops working, now the next step is constraining the bugs inside the ellipse. Can i do that with the dist function on the b.move()?

I changed the fullscreen for size(1000,1000); and made radius of width/2

```auto
// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;

PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[5];

float raioCircunferencia;
float x;
float y;

Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(1000,1000);
  smooth();
  raioCircunferencia = width/2;
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = cos(a)*random(raioCircunferencia);
    y = sin(a)*random(raioCircunferencia);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  background(255,30);
  translate(width/2,height/2);
  stroke(1);
  fill(255);
  ellipse(0,0,raioCircunferencia,raioCircunferencia);
  
  float som = in.mix.level();
  
  
  for (Bug b : bugs) {
    b.move(som);
    b.display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

class Bug {

  color c;
  color bugColor;
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    if(tx <= -raioCircunferencia || tx >= raioCircunferencia || ty <= -raioCircunferencia || ty >= raioCircunferencia){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    if (x <= 0) {
      bugColor = color(255, 0, 0);
    } 
    else {
      bugColor = color(0, 0, 255);
    }
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 1, 2018, 7:15pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/8 "2018-06-01T19:15:28Z")

</div>

> [@RafaelAzevedo](#):
>
> Can i do that with the dist function on the b.move()?

Yes you can do (in the function move) :

```auto
    float distance = dist(tx,ty,0,0);
    
    if(distance >= raioCircunferencia){
      tx = x;
      ty = y;
    }

```

And you have to say :

```auto
ellipse(0,0,raioCircunferencia*2,raioCircunferencia*2);

```

---

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 1, 2018, 10:05pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/9 "2018-06-01T22:05:16Z")

</div>

> [@josephh](#):
>
> float distance = dist(tx,ty,0,0); if(distance \>= raioCircunferencia){ tx = x; ty = y; }

So i got this running and working! Thanks!  
Another question, can i make so if i press the mouse and make the ellipse appear, the bugs that are outside pop and a new one is created?

```auto
// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;

PImage imgBug;
// Array de Bugs
Bug[] bugs = new Bug[50];

float raioCircunferencia;
float meioRaio;
float x;
float y;
int click = 0;

Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(2000, 2000);
  smooth();
  raioCircunferencia = 1000;
  meioRaio = raioCircunferencia/2;
  
  background(255);
  
  for (int i = 0; i < bugs.length; i++) {
    float a = random(TWO_PI);
    
    x = width/2+cos(a)*random(meioRaio);
    y = height/2+sin(a)*random(meioRaio);
    
    float r = random(50,200);
    color c = color(int(random(255)),int(random(255)),int(random(255)));
    
    bugs[i] = new Bug(c, x, y, r);
  }
  
  
  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
  
}

void draw() {
  
  if(click == 0){
    stroke(0);
    strokeWeight(10);
    fill(255,30);
    ellipse(width/2,height/2,raioCircunferencia,raioCircunferencia);
  }
  
  float som = in.mix.level();
  
  fill(255,30);
  rect(0,0,width,height);
  
  
  for (int i = 0; i < bugs.length; i++) {
    bugs[i].move(som);
    bugs[i].display();
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

class Bug {

  color c;
  color bugColor = color(random(int(255)),random(int(255)),random(int(255)));
  float x;
  float y;
  float r;
  float speed = 2.50;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;
    
    tx = x;
    ty = y;
    
    speed = map(audio, 0.00001, 1, 10, 30);
    
    tx += random(-speed, speed);
    ty += random(-speed, speed);
    
    float distance = dist(tx,ty,width/2,height/2);
    
    if (click == 0){
      if(distance >= meioRaio){
        tx = x;
        ty = y;
      }
    }
    
    
    if(tx <= 0 || tx >= width || ty <= 0 || ty >= height){
      tx = x;
      ty = y;
    }
    
    x = tx;
    y = ty;
    
  }

  void display() {
    //if (x <= width/2) {
    // bugColor = color(255, 0, 0);
    //} 
    //else {
    // bugColor = color(0, 0, 255);
    //}
    
    //fill(bugColor);
    
    fill(bugColor);
    noStroke();
    ellipse(x, y, r, r);
  }
}

void mousePressed(){
  click ++;
  if(click > 1) click = 0;
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 2, 2018, 10:17am UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/10 "2018-06-02T10:17:43Z")

</div>

> [@RafaelAzevedo](#):
>
> Another question, can i make so if i press the mouse and make the ellipse appear, the bugs that are outside pop and a new one is created?

Yes, you should use : [ArrayList / Reference / Processing.org](https://processing.org/reference/ArrayList.html)  
And do :

```auto
bugs.remove(i);

```

if the distance is higher than raiocircunferencia when the mouse is pressed.

---

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 2, 2018, 12:17pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/11 "2018-06-02T12:17:51Z")

</div>

Ok, got that part working…

Getting the IndexOutOfBoundsException, i’m making the loop backwards so i don’t skip any index.

```auto
// Importar Bibliotecas
import ddf.minim.*;
import processing.video.*;

PImage imgBug;
// Array de Bugs
ArrayList<Bug> arrayBugs;

float raioCircunferencia;
float meioRaio;
float x;
float y;
int numBugs = 100;
int click = 0;

Minim minim;
AudioInput in;

Capture cam;

void setup() {
  size(2000, 2000);

  smooth();

  raioCircunferencia = 1000;
  meioRaio = raioCircunferencia/2;

  arrayBugs = new ArrayList<Bug>();

  background(255);

  for (int i = numBugs-1; i >= 0; i--) {
    float a = random(TWO_PI);

    x = width/2+cos(a)*random(meioRaio);
    y = height/2+sin(a)*random(meioRaio);

    float r = random(50, 200);
    color c = color(int(random(255)), int(random(255)), int(random(255)));

    arrayBugs.add(new Bug(c, x, y, r));
  }

  // SOM 
  minim = new Minim(this);
  minim.debugOn();
  in = minim.getLineIn(Minim.STEREO, 2048);
}

void draw() {

  if (click == 0) {
    stroke(0);
    strokeWeight(10);
    fill(255, 30);
    ellipse(width/2, height/2, raioCircunferencia, raioCircunferencia);
  }

  float som = in.mix.level();
  noStroke();
  fill(255, 30);
  rect(0, 0, width, height);

  for (int i = numBugs-1; i >= 0; i--) {
    Bug b = arrayBugs.get(i);
    b.move(som);
    b.display();
    if(b.morre()){
      arrayBugs.remove(i);
    }
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  super.stop();
}

class Bug {

  color c;
  color bugColor = color(random(int(255)), random(int(255)), random(int(255)));
  float x;
  float y;
  float r;
  float speed = 2.50;
  int opacidade = 100;

  Bug (color cor, float posX, float posY, float raio) {

    c = cor;
    x = posX;
    y = posY;
    r = raio;
  }

  void move(float audio) {
    float tx, ty;

    tx = x;
    ty = y;

    float distance = dist(tx, ty, width/2, height/2);

    speed = map(audio, 0.00001, 1, 10, 30);

    tx += random(-speed, speed);
    ty += random(-speed, speed);

    if (click == 0) {
      if (distance > meioRaio) {
        tx = x;
        ty = y;
      }
    }

    if (tx <= 0 || tx >= width || ty <= 0 || ty >= height) {
      tx = x;
      ty = y;
    }

    x = tx;
    y = ty;
  }

  void display() {
    //if (x <= width/2) {
    // bugColor = color(255, 0, 0);
    //} 
    //else {
    // bugColor = color(0, 0, 255);
    //}

    //fill(bugColor);
    float tx, ty;

    tx = x;
    ty = y;

    float distance = dist(tx, ty, width/2, height/2);
    if (distance > meioRaio && click == 0) {
      fill(bugColor, opacidade);
      opacidade--;
    } else {
      fill(bugColor, 100);
    }

    noStroke();
    ellipse(x, y, r, r);
  }
  
  boolean morre() {
    if(opacidade < 0){
      return true;
    }
    else {
      return false;
    }
  }
}

void mousePressed() {
  click ++;
  if (click > 1) click = 0;
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [June 2, 2018, 6:06pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/12 "2018-06-02T18:06:08Z")

</div>

It is because you have to use `arrayBugs.size()` in the for loops not `numBugs` :

For example

```auto
for (int i = arrayBugs.size()-1; i >= 0; i--) {
    Bug b = arrayBugs.get(i);
    b.move(som);
    b.display();
    if(b.morre()){
      arrayBugs.remove(i);
    }
  }

```

But you use `numBugs` when you initialize your array of bugs.

---

<div class="post-metadata">

**Author:** ![RafaelAzevedo](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@RafaelAzevedo](https://discourse.processing.org/u/RafaelAzevedo)\
**Post date:** [June 2, 2018, 7:13pm UTC](https://discourse.processing.org/t/create-bugs-inside-of-an-ellipse/599/13 "2018-06-02T19:13:10Z")

</div>

Oh, ok! Thank you so very much!✌
