#ifdef __APPLE__ #include #else #include #endif #include #include #include #include #define WINDOW_WIDTH 500 #define WINDOW_HEIGHT 500 #define LATTICE_WIDTH 100 #define LATTICE_HEIGHT 100 #define LENGTH 4 #define LAST LENGTH-1 #define POINT_SIZE 2 #define SPACING 4 short a1[LATTICE_WIDTH][LATTICE_HEIGHT]; short b1[LATTICE_WIDTH][LATTICE_HEIGHT]; short c1[LATTICE_WIDTH][LATTICE_HEIGHT]; short d1[LATTICE_WIDTH][LATTICE_HEIGHT]; short boundary[LATTICE_WIDTH][LATTICE_HEIGHT]; short a2[LATTICE_WIDTH][LATTICE_HEIGHT]; short b2[LATTICE_WIDTH][LATTICE_HEIGHT]; short c2[LATTICE_WIDTH][LATTICE_HEIGHT]; short d2[LATTICE_WIDTH][LATTICE_HEIGHT]; int mouse_x, mouse_y; unsigned int next_rand; int random_int() { next_rand = next_rand * 1103515245 + 12345; return (unsigned int) (next_rand / 65536); } int flip_coin() { int r = random_int(); return r % 2; } void next() { int x, y; short change; for (x = 0; x < LATTICE_WIDTH; x++) { for (y = 0; y < LATTICE_HEIGHT; y++) { change = ((a1[x][y] & c1[x][y] & ~(b1[x][y] | d1[x][y])) | (b1[x][y] & d1[x][y] & ~(a1[x][y] | c1[x][y]))) & boundary[x][y]; a2[x][y] = a1[x][y] ^ change; b2[x][y] = b1[x][y] ^ change; c2[x][y] = c1[x][y] ^ change; d2[x][y] = d1[x][y] ^ change; } } // propagate for (x = 1; x < LATTICE_WIDTH - 1; x++) { for (y = 1; y < LATTICE_HEIGHT - 1; y += 2) { a1[x][y] = (a2[x][y - 1] >> 1) + (a2[x - 1][y - 1] << LAST); b1[x][y] = b2[x][y - 1]; c1[x][y] = c2[x][y + 1]; d1[x][y] = (d2[x][y + 1] >> 1) + (d2[x - 1][y + 1] << LAST); a1[x][y + 1] = a2[x][y]; b1[x][y + 1] = (b2[x][y] << 1) + (b2[x + 1][y] >> LAST); c1[x][y + 1] = (c2[x][y + 2] << 1) + (c2[x + 1][y + 2] >> LAST); d1[x][y + 1] = d2[x - 1][y + 2]; } } } void draw_point(int x, int y, short value) { short i, out; short mask = 1; for (i = 0; i < LENGTH; i++) { out = value & mask; mask = mask << 1; if (out == 0) { /* glColor3f(0.2, 0.2, 1.0);*/ glColor3f(0.1, 0.0, 0.0); } glVertex3f(x + WINDOW_WIDTH / 8, y + WINDOW_HEIGHT / 8, 0.0); } } void display() { int i, j; int x, y; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_POINTS); for (x = 0; x < LATTICE_WIDTH; x++) { for (y = 0; y < LATTICE_HEIGHT; y++) { i = x * SPACING; j = y * SPACING; glColor3f(0.2, 0.0, 0.0); /* glColor3f(1.0, 0.0, 0.0);*/ glColor4f(0.2, 0.2, 0.9, 0.15); draw_point(i - 1, j + 1, a1[x][y]); /* glColor3f(0.0, 1.0, 0.0);*/ glColor4f(0.2, 0.2, 0.9, 0.15); draw_point(i - 1, j - 1, b1[x][y]); /* glColor3f(0.0, 0.0, 1.0);*/ glColor4f(0.2, 0.2, 0.9, 0.15); draw_point(i + 1, j - 1, c1[x][y]); /* glColor3f(1.0, 1.0, 1.0);*/ glColor4f(0.2, 0.2, 0.9, 0.15); draw_point(i + 1, j + 1, d1[x][y]); if (boundary[x][y] == 0) { glColor3f(1.0, 1.0, 1.0); glVertex3f(i + WINDOW_WIDTH / 8, j + 1 + WINDOW_HEIGHT / 8, 0.0); glVertex3f(i - 1 + WINDOW_WIDTH / 8, j - 1 + WINDOW_HEIGHT / 8, 0.0); glVertex3f(i + 1 + WINDOW_WIDTH / 8, j - 1 + WINDOW_HEIGHT / 8, 0.0); glVertex3f(i + 1 + WINDOW_WIDTH / 8, j + 1 + WINDOW_HEIGHT / 8, 0.0); glColor3f(0.0, 0.0, 0.0); } } } glEnd(); glutSwapBuffers(); } void idle() { int i, j, k; for (i = 0; i < LATTICE_HEIGHT / 20; i++) { for (j = 0; j < LATTICE_WIDTH / 20; j++) { if (1) { a1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = 0xF; //random_int( ) % 16; b1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = 0xF; //random_int( ) % 16; c1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = 0xF; //random_int( ) % 16; d1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = 0xF; //random_int( ) % 16; } } } /* printf("boundary at i=%d is %d\n", WINDOW_WIDTH/3, boundary[WINDOW_WIDTH/3][0]);*/ next(); glutPostRedisplay(); } void init() { int i, j, k; next_rand = 0; for (i = 0; i < LATTICE_WIDTH; i++) { for (j = 0; j < LATTICE_HEIGHT; j++) { a1[i][j] = 0; b1[i][j] = 0; c1[i][j] = 0; d1[i][j] = 0; /* if(random_int( )%45 == 0)*/ /* {*/ /* a1[i][j] = 0xF;*/ /* b1[i][j] = 0xF;*/ /* c1[i][j] = 0xF;*/ /* d1[i][j] = 0xF;*/ /* }*/ boundary[i][j] = 0xF; } } for (i = 0; i < LATTICE_WIDTH; i++) { boundary[i][LATTICE_HEIGHT - 1] = 0; boundary[i][0] = 0; } for (j = 0; j < LATTICE_HEIGHT; j++) { boundary[0][j] = 0; boundary[LATTICE_WIDTH - 1][j] = 0; } for (i = WINDOW_WIDTH / 2; i < WINDOW_WIDTH; i++) { for (j = WINDOW_HEIGHT / 2; j < WINDOW_HEIGHT; j++) { boundary[i][j] = 0; } } glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT, -20.0, 20.0); glMatrixMode(GL_MODELVIEW); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glShadeModel(GL_SMOOTH); glPointSize(POINT_SIZE); } void mouse(int button, int state, int x, int y) { mouse_x = x; mouse_y = y; } int main(int argc, char **argv) { mouse_x = 0; mouse_y = 0; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutCreateWindow("fhp"); glEnable(GL_DEPTH_TEST); init(); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return 0; }