Code Running slow when using text()

I have created a particle animation to visualize data from a tsv file. Each particle instance is store in an arraylist that removes an element when it is no longer visible (following the example on Daniel Shiffmann NOC_4_02_ArrayListParticles The arraylist size counts up to 750 elements and then start deleting them. When I run the sketch without any text() it runs smoothly. But when I run it with a string with information about each particles, using text(), the sketch runs very slow (from 60fps to 30 fps). Even when I just apply a random static text I get the same problem. any ideas why this is happening? thanks from Chile! (sorry for the variable names in spanish)

Here the particle class called "Pais"

class Pais{

PVector pais; 
PVector santiago = mercatorMap.getScreenLocation(new PVector(-29.469,-70.649)); 

float distX;
float distY;
float pasos;
float porcentajeViajado = 0;
float x = 0.0;
float y = 0.0;
float valor = 0.0;
float latitud;
float longitud;
float k;
String nombre;
int mes;
float vidaNombre = 0;
float posXMes = width - 10;
float posYMes = 20;


Pais(float latitudTemp, float longitudTemp, float valorTemp, int mesTemp, String nombreTemp){
    latitud = latitudTemp;
    longitud = longitudTemp;
    valor = valorTemp;
    nombre = nombreTemp;
    mes = mesTemp;
}

void run(){
    configurar();
    //informacionSobreLaExportacion();
    viaja();
}

void configurar(){
    pais = mercatorMap.getScreenLocation(new PVector(latitud, longitud));
    distX = pais.x - santiago.x;
    distY = pais.y - santiago.y;

    if ((abs(distX) + abs(distY)) <= 50){
        pasos = 0.01;
    }
    else if((abs(distX) + abs(distY)) > 50 && (abs(distX) + abs(distY)) <= 200  ){
        pasos = 0.008;
    }
    else if((abs(distX) + abs(distY)) > 200 && (abs(distX) + abs(distY)) < 500  ){
        pasos = 0.004;
    }
    else if((abs(distX) + abs(distY)) >= 500){
        pasos = 0.001;
    }

    k = valor;
    if (k <= 2000){
        k = 1;
    }
    else if (k > 2000 && k <= 6000){
        k = 2;
    }
    else if (k > 6000 && k <= 12000){
        k = 3;
    }
    else if (k > 12000 && k <= 55000){
        k = 4;
    }
    else if (k > 55000 && k <= 100000){
        k = 5;
    }
    else if (k > 100000 && k <= 200000){
        k = 6;
    }
    else if (k > 200000 && k <= 700000){
        k = 6;
    }
    else if (k > 700000 && k <= 1000000){
        k = 7;
    }
    else if (k > 1000000 && k <= 9000000){
        k = 8;
    }
    else if (k > 9000000 && k <= 16000000){
        k = 9;
    }
    else if (k > 16000000 && k <= 26000000){
        k = 10;
    }
    else if (k > 26000000 && k <= 46000000){
        k = 11;
    }
    else if (k > 46000000 && k <= 60000000){
        k = 12;
    }
    else if (k > 60000000 && k <= 100000000){
        k = 13;
    }
    else if (k > 100000000){
        k = 18;
    }
}

void viaja(){
    porcentajeViajado += pasos; 
    if(porcentajeViajado < 1){
        x =  santiago.x + (porcentajeViajado * distX);
        y =  santiago.y + (pow(porcentajeViajado, 2)* distY);
        fill(#78cffa);
        ellipse(x, y, k, k);
    }
}

boolean yaNoSirve(){
    if (porcentajeViajado >= 1.0){
        return true;
    }
    else{
        return false;
    }
}

void informacionSobreLaExportacion(){
    vidaNombre += 2.5;
    if(porcentajeViajado > 0.85){
        textFont(font,8);
        fill(#b1e3fc, vidaNombre);
        text(nombre, pais.x, pais.y);
    }

    textAlign(RIGHT);
    textFont(font,14);
    fill(100,10);
    textLeading(15);
    text("Enero\nFebrero\nMarzo\nAbril\nMayo\nJunio\nJulio\nAgosto\nSeptiembre\nOctubre\nNoviembre\nDiciembre",posXMes,posYMes);

    fill(#b1e3fc);
    switch (mes) {
        case 1:
            text("Enero", posXMes, posYMes);
            break;
        case 2:
            text("Febrero", posXMes, posYMes+15);
            break;
        case 3:
            text("Marzo", posXMes, posYMes+30);
            break;
        case 4:
            text("Abril", posXMes, posYMes+45);
            break;
        case 5:
            text("Mayo", posXMes, posYMes+60);
            break;
        case 6:
            text("Junio", posXMes, posYMes+75);
            break;
        case 7:
            text("Julio", posXMes, posYMes+90);
            break;
        case 8:
            text("Agosto", posXMes, posYMes+105);
            break;
        case 9:
            text("Septiembre", posXMes, posYMes+120);
            break;
        case 10:
            text("Octubre", posXMes, posYMes+135);
            break;
        case 11:
            text("Noviembre", posXMes, posYMes+150);
            break;
        case 12:
            text("Diciembre", posXMes, posYMes+165);
            break;
    }
}
}

This is the way i'm looping through the array

void draw() {
  noStroke();
  tint(255,20);
  image(mapImage, 0, 0, width, height);
  pais = exportaciones.nuevaImportacion();
  paises.add ( new Pais( pais.x, pais.y, pais.z, exportaciones.mesExportacion(), exportaciones.nombrePais()));

  for (int i = paises.size()-1; i >= 0; i--) {
    Pais p = paises.get(i);
    p.run();
    if (p.yaNoSirve()){
      paises.remove(i);
    }
  }
}

Answers

  • Well, you display the whole calendar for each particle? That's lot to draw...

    How do you declare the font? Is it bitmap (VLW) or TrueType? The latter might be a bit slower.
    An alternative is to draw, in setup(), each label in a small PGraphics, then to draw the corresponding graphics in place of the text() call. This way, there is no computation of letters, of position of each letter, etc.

  • I was able to do it running the text directly in the draw() instead of each partciles. thanks!

Sign In or Register to comment.