class Ball { float r; float m; float x; float y; float vx; float vy; int id; float ka; String nombre; int ocurrencias; // Spring float mass; // Masa float kspring; // Constante de resorte float damp; // Damping float rest_posx = ( ( width-espacio_derecha ) / 2 ) + espacio_izquierda / 2; float rest_posy = ( ( height-espacio_abajo ) / 2 ) + espacio_derecha / 2; float accel = 0; // Aceleracion float force = 0; // Fuerza boolean estamos_encima; Ball( int ID, float KA, String NOMBRE, int OCURR ) { ka = KA; r = sqrt( ka / PI ); m = r; x = random(r+espacio_izquierda,width-espacio_derecha-r); y = random(r+espacio_arriba,height-espacio_abajo-r); vx = random(-3,3); vy = random(-3,3); id = ID; nombre = NOMBRE; ocurrencias = OCURR; estamos_encima = false; mass = sqrt( ( ( (PI*pow((height-espacio_abajo-espacio_arriba)/2,2)*0.8) / 2000 ) / PI ) ); damp = 0.85; kspring = 0.01; } void fall() { if ( keyboard.pressed(UP) ) vy -= grav; if ( keyboard.pressed(DOWN) ) vy += grav; if ( keyboard.pressed(LEFT) ) vx -= grav; if ( keyboard.pressed(RIGHT) ) vx += grav; } void spring() { rest_posx = ( ( width-espacio_derecha ) / 2 ) + espacio_izquierda / 2; rest_posy = ( ( height-espacio_abajo ) / 2 ) + espacio_derecha / 2; if ( balls.length > 0 && ( balls[0].ocurrencias - balls[burbujas_graficadas-1].ocurrencias ) > 0 ) { float A = balls[0].ocurrencias; // maximo original float C = ocurrencias; // valor original float B = balls[burbujas_graficadas-1].ocurrencias; // minimo original float D = 5; // nuevo maximo float E; // nuevo minimo if ( burbujas_graficadas > 20 ) E = -1; else E = 0; kspring = -1 * ( ( ( A - C ) / ( A - B ) ) * ( D - E ) - D ); } if ( burbujas_graficadas == 1 ) kspring = 4; //mass = r; force = -kspring * (y - rest_posy); // f=-ky accel = force / mass; // Asignar aceleracion vy = damp * (vy + accel); // Definir velocidad //y += vy; force = -kspring * (x - rest_posx); // f=-ky accel = force / mass; // Asignar aceleracion vx = damp * (vx + accel); // Definir velocidad //x += vx; } void bounce() { if ( y + vy + r > height-espacio_abajo ) { y = height-espacio_abajo - r; vx *= f; vy *= -b; } if ( y + vy - r < espacio_arriba ) { y = r+espacio_arriba; vx *= f; vy *= -b; } if ( x + vx + r > width-espacio_derecha ) { x = width-espacio_derecha - r; vx *= -b; vy *= f; } if ( x + vx - r < espacio_izquierda ) { x = r+espacio_izquierda; vx *= -b; vy *= f; } } void collide() { for ( int i=burbujas_maximas; i>=0; i-- ) { if ( i < balls.length ) { float X = balls[i].x; float Y = balls[i].y; float R = balls[i].r; float M = balls[i].m; float deltax = X-x; float deltay = Y-y; float d = sqrt(pow(deltax,2)+pow(deltay,2)); if ( d < r + R && d > 0 ) { float dD = r + R - d; float theta = atan2(deltay,deltax); vx += -dD*cos(theta)*M/(m+M); vy += -dD*sin(theta)*M/(m+M); vx *= b; vy *= b; } } } } void move() { if ( estamos_encima && mousePressed && ( arrastrando == -1 || arrastrando == id ) ) { x = mouseX; y = mouseY; vx = 0; vy = 0; arrastrando = id; } else { x += vx; y += vy; } } void encima() { if ( dist(x, y, mouseX, mouseY) < r ) estamos_encima = true; else estamos_encima = false; } void display() { float A = balls[0].ocurrencias; // maximo original float C = ocurrencias; // valor original float B = balls[burbujas_graficadas-1].ocurrencias; // minimo original float D; // nuevo maximo float E; // nuevo minimo //nuevo_valor = -1 * ( ( ( A - C ) / ( A - B ) ) * ( D - E ) - D ); if ( visual_mode == "lineal" ) { if ( llenar_burbujas ) fill(255,255,255); else noFill(); if ( estamos_encima ) fill(0,0,0,15); strokeWeight(r/10); //stroke(ColorLineasGrales); float lc = -1 * ( ( ( A - C ) / ( A - B ) ) * ( 60 - 200 ) - 60 ); float lcalpha = -1 * ( ( ( A - C ) / ( A - B ) ) * ( 255 - 90 ) - 255 ); if ( A == B ) lcalpha = 255; color local = color( lc ); stroke( local ); //noFill(); ellipse(x,y,2*r-r/10,2*r-r/10); float tamanio = r*0.8; textFont(font, tamanio); textAlign(CENTER); fill(0, 102, 153, lcalpha); //fill(0, 102, 153); //if ( show_info || estamos_encima ) text(nombre, x, y+tamanio/5); if ( show_info ) text(nombre, x, y+tamanio/5); else text(nombre, x, y+tamanio/3); //if ( show_info || estamos_encima ) { if ( show_info ) { float tamanio1 = r*0.3; textFont(font, tamanio1); fill(0, 102, 153, lcalpha); text(str(ocurrencias), x, y+tamanio/3+tamanio1); } } } } Ball[] append(Ball t[], float ka, String NOMBRE, int OCURR) { Ball temp[]=new Ball[t.length+1]; System.arraycopy(t,0,temp,0,t.length); temp[t.length]=new Ball(t.length, ka, NOMBRE, OCURR); return temp; }