Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int width = 600; // Ancho
- int height = 400; // Alto
- int dotsize = 10; // Tamanho de los cuadrados
- boolean field[]; // Campo
- int dwidth; // Cuadrados por linea
- boolean stop = true; // ΒΏEsta parado?
- int fsize; // Tamanho del array de campo
- /* Dibuja una cuadrcula */
- void gen_background() {
- stroke( 0 );
- for ( int x = 0 ; x < width ; x += dotsize ) {
- line( x, 0, x, height );
- }
- for ( int y = 0 ; y < height ; y += dotsize ) {
- line( 0, y, width, y );
- }
- }
- /* Prepara el escenario */
- void setup() {
- size( width, height );
- dwidth = width / dotsize;
- fsize = (height / dotsize) * dwidth;
- field = new boolean[ ( width * height ) / ( dotsize * dotsize) ];
- }
- /* Cuenta los "vecinos" */
- int count_elements( boolean f[], int p ) {
- int i = 0 ;
- if (p + 1 >= dwidth) {
- if (f[ (p + 1 - dwidth) ]) {
- i++;
- }
- }
- if (p >= dwidth)
- if (f[ (p - dwidth) ]) {
- i++;
- }
- if (p - 1 >= dwidth ) {
- if (f[ (p - 1 - dwidth) ]) {
- i++;
- }
- }
- if (p > 0) {
- if (f[ (p - 1) ]) {
- i++;
- }
- }
- if (p + 1 < fsize) {
- if (f[ (p + 1) ]) {
- i++;
- }
- }
- if (p + 1 < fsize - dwidth ) {
- if (f[ (p + 1 + dwidth) ]) {
- i++;
- }
- }
- if (p - 1 < fsize - dwidth ) {
- if (f[ (p - 1 + dwidth) ]) {
- i++;
- }
- }
- if (p < fsize - dwidth ) {
- if (f[ (p + dwidth) ]) {
- i++;
- }
- }
- return i;
- }
- /* Obtiene el siguiente array */
- void getNextArr( boolean f[], boolean o[] ) {
- int last = ( width * height ) / ( dotsize * dotsize ) ;
- boolean flipFlop = !o[0];
- for ( int i = 0 ; i < last ; i++ ) {
- int v = count_elements(o, i);
- f [ i ] = ((o[ i ] && v > 1 && v < 4) ||
- ( v == 3 ) );
- }
- }
- /* Muestra el campo */
- void show_field( boolean f[] ) {
- fill( 0 );
- int i = 0;
- for( int x = 0 ; x < width ; x += dotsize ) {
- int j = 0;
- for ( int y = 0 ; y < height ; y += dotsize ) {
- if ( f[ ( dwidth * j) + i ] ) {
- rect( x, y, dotsize, dotsize );
- }
- j++;
- }
- i++;
- }
- }
- /* Funcion por defecto */
- void draw() {
- background( 255, 255, 255 );
- if (!stop) {
- boolean old[] = field;
- field = new boolean[ (width * height) / ( dotsize * dotsize) ];
- getNextArr( field, old );
- }
- gen_background();
- show_field( field );
- }
- /* Activa/Desactiva una casilla con el raton */
- void mousePressed() {
- stop = true;
- int pos = ( mouseY / dotsize * dwidth ) + ( mouseX / dotsize );
- field[ pos ] = !field[ pos ];
- }
- /* Para/Reanuda con el teclado */
- void keyPressed() {
- stop = !stop;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement