Animation with 5 balls

I have tried this code to iterate an array in a circular way and assign the y values to five circles. But I suspect it is not the most efficient way. Are there other ways to do it?

int[] secuencia = {0, 5, 10, 15, 20, 25, 20, 15, 10, 5};
int contador = 0;

void setup(){
  size(200, 100);
  frameRate(30);
}

void draw(){
  background(200);
  fill(255, 0, 128);
  noStroke();
  for(int i = 0; i < 5; i++){
    if((contador-i)<0)
      ellipse(40+i*30, 50 - secuencia[contador - i + secuencia.length], 10, 10);
    else
      ellipse(40+i*30, 50 - secuencia[contador - i], 10, 10);
  }
  if(contador == (secuencia.length - 1))
    contador = 0;
  else
    contador++;
}
2 Likes

other yes, efficient ? BUT MORE FUN

int k=0,km = 5;

void setup() {
  size(300, 100);
  frameRate(5);
  println("use: mouseX and mouseY");
}

void draw() {
  background(200);
  fill(255, 0, 128);
  noStroke();
  for (int i = 0; i < 10; i++) circle(40+i*25,50+make_k(),10);
}

int make_k() {
  k += km;
  if ( k > mouseX/6)  km= -mouseY/10;
  if ( k <-mouseX/6 ) km = mouseY/10;
  return k;
}

2 Likes

I needed a spreadsheet to understand what was happening. Converts continuous values given by mouseX and mouseY into discrete values, and change sign periodically. Amazing! I liked it a lot! I did not understand why sometimes the balls stopped, but it seems that it is because the series of data of a cycle coincides with the number of balls, therefore it reassigns the same values to the same balls.

Fantastic!

I’ll keep it. Thank you.

1 Like

sorry, should have been more user friendly
( i like you used a spread sheet BUT try)

int k=0,km = 5;
boolean diagp = true;

void setup() {
  size(300, 100);
  frameRate(5);
  println("use: mouseX and mouseY");
}

void draw() {
  background(200);
  fill(255, 0, 128);
  noStroke();
  for (int i = 0; i < 10; i++) circle(40+i*25,50+make_k(i),10);
  if ( diagp ) println("");
}

int make_k(int i) {
  k += km;
  if ( diagp ) println("i "+i+" k "+k+" km "+km+" mouseX "+mouseX+" mouseY "+mouseY);
  if ( k > mouseX/6)  km= -mouseY/10;
  if ( k <-mouseX/6 ) km = mouseY/10;
  return k;
}

1 Like

Use %.

In two places:

  1. to wrap your counter around every time it gets longer than the sequence length.
  2. to wrap your values counter+1, counter+2, counter+3… around any time they get longer than the sequence length.

This gets rid of both of your if/else blocks – you don’t need to check if the numbers are longer than secuencia.length; they never are.

int[] secuencia = {0, 5, 10, 15, 20, 25, 20, 15, 10, 5};
int contador = 0;

void setup(){
  size(200, 100);
  frameRate(30);
}

void draw(){
  background(200);
  fill(255, 0, 128);
  noStroke();
  for(int i = 0; i < 5; i++){
    ellipse(40+i*30, 50 - secuencia[(contador+i)%secuencia.length], 10, 10);
  }
  contador = (contador+1)%secuencia.length;
}
4 Likes

Very ingenious and more efficient than using conditionals.

Thank you.