Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » diffusion limited aggregation whit allegro

This thread is locked; no one can reply to it. rss feed Print
diffusion limited aggregation whit allegro
phy1888
Member #16,768
November 2017

hi I need help, I have this code and I have to change it for a limited central diffusion of radius r = 7, and the center is darker..

my code is :

#include <allegro.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string>
#include <iostream>

using namespace std;

const int bSchermo = 500; //riquadro 500x500
const int hSchermo = 510; //ultime 10 righe per informazioni(passi, statistica et c.)
const int d = bSchermo+2; // +2 per bordi o per sovrapposizione toro
//.... colori
int nero;// = makecol(0,0,0);
int blu ;// sfondo spazio vuoto = makecol(0,128,255);
int bianco;// particelle mobili = makecol(255,255,255);
int verdescuro; // = makecol(0, 128, 50);
int verdechiaro;//= makecol( 0, 255, 128);
int rosso;// particelle fisse = makecol(255,0,0);

//..........altri colori
BITMAP *buffer;//Dichiarazioni generali: buffer
int passi=0; // passi

int conf0[d][d]; // configurazioni
int conf1[d][d];

// variabili globali di DIFFUSIONE-AGGREGAZIONE

// ----------------------------

void toro()
{// condizione superficie toroidale
int lim = d-1;
for (int i=1; i<lim; i++)
{conf0[0][i] = conf0[lim-1][i] ;
conf0[i][0] = conf0[i][lim-1];
conf0[lim][i] = conf0[1][i];
conf0[i][lim] = conf0[i][1];
}
conf0[0][0] = conf0[lim-1][lim-1] ;
conf0[0][lim] = conf0[lim-1][1] ;
conf0[lim][0] = conf0[1][lim-1] ;
conf0[lim][lim] = conf0[1][1] ;
}

void cornice_inerte()
{// condizione cornice di celle inerti
for (int k=0; k<d; k++)
{conf0[0][k] = -1 ;
conf0[k][0] = -1;
conf0[d-1][k] = -1;
conf0[k][d-1] = -1;
}
}

void inizial ()
{
// DATI IN INGRESSO
float perc=90;
int iniziali=5;

/* generazione pseudocasuale */
float c;
int lim=d-1;
srand(time (0));
for (int i=1; i<lim; i++)
for (int j=1; j<lim; j++)
{ c=rand()%100;
if (c <= perc) conf0[i][j] = -1;
else conf0[i][j] = 1;
}
for (int i=0; i<iniziali; i++)
conf0[rand()%lim+1][rand()%lim+1] = 0;
// condizione superficie toroidale
toro();
}

void cella_aggr (int x, int y)
{ int sini, des, alto, basso, prod=1;
sini=x-1; des=x+1;
basso=y-1; alto=y+1;
for (int i=sini; i<=des; i++)
for (int j=basso; j<=alto; j++)
prod=prod*conf0[i][j];
if ((prod==0) && (conf0[x][y]==1)) conf1[x][y]=0;
}

/*void cella_trans (int x, int y)
{ switch(conf0[x][y])
{ case -1: conf1[x][y]=-1; break;
case 0: conf1[x][y]= 2; break;
case 1: conf1[x][y]= contagio(x,y); break;
case 2: conf1[x][y]= 2; break;;
}
}

void cella_trans (int x, int y)
{ switch(conf0[x][y])
{ case -1: conf1[x][y]=-1; break;
case 0: conf1[x][y]= 2; break;
case 1: conf1[x][y]= contagio(x,y); break;
case 2: conf1[x][y]= 2; break;;
}
}*/
void copia ()
// si copia la configurazione
{ for (int i=0; i<d; i++)
for (int j=0; j<d; j++)
conf0[i][j] = conf1[i][j] ;
}

/*void cella_aggr(int x, int y)
{if ((conf0[x-1][y]==0)||(conf0[x+1][y]==0)||(conf0[x][y+1]==0)||(conf0[x][y-1]==0))
if (conf0[x][y] == 1 )conf1[x][y] = 0;
_________________________________________________
//for (int i=x-1; i<=(x+1); i++)
//for (int j=y-1; j<=(y+1); j++)
//if ((conf0[i][j] == 0) && )
}*/

void cella_diff (int x, int y)
{bool clockwise=rand()%2 ;
int aux;
if (!((conf0[x][y]==0)||(conf0[x+1][y]==0)||(conf0[x][y+1]==0)||(conf0[x+1][y+1]==0)))
if (clockwise)
{
conf1[x][y] = conf0[x][y+1];
conf1[x][y+1] = conf0[x+1][y+1];
conf1[x+1][y+1] = conf0[x+1][y];
conf1[x+1][y] = conf0[x][y];
}
else
{
conf1[x][y] = conf0[x+1][y] ;
conf1[x+1][y] = conf0[x+1][y+1];
conf1[x+1][y+1] = conf0[x][y+1];
conf1[x][y+1] = conf0[x][y];
}
}

void diffus1 ()
{ for (int i=0; i<d; i+=2)
for (int j=0; j<d; j+=2)
cella_diff (i,j);
}

void diffus2 ()
{for (int i=1; i<d-1; i+=2)
for (int j=1; j<d; j+=2)
cella_diff (i,j);
}

void aggrega ()
{for (int i=1; i<d; i++)
for (int j=1; j<d; j++)
cella_aggr (i,j);
}

void transiz ()
{ if (passi%2==0)diffus1();
else diffus2();
// condizione superficie toroidale
toro();
copia();
aggrega();
toro();
copia();
}

int main()
{// inizializzazione;
float percvive;// perc = 4
inizial ();
allegro_init();
install_keyboard();
install_mouse();
set_gfx_mode( GFX_AUTODETECT_WINDOWED, bSchermo, hSchermo, 0, 0);
//Indica al computer la modalità grafica che intendiamo usare
show_mouse(screen);
//Mostra il mouse sullo schermo
buffer = create_bitmap(bSchermo, hSchermo);
// Funzione che crea il buffer dove disegniamo gli elementi
blu = makecol(0, 128, 255);
nero = makecol(0, 0, 0);
bianco = makecol(255,255,255);
verdescuro = makecol( 0, 128, 50);
verdechiaro = makecol (0, 255, 128);
rosso = makecol(255, 0, 0);
//Regola il colore degli elementi
while (!key[KEY_ESC])
{/* funzione di transizione */
transiz();
passi++;
for (int y=0; y<bSchermo; y++)
for (int x=0; x<hSchermo; x++)
switch(conf0[x][y])
{ case -1: putpixel(buffer, x, y, blu); break;
case 0: putpixel(buffer, x, y, rosso); break;
case 1: putpixel(buffer, x, y, bianco); break;
//case 2: putpixel(buffer, x, y, verdechiaro); break;;
}
textprintf_ex(buffer,font,0,502,bianco,nero, "iterazioni: %i",passi);
//textprintf_ex(buffer,font,280,502,bianco,nero, "percentuale vive : %f",percvive); //stampa sul buffer il numero di iterazioni
blit(buffer,screen,0,0,0,0,bSchermo,hSchermo);
//stampa (disegna)sullo schermo il buffer d'uscita
}
return 0;
}
END_OF_MAIN()

Chris Katko
Member #1,881
January 2002
avatar

don't forget to use <code > tags

#SelectExpand
1 2#include <allegro.h> 3#include <stdio.h> 4#include <stdlib.h> 5#include <math.h> 6#include <time.h> 7#include <string> 8#include <iostream> 9 10using namespace std; 11 12const int bSchermo = 500; //riquadro 500x500 13const int hSchermo = 510; //ultime 10 righe per informazioni(passi, statistica et c.) 14const int d = bSchermo+2; // +2 per bordi o per sovrapposizione toro 15//.... colori 16int nero;// = makecol(0,0,0); 17int blu ;// sfondo spazio vuoto = makecol(0,128,255); 18int bianco;// particelle mobili = makecol(255,255,255); 19int verdescuro; // = makecol(0, 128, 50); 20int verdechiaro;//= makecol( 0, 255, 128); 21int rosso;// particelle fisse = makecol(255,0,0); 22 23//..........altri colori 24BITMAP *buffer;//Dichiarazioni generali: buffer 25int passi=0; // passi 26 27int conf0[d][d]; // configurazioni 28int conf1[d][d]; 29 30// variabili globali di DIFFUSIONE-AGGREGAZIONE 31 32// ---------------------------- 33 34void toro() 35{// condizione superficie toroidale 36int lim = d-1; 37for (int i=1; i<lim; i++) 38{conf0[0][i] = conf0[lim-1][i] ; 39conf0[i][0] = conf0[i][lim-1]; 40conf0[lim][i] = conf0[1][i]; 41conf0[i][lim] = conf0[i][1]; 42} 43conf0[0][0] = conf0[lim-1][lim-1] ; 44conf0[0][lim] = conf0[lim-1][1] ; 45conf0[lim][0] = conf0[1][lim-1] ; 46conf0[lim][lim] = conf0[1][1] ; 47} 48 49void cornice_inerte() 50{// condizione cornice di celle inerti 51for (int k=0; k<d; k++) 52{conf0[0][k] = -1 ; 53conf0[k][0] = -1; 54conf0[d-1][k] = -1; 55conf0[k][d-1] = -1; 56} 57} 58 59void inizial () 60{ 61// DATI IN INGRESSO 62float perc=90; 63int iniziali=5; 64 65/* generazione pseudocasuale */ 66float c; 67int lim=d-1; 68srand(time (0)); 69for (int i=1; i<lim; i++) 70for (int j=1; j<lim; j++) 71{ c=rand()%100; 72if (c <= perc) conf0[i][j] = -1; 73else conf0[i][j] = 1; 74} 75for (int i=0; i<iniziali; i++) 76conf0[rand()%lim+1][rand()%lim+1] = 0; 77// condizione superficie toroidale 78toro(); 79} 80 81void cella_aggr (int x, int y) 82{ int sini, des, alto, basso, prod=1; 83sini=x-1; des=x+1; 84basso=y-1; alto=y+1; 85for (int i=sini; i<=des; i++) 86for (int j=basso; j<=alto; j++) 87prod=prod*conf0[i][j]; 88if ((prod==0) && (conf0[x][y]==1)) conf1[x][y]=0; 89} 90 91/*void cella_trans (int x, int y) 92{ switch(conf0[x][y]) 93{ case -1: conf1[x][y]=-1; break; 94case 0: conf1[x][y]= 2; break; 95case 1: conf1[x][y]= contagio(x,y); break; 96case 2: conf1[x][y]= 2; break;; 97} 98} 99 100void cella_trans (int x, int y) 101{ switch(conf0[x][y]) 102{ case -1: conf1[x][y]=-1; break; 103case 0: conf1[x][y]= 2; break; 104case 1: conf1[x][y]= contagio(x,y); break; 105case 2: conf1[x][y]= 2; break;; 106} 107}*/ 108void copia () 109// si copia la configurazione 110{ for (int i=0; i<d; i++) 111for (int j=0; j<d; j++) 112conf0[i][j] = conf1[i][j] ; 113} 114 115/*void cella_aggr(int x, int y) 116{if ((conf0[x-1][y]==0)||(conf0[x+1][y]==0)||(conf0[x][y+1]==0)||(conf0[x][y-1]==0)) 117if (conf0[x][y] == 1 )conf1[x][y] = 0; 118_________________________________________________ 119//for (int i=x-1; i<=(x+1); i++) 120//for (int j=y-1; j<=(y+1); j++) 121//if ((conf0[i][j] == 0) && ) 122}*/ 123 124void cella_diff (int x, int y) 125{bool clockwise=rand()%2 ; 126int aux; 127if (!((conf0[x][y]==0)||(conf0[x+1][y]==0)||(conf0[x][y+1]==0)||(conf0[x+1][y+1]==0))) 128if (clockwise) 129{ 130conf1[x][y] = conf0[x][y+1]; 131conf1[x][y+1] = conf0[x+1][y+1]; 132conf1[x+1][y+1] = conf0[x+1][y]; 133conf1[x+1][y] = conf0[x][y]; 134} 135else 136{ 137conf1[x][y] = conf0[x+1][y] ; 138conf1[x+1][y] = conf0[x+1][y+1]; 139conf1[x+1][y+1] = conf0[x][y+1]; 140conf1[x][y+1] = conf0[x][y]; 141} 142} 143 144void diffus1 () 145{ for (int i=0; i<d; i+=2) 146for (int j=0; j<d; j+=2) 147cella_diff (i,j); 148} 149 150void diffus2 () 151{for (int i=1; i<d-1; i+=2) 152for (int j=1; j<d; j+=2) 153cella_diff (i,j); 154} 155 156void aggrega () 157{for (int i=1; i<d; i++) 158for (int j=1; j<d; j++) 159cella_aggr (i,j); 160} 161 162void transiz () 163{ if (passi%2==0)diffus1(); 164else diffus2(); 165// condizione superficie toroidale 166toro(); 167copia(); 168aggrega(); 169toro(); 170copia(); 171} 172 173int main() 174{// inizializzazione; 175float percvive;// perc = 4 176inizial (); 177allegro_init(); 178install_keyboard(); 179install_mouse(); 180set_gfx_mode( GFX_AUTODETECT_WINDOWED, bSchermo, hSchermo, 0, 0); 181//Indica al computer la modalità grafica che intendiamo usare 182show_mouse(screen); 183//Mostra il mouse sullo schermo 184buffer = create_bitmap(bSchermo, hSchermo); 185// Funzione che crea il buffer dove disegniamo gli elementi 186blu = makecol(0, 128, 255); 187nero = makecol(0, 0, 0); 188bianco = makecol(255,255,255); 189verdescuro = makecol( 0, 128, 50); 190verdechiaro = makecol (0, 255, 128); 191rosso = makecol(255, 0, 0); 192//Regola il colore degli elementi 193while (!key[KEY_ESC]) 194{/* funzione di transizione */ 195transiz(); 196passi++; 197for (int y=0; y<bSchermo; y++) 198for (int x=0; x<hSchermo; x++) 199switch(conf0[x][y]) 200{ case -1: putpixel(buffer, x, y, blu); break; 201case 0: putpixel(buffer, x, y, rosso); break; 202case 1: putpixel(buffer, x, y, bianco); break; 203//case 2: putpixel(buffer, x, y, verdechiaro); break;; 204} 205textprintf_ex(buffer,font,0,502,bianco,nero, "iterazioni: %i",passi); 206//textprintf_ex(buffer,font,280,502,bianco,nero, "percentuale vive : %f",percvive); //stampa sul buffer il numero di iterazioni 207blit(buffer,screen,0,0,0,0,bSchermo,hSchermo); 208//stampa (disegna)sullo schermo il buffer d'uscita 209} 210return 0; 211} 212END_OF_MAIN()

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

phy1888
Member #16,768
November 2017

thank you, can you help me??;D;D;

beoran
Member #12,636
March 2011

What is this program supposed to do and what do you want to change? I dont /nderstand Italian very well...

phy1888
Member #16,768
November 2017

this program shows me how the diffusion of red pairs occurs and how they are united after interacting with other white particles that move.
And now, I would like this diffusion to be no more random, but I would like it to be limited and be able to give it a ray..::)???

Go to: