/* basic flies source in C by Alex Vulliamy - http://www.alxvy.org */ /* /* You are welcome to do whatever you wish with this code, as long /* as Jeff Cragg and I (Alex Vulliamy) are given a credit. #include #define FLYS 90 /* number of flies */ #define VX 2 /* array element that represents x-velocity */ #define VY 3 /* array element .. y-veloctiy */ #define NN 5 /* velocity for bouncing off walls */ #define X 0 /* array element ... x position */ #define Y 1 /* array element ... y position */ #define ZZ 30 /* maximum x or y speed */ int fly [FLYS][4],width,nf,rendomindx; /* rendomindx is the number of fly whose turn it is to have a neighbour randomised */ int neighbour [FLYS][2]; /* each fly has two nearest neighbours */ int random(int range) { int rdm; if (range==0) return 0; rdm=rand() % range; return (int) rdm; } /* randomises flies neighbours */ void rendombit() { extern int neighbour[FLYS][2],nf; auto int indx; for (indx=0;indx=nf) rendomindx=0; neighbour [rendomindx++][0]=random(nf); /* randomise flies neighbours every so often */ if (rand()<100) rendombit(); } void edges (int tick) { extern int fly [FLYS][4]; if (fly [tick][X]<1) fly [tick][VX]=random(NN); if (fly [tick][X]>295) fly [tick][VX]=-random(NN); if (fly [tick][Y]<1) fly [tick][VY]=random(NN); if (fly [tick][Y]>295) fly [tick][VY]=-random(NN); } void proxes (int tick) { extern int fly [FLYS][4]; extern int neighbour [FLYS][2]; auto int proxi,i,j,n[2],temp1,temp2,tempa,tempb,ni; n[0]=neighbour[tick][0]; n[1]=neighbour[tick][1]; /* this bit compares the nearest neighbours to the nearest neighbours of the nearest neighbours and sees if the latter are closer than the former, if they are then they replace the former */ for (i=0;i<2;i++) { ni=n[i]; proxi=distance(ni,tick); for (j=0;j<2;j++) { tempb=neighbour[ni][j]; if ( proxiZZ) fly[tick][VX]=ZZ; if (fly[tick][VX]<-ZZ) fly[tick][VX]=-ZZ; if (temp2!=0) fly[tick][VY]=fly[tick][VY]+tempa*(abs(temp2)/temp2); if (fly[tick][VY]>ZZ) fly[tick][VY]=ZZ; if (fly[tick][VY]<-ZZ) fly[tick][VY]=-ZZ; } } int distance (int zero,int one) { auto int x,a,b; extern int fly [FLYS][4]; a= fly [zero][X]-fly[one][X]; b= fly [zero][Y]-fly[one][Y]; x=(a*a)+(b*b); return x; } main () { int nf; nf=FLYS; setup (); for (;;) {refresh ();} }