Array interaction problem!

My question are:

  1. Why i’m getting 4 valour for every p[i], i should have 4 circle but i have 16!
  2. Why the “fase 1” ir rolling forever
  3. Why c[i] isnt getting any value

for now that’s it!

//Quantidade de circulo]
int ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos

  if (fase==0) {
    println("fase = " + fase);
    for (i=0; i<ct; i++) {
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      //ct - 1 para a quantidade de circulos sempre ser "ct^2"
      println("Valor de =" + p[i]);
      }
      if (cfase1 >= ct - 1) {
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println("fase = " + fase);
    for (i = 0; i < fact(ct); i++) {
      for (j = 0; j < ct; j++) {
        //Calculo entre duas distancias
        c[i] = sqrt(pow(p[j].x - p[j].x, 2) + pow(p[j].y - p[j].y, 2));
        println("Valor de c[i]" + c[i]);
      }
    }
        if (cfase2 >= ct - 1) {
          fase++;
        }
        cfase2++;
      } else if (fase==2){
        fase++;
        cfase3++;
      }
    }


1 Like

I was trying to fix it, oh boy, everything was wrong haha

Now i don’t know why i’m getting the error “ArrayIndexOutOfBoundsException”

//Quantidade de circulo]
int ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos

  if (fase==0) {
    println("fase = " + fase);
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      println("Valor de =" + p[i]);
      i++;
      if (cfase1 >= ct - 1) {
        fase++;
        i=0;
    }
    cfase1++;
  } else if (fase==1) {
    println("fase = " + fase);
        c[i] = sqrt(pow(p[i+1].x - p[i].x, 2) + pow(p[i+1].y - p[i].y, 2));
        println("Valor de c[i]" + c[i]);
        i++;
        if (cfase2 >= fact(ct) ){
          fase++;
      }
      cfase2++;
}
}

My problem now is on “fase1” i need to keep the operation rolling till c reaches at c[fact(ct)], but my counts have to work in a certain way, everytime i reaches j, i needs to be put on 0 and j needs to decrease by 1, but when there’s a time when j needs to jump 1 unit, and i don’t know how do to it…

//Quantidade de circulo]
int ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j = ct - 1;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos

  if (fase==0) {
    println("fase = " + fase);
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      println("Valor de =" + p[i]);
      i++;
      if (cfase1 >= ct - 1) {
        i=0;
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println("fase = " + fase);
        c[i] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
        println("Valor de c[i]" + c[i]);
        i++;
        if (cfase2 >= fact(ct) ){
          fase++;
      }
      cfase2++;
}
}

looks like you have 2 arrays ( c and p ) what have not the same length
but you use ONE index ( i ) for talk to both of them.
possibly you wanted to do like

c[j] ...
j++

also remember with this writing

p[i]..
i++

i points to a future index
so you can not use / read p[i] ( ok can but its 0 because not written, unless its already >= p.length )

you could however

int i = -1;

//...
i++;
if ( i >= p.length ) i = 0;
p[i]=...
//..
circle(p[i].x,p[i].y,5);

because now i point to the current / last set

1 Like

When i was on my university i figured it out, but now i’m having another problem, i read that this problem has the solution = n!, but now when doing it for n=4, the math doesnt count out, i only have k= 16, and it should be 23, since it has a -1, since my array start at 0

//Quantidade de circulo]
int ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j = ct - 1, k = fact(ct) -1;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos
  if (fase==0) {
    println("fase = " + fase);
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      println("Valor de =" + p[i]);
      if (i != j) i++;
      if (cfase1 >= ct - 1) {
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println("fase = " + fase);
        c[k] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
        println("Valor de c[k]" + c[k] + "  " + k + " " + j + " " + i);
        k--;
        i--;
        if( i < 0){
          j--;
          i = ct - 1;
        }
        
        if ( k == -1){
            fase++;
        }
        cfase2++;
}
}

Update: I made it run using “if(k==7) fase++” but i really don’t know why, i know that its because the way i’m doing it i’ll have c[c^t2], but i really don’t get why haha, my next “fase” will be to get the values on c[k] and make a line conecting the ellipses using the lowest distance between they, i’ll start from p[4] probably, too tired to keep going!

//Quantidade de circulo]
int ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j = ct - 1, k = fact(ct) -1;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos
  if (fase==0) {
    println("fase = " + fase);
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      println("Valor de =" + p[i]);
      if (i != j) i++;
      if (cfase1 >= ct - 1) {
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println("fase = " + fase);
        c[k] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
        println("Valor de c[k]" + "  " + c[k] + "  " + k + "  " + j + "  " + i);
        k--;
        i--;
        if( i < 0){
          j--;
          i = ct - 1;
        }
        //Não entendi porque 7 aqui, é para parar quando ele fizesse todas os cálculos de todas as distâncias!
        if ( k == 7){
            fase++;
        }
        cfase2++;
}
}

Math isn’t my strong suit so I can’t help you there, but each time k = 7, j happens to be -1. That’s the cause for the error. I cleaned up your code a bit and changed its output. Try running it and you’ll see what I mean:

int ct = 4, i, j = ct - 1, k = fact(ct) -1;
float sizeEllipse = 6, fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
float[] c = new float[fact(ct)];
PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
}

void draw() {
  
  if (fase == 0) {
    p[i] = new PVector(random(sizeEllipse, width - sizeEllipse), random(sizeEllipse, height - sizeEllipse));
    ellipse(p[i].x, p[i].y, sizeEllipse, sizeEllipse);
    if (i != j) i++;
    if (cfase1 >= ct - 1) {
      fase++;
    }
    cfase1++;
  } 
  
  if (fase == 1) {
    println(i, j, k);
    c[k] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
    k--;
    i--;
    if (i < 0) {
      j--;
      i = ct - 1;
    }
    if (k == 7) {
      fase++;
    }
    cfase2++;
    println(i, j, k);
    println();
  }
  
  if (fase == 2) {
    println("we're in fase 2, program paused");
    noLoop();
  }
}

static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}
2 Likes

Tiemen, i used some of your chances and also figured out why the k wasn’t working, but now i got another problem. I’m trying to get those values into my c[v-1] = 3j2i, 3j1i, 3j0i, 2j1i, 2j0i, 1j0i, but i’m having a problem to get those values.

I’m trying to make it jump when j and i has the same values, 3j3i, 2j2i, 1j1i, 0j0j, but i can’t find any method to work… still trying to figure it out

//Quantidade de circulo
int ct = 4;
//Quantidade de valores
int v = ((ct*ct) - ct)/2;
//Comprimento e Largura de Circulo
float tamanhoC = 6;
//Contador
int i, j = ct - 1, k = fact(ct) -1, l;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[v];
//O vector que vai receber a quantidade de vetores
PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculo
  if (fase==0) {
    println("fase = " + fase);
      p[i] = new PVector(random(tamanhoC, width - tamanhoC), random(tamanhoC, height - tamanhoC));
      ellipse(p[i].x, p[i].y, tamanhoC, tamanhoC);
      println("Valor de =" + p[i]);
      if (i != j) i++;
      if (cfase1 >= ct - 1) {
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println("fase = " + fase);
        i--;
        c[l] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
        println("Valor de c[k]" + "  " + c[l] + "  " + l + "  " + j + "  " + i);
        l++;
        if( i <= 0){
          j--;
          i = ct -1;
          if (j == i ) i--;
        if ( l >= v - 1){
            fase++;
        }
        }
        cfase2++;
}
if (fase == 2){
  println("Segunda fase, pausa");
  noLoop();
}
}

Omg, it was so simple that i can’t belive i was taking forever doing it… Now i need to see the value and make a line connecting the circles starting from the last circle p[3]

//Quantidade de circulo
int ct = 4;
//Quantidade de valores
int v = ((ct*ct) - ct)/2;
//Comprimento e Largura de Circulo
float tamanhoC = 6;
//Contador
int i, j = ct - 1, k = fact(ct) -1, l;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[v];
//O vector que vai receber a quantidade de vetores
PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculo
  if (fase==0) {
    println(" ");
      p[i] = new PVector(random(tamanhoC, width - tamanhoC), random(tamanhoC, height - tamanhoC));
      ellipse(p[i].x, p[i].y, tamanhoC, tamanhoC);
      println("Valor de =" + p[i]);
      if (i != j) i++;
      //Quando ela rodar a quantidade de vezes que for o J, ou seja, a quantidade de circulos -1 vai pra outra fase.
      if (cfase1 >= j) {
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println(" ");
        i--;
        c[l] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
        println("Valor de c[k]" + "  " + c[l] + "  " + l + "  " + j + "  " + i);
        l++;
        if( i <= 0){
          j--;
          i = j;
        if ( cfase2 >= v - 1){
            fase++;
        }
        }
        cfase2++;
}
if (fase == 2){
  println("Segunda fase, pausa");
  noLoop();
}
}
1 Like

Now i’m having some difficulty in searching on c[l] for the lowest value then drawing a line between the circles, i’m thinking of some ways to do it, but none workout. I need to find the first lowest value, so i’m gonna search on my c[l] but when i find it, how i’ll know which xiyi and xjyj it belongs… so i can use a “line(p[i].x,p[i].y,p[j].x,p[j].y”

I accept some help :3

//Quantidade de circulo
int ct = 4;
//Quantidade de valores
int v = ((ct*ct) - ct)/2;
//Comprimento e Largura de Circulo
float tamanhoC = 6;
//Contador
int i, j = ct - 1, k = fact(ct) -1, l;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[v];
//O vector que vai receber as coordenadas dos circulos
PVector[] p = new PVector[ct];
//Teste array caminho?
float[]g = new float[20];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculo
  if (fase==0) {
    println(" ");
      p[i] = new PVector(random(tamanhoC, width - tamanhoC), random(tamanhoC, height - tamanhoC));
      ellipse(p[i].x, p[i].y, tamanhoC, tamanhoC);
      println("Valor de =" + p[i]);
      if (i != j) i++;
      //Quando ela rodar a quantidade de vezes que for o J, ou seja, a quantidade de circulos -1 vai pra outra fase.
      if (cfase1 >= j) {
        fase++;
    }
    cfase1++;
  } else if (fase==1) {
    println(" ");
        i--;
        c[l] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
        println("Valor de c[l]" + "  " + c[l] + " Posicao de L " + l + "  " + j + "  " + i);
        l++;
        if( i <= 0){
          j--;
          i = j;
        if ( cfase2 >= v - 1){
            fase++;
            //To resentado os valores para voltar do ínicio da matriz, creio que sera mais fácil depois para achar os valores
            //j = ct - 1;
            //i = j;
        }
        }
        cfase2++;
}
if (fase == 2){
  l--;
  //i--;
  println(" ");
  println("Valor de i " + i, "valor de j " + j, "valor de l " + l);
  println("Segunda fase, pausa");
  noLoop();
}
}

My trial of doing it, still far away from good

//Quantidade de circulo
int ct = 4;
//Quantidade de valores
int v = ((ct*ct) - ct)/2;
//Comprimento e Largura de Circulo
float tamanhoC = 6;
//Contador
int i, j = ct - 1, k = fact(ct) -1, l, h;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[v];
//O vector que vai receber as coordenadas dos circulos
PVector[] p = new PVector[ct];
//Teste array caminho?
PVector[]g = new PVector[20];
PVector[]f = new PVector[20];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculo
  if (fase==0) {
    println(" ");
    p[i] = new PVector(random(tamanhoC, width - tamanhoC), random(tamanhoC, height - tamanhoC));
    ellipse(p[i].x, p[i].y, tamanhoC, tamanhoC);
    println("Valor de =" + p[i]);
    if (i != j) i++;
    //Quando ela rodar a quantidade de vezes que for o J, ou seja, a quantidade de circulos -1 vai pra outra fase.
    if (cfase1 >= j) {
      fase++;
    }
    cfase1++;
    println(i);
  } else if (fase==1) {
    println(" ");
    i--;
    c[l] = sqrt(pow(p[j].x - p[i].x, 2) + pow(p[j].y - p[i].y, 2));
    println("Valor de c[l]" + "  " + c[l] + " Posicao de L " + l + "  " + j + "  " + i);
    l++;
    if ( i <= 0) {
      j--;
      i = j;
      if ( cfase2 >= v - 1) {
        fase++;
        //To resentado os valores para voltar do ínicio da matriz, creio que sera mais fácil depois para achar os valores
        j = ct - 1;
        i = j;
        l = 0;
      }
    }
    cfase2++;
  }
  if (fase == 2) {
    i--;
    if ((c[l] < c[l+1]) && i >= 0 ) {
      g[h] = new PVector(p[j].x, p[j].y);
      f[h] = new PVector(p[i].x, p[i].y);
    }
    l++;
    //Passar de fase como eu to vendo o menor, ta indo por par, 0-1, 1-2, 2-3, 3-5, 4-5 ai é a quantidade de valores menos - 1
    if (l >= v - 1) fase++;
    if ( i <=0 ) {
      j--;
      i = j;
      h++;
      if (cfase3 >= v - 1) {
        fase++;
      }
    }
    cfase3++;
  } 
  if (fase == 3) {
    println("valor de h " + h);
    h--;
    if (h <= 0) {
      noLoop();
    }
    line(g[h].x, g[h].y, f[h].x, f[h].y);
  }
}

Hmm… please try to solve your problems first, before posting another question (since you‘re finding a solution yourself for 6 out of 8 questions you posted… (only 2 out of 10 posts are from others…).

Not that it‘s a problem, but we have to look through all the Code to help you each time, and then you find the answer 2 seconds later Anyway, so… which makes it difficult to know when you really need help.

We‘re always glad to help, but please try it yourself first, to see if it‘s not something you can solve easily. Thanks :sweat_smile:

2 Likes

I was updating the code and posting, in my head would be nice for someone else to see all the chances! Maybe someone would have the same problem that i’m having right now and seeing how my code evolved would help them!

Now i’m having the problem to draw the line between the circle, i have the all the value but i don’t know exactly when its the lowest to start from there and keep advancing…

Im not sure what you‘re Trying to do, but maybe you want to find the lowest circle and comment a line from there to the Next lowest and so on?

If that‘s the case, and you want to do it the same way you‘re doing the rest of the code, then try to go through all circles and save the one with the lowest position.

When you have that, reset the h variable and do go through all again, but this time ignore the one you found last. Then draw a line there.
Repeat this for all circles.

1 Like

I’m trying to make a path always using the lowest distance between points! The code that i posted on before you comment is what i managed to do, but it’s incorrect, and i still didn’t figured out how to fix it