|
Zakath |
 |
Guru 
Déconnecté
Niveau : 5 N° de Membre :
7213
Ancienneté : 97%
Participation : 12%
Inscription: 15 Dec 2002
Localisation: Copenhague
Messages: 9866
Sujets Lancés : 426
|
Citation: Message écrit par shakes808080, le 31-01-2005 à 10:10
Très intéressant... maintenant, faudrait montrer ce que ça donne concrètement par exemple : on a tous des PC capables de faire des simulations de ce genre, il faudrait juste un petit programme . |
Oui, ce n'est vraiment pas difficile, surtout pour les unidimensionnels.
Si tu veux, je peux te passer mes sources C (avec le Makefile qui va bien) pour gcc sous Linux (il te faut la SDL pour le graphisme) qui font ces zolis dessins.
Edit: voila la chose, recopie dans les fichiers qui vont bien puis fais un make. Il te faut gcc et SDL. Il y a deux règles, 54 et 110, renomme celle de ton choix en rules.c puis recompile. Le lancement se fait avec
code: ./cells largeur etapes random seed
Makefile: code: CC = gcc
CFLAGS = -Wall
SRC = rules.c graph.c sim.c
PROG = cells
INC = -I /usr/include/SDL
INCLIBS = -lSDL
all: $(PROG)
$(PROG):
$(CC) $(CFLAGS) $(INC) $(SRC) -o $@ $(INCLIBS)
clean:
rm cells
sim.c : code: /* you must rename the rules files you intend to use in rules.c/rules.h */
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include "rules.h"
#include "graph.h"
int* conf;
int size;
int voisin[VECTS];
void compute (void) {
int i, j, k;
int valide;
int* tmp;
tmp = calloc (size, sizeof(int));
for (i = 0; i < size; i++) {
valide = 1;
for (j = 0; j < VECTS; j++) {
k = i + vects[j][0];
if (k >= 0 && k < size)
voisin[j] = conf[k];
else {
valide = 0;
break;
}
}
if (valide)
tmp[i] = delta(voisin);
else
/* On choisit de remplir les bords extérieurs avec des 0 */
tmp[i] = 0;
}
free (conf);
conf = tmp;
return;
}
int main (int argc, char* argv[]) {
int i;
SDL_Surface* screen;
int steps, is_random, seed;
if (argc != 5) {
printf ("Bad number of arguments. Expected 4 and received %d\n", argc-1);
exit (0);
}
if (DIM != 1) {
fprintf (stderr, "As for now, only unidimensionnal automata are supported, sorry\n");
exit (0);
}
screen = SDL_Initialisation();
size = atoi(argv[1]);
steps = atoi(argv[2]);
is_random = atoi(argv[3]);
seed = atoi(argv[4]);
srand(seed);
conf = calloc (size, sizeof(int));
if (is_random) {
for (i = 0; i < size; i++) {
conf[i] = rand()%STATES;
}
} else {
for (i = 0; i < size; i++) {
if (i == size/2)
conf[i] = 1;
else
conf[i] = 0;
}
}
for (i = 0; i < steps && i < HEIGHT/2; i++) {
compute();
paint_line (conf, i, size, screen);
}
getc(stdin);
free (conf);
return 0;
}
graph.c : code: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL.h>
#include "graph.h"
int color_r[2] = {0xff, 0x00};
int color_g[2] = {0xff, 0x00};
int color_b[2] = {0xff, 0x00};
SDL_Surface* SDL_Initialisation (void) {
SDL_Surface *screen;
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 8, SDL_SWSURFACE);
return (screen);
}
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface-> ixels + y * surface-> itch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
void paint_pixel (SDL_Surface* screen, int y, int x, int r, int g, int b) {
Uint32 yellow;
yellow = SDL_MapRGB(screen->format, r, g, b);
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
return;
}
}
putpixel(screen, x, y, yellow);
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
return;
}
void paint_line (int* bin, int line, int size, SDL_Surface* screen) {
int i, r, g, b;
int col;
for (col = 0; col < WIDTH/2 && col < size; col++) {
i = bin[col];
r = color_r[i];
g = color_g[i];
b = color_b[i];
paint_pixel (screen, 2*line, 2*col, r, g, b);
paint_pixel (screen, 2*line+1, 2*col, r, g, b);
paint_pixel (screen, 2*line, 2*col+1, r, g, b);
paint_pixel (screen, 2*line+1, 2*col+1, r, g, b);
}
SDL_UpdateRect(screen, 0, 2*line, WIDTH, 2);
return;
}
graph.h : code: #include <SDL.h>
#define WIDTH 1024
#define HEIGHT 768
SDL_Surface* SDL_Initialisation (void);
void paint_line (int*, int, int, SDL_Surface*);
rules.h : code: /* dimension de l'automate */
#define DIM 1
/* Nombre d'états */
#define STATES 2
/* Nombre de vecteurs de voisinage */
#define VECTS 3
int vects[VECTS][DIM];
int delta (int*);
r54.c : code: /* Rule 54 */
#include "sample.h"
int vects[VECTS][DIM] = {{-1}, {0}, {1}};
int delta (int* v) {
if (v[1] == 0) {
if (v[0] == 0 && v[2] == 0)
return 0;
else
return 1;
}
if (v[0] == 0 && v[2] == 0)
return 1;
return 0;
}
r1110.c : code: /* Rule 110 */
#include "sample.h"
int vects[VECTS][DIM] = {{-1}, {0}, {1}};
int delta (int* v) {
if (v[0] == 1) {
if (v[1] == v[2])
return 0;
else
return 1;
} else {
if (v[1] == 0 && v[2] == 0)
return 0;
}
return 1;
}
__________________
Allez jeter un œil à mon portfolio !
Edité par Zakath le 31-01-2005 à 22:40
Signaler ce message à un modérateur | IP: Logguée Temps en ligne : 103 Jours, 6 Heures, 14 Minutes, 38 Secondes en ligne
|