Testo nascosto, fai click qui per vederlo
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
int nMinX, nMaxX, nMinY, nMaxY;
int **p;
int setcall = 0;
typedef struct { int xl, xr, y, dy; } LINESEGMENT;
#define MAXDEPTH 10000
#define PUSH(XL, XR, Y, DY) \
if( sp < stack+MAXDEPTH && Y+(DY) >= nMinX && Y+(DY) <= nMaxY ) \
{ sp->xl = XL; sp->xr = XR; sp->y = Y; sp->dy = DY; ++sp; }
#define POP(XL, XR, Y, DY) \
{ --sp; XL = sp->xl; XR = sp->xr; Y = sp->y+(DY = sp->dy); }
int GetPixel(int x, int y) {
return p[x][y];
}
void SetPixel(int x, int y, int c) {
p[x][y] = c;
setcall++;
}
// Fill background with given color
void SeedFill_4(int x, int y, int new_color)
{
int left, x1, x2, dy;
int old_color;
LINESEGMENT stack[MAXDEPTH], *sp = stack;
old_color = GetPixel(x, y);
if( old_color == new_color )
return;
if( x < nMinX || x > nMaxX || y < nMinX || y > nMaxY ) {
puts("Border");
return;
}
PUSH(x, x, y, 1); /* needed in some cases */
PUSH(x, x, y+1, -1); /* seed segment (popped 1st) */
while( sp > stack ) {
POP(x1, x2, y, dy);
for( x = x1; x >= nMinX && GetPixel(x, y) == old_color; --x )
SetPixel(x, y, new_color);
if( x >= x1 )
goto SKIP;
left = x+1;
if( left < x1 )
PUSH(y, left, x1-1, -dy); /* leak on left? */
x = x1+1;
do {
for( ; x<=nMaxX && GetPixel(x, y) == old_color; ++x )
SetPixel(x, y, new_color);
PUSH(left, x-1, y, dy);
if( x > x2+1 )
PUSH(x2+1, x-1, y, -dy); /* leak on right? */
SKIP: for( ++x; x <= x2 && GetPixel(x, y) != old_color; ++x ) {;}
left = x;
} while( x<=x2 );
}
}
int main(int argc, char *argv[]) {
// dimensione del reticolo
int d = 1000;
// numero di prove
int iter = 100;
double res[iter];
nMinX = 0;
nMaxX = d-1;
nMinY = 0;
nMaxY = d-1;
p = new int*[d];
for (int i=0; i<d; i++) {
p[i] = new int[d];
}
// inizializza il generatore random
srand(time(0));
double s = 0, ss = 0;
for (int n = 0; n < iter; n++) {
// inizializza il reticolo con dati random fra 0 e 1
for (int i=0; i<d; i++) {
for (int j=0; j<d; j++) {
p[i][j] = rand() % 2;
}
}
setcall = 0;
SeedFill_4(d/2, d/2, 1 - GetPixel(d/2, d/2));
printf("%7d,", setcall);
res[n] = setcall;
s += setcall;
double s2 = setcall;
ss += s2 * s2;
}
double m = s / iter;
double var = (ss - s*s / iter) / (iter - 1);
//printf("\nSomma = %f, Somma quad = %f\n", s, ss);
printf("\nMedia = %f, varianza = %f\n", m, var);
}
#include <stdio.h>
#include <math.h>
#include <time.h>
int nMinX, nMaxX, nMinY, nMaxY;
int **p;
int setcall = 0;
typedef struct { int xl, xr, y, dy; } LINESEGMENT;
#define MAXDEPTH 10000
#define PUSH(XL, XR, Y, DY) \
if( sp < stack+MAXDEPTH && Y+(DY) >= nMinX && Y+(DY) <= nMaxY ) \
{ sp->xl = XL; sp->xr = XR; sp->y = Y; sp->dy = DY; ++sp; }
#define POP(XL, XR, Y, DY) \
{ --sp; XL = sp->xl; XR = sp->xr; Y = sp->y+(DY = sp->dy); }
int GetPixel(int x, int y) {
return p[x][y];
}
void SetPixel(int x, int y, int c) {
p[x][y] = c;
setcall++;
}
// Fill background with given color
void SeedFill_4(int x, int y, int new_color)
{
int left, x1, x2, dy;
int old_color;
LINESEGMENT stack[MAXDEPTH], *sp = stack;
old_color = GetPixel(x, y);
if( old_color == new_color )
return;
if( x < nMinX || x > nMaxX || y < nMinX || y > nMaxY ) {
puts("Border");
return;
}
PUSH(x, x, y, 1); /* needed in some cases */
PUSH(x, x, y+1, -1); /* seed segment (popped 1st) */
while( sp > stack ) {
POP(x1, x2, y, dy);
for( x = x1; x >= nMinX && GetPixel(x, y) == old_color; --x )
SetPixel(x, y, new_color);
if( x >= x1 )
goto SKIP;
left = x+1;
if( left < x1 )
PUSH(y, left, x1-1, -dy); /* leak on left? */
x = x1+1;
do {
for( ; x<=nMaxX && GetPixel(x, y) == old_color; ++x )
SetPixel(x, y, new_color);
PUSH(left, x-1, y, dy);
if( x > x2+1 )
PUSH(x2+1, x-1, y, -dy); /* leak on right? */
SKIP: for( ++x; x <= x2 && GetPixel(x, y) != old_color; ++x ) {;}
left = x;
} while( x<=x2 );
}
}
int main(int argc, char *argv[]) {
// dimensione del reticolo
int d = 1000;
// numero di prove
int iter = 100;
double res[iter];
nMinX = 0;
nMaxX = d-1;
nMinY = 0;
nMaxY = d-1;
p = new int*[d];
for (int i=0; i<d; i++) {
p[i] = new int[d];
}
// inizializza il generatore random
srand(time(0));
double s = 0, ss = 0;
for (int n = 0; n < iter; n++) {
// inizializza il reticolo con dati random fra 0 e 1
for (int i=0; i<d; i++) {
for (int j=0; j<d; j++) {
p[i][j] = rand() % 2;
}
}
setcall = 0;
SeedFill_4(d/2, d/2, 1 - GetPixel(d/2, d/2));
printf("%7d,", setcall);
res[n] = setcall;
s += setcall;
double s2 = setcall;
ss += s2 * s2;
}
double m = s / iter;
double var = (ss - s*s / iter) / (iter - 1);
//printf("\nSomma = %f, Somma quad = %f\n", s, ss);
printf("\nMedia = %f, varianza = %f\n", m, var);
}