3 * Program to draw a fractal by Michael Barnsley's deterministic algorithm.
5 * (1) Define the affine transformations (of the form r(i+1) = A r(i) + b )
6 * (2) Find the stationary point for each transformation
8 * - If you are at the lowest level, draw lines connecting all the stationary points
9 * - If not, call the draw function recursively with each affine transformation applied
14 * +,- - increment/decrement number of levels
15 * PgUp, PgDn - increase/decrease scaling
16 * Arrow keys - translate viewing section
21 #include <GL/freeglut.h>
29 double a00, a01, a10, a11 ; /* Transformation matrix */
30 double b0, b1 ; /* Constant vector added on */
31 double statx, staty ; /* Coordinates of the stationary point */
35 /* Number of levels to draw the fractal */
36 static int num_levels = 4 ;
38 /* The definition of the fractal */
39 static int num_trans ;
40 static AffineTrans *affine ;
42 /* Flag telling us to keep executing the main loop */
43 static int continue_in_main_loop = 1;
45 /* the window title */
46 char window_title [ 80 ] ;
48 /* The amount the view is translated and scaled */
49 double xwin = 0.0, ywin = 0.0 ;
50 double scale_factor = 1.0 ;
52 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
54 /* Draw a fractal transformed by "M", "N" as passed in */
59 double x0 = m00 * affine[0].statx + m01 * affine[0].staty + n0 ;
60 double y0 = m10 * affine[0].statx + m11 * affine[0].staty + n1 ;
62 for ( i = 1; i < num_trans; i++ )
64 double x1 = m00 * affine[i].statx + m01 * affine[i].staty + n0 ;
65 double y1 = m10 * affine[i].statx + m11 * affine[i].staty + n1 ;
67 glVertex2d ( x0, y0 ) ;
68 glVertex2d ( x1, y1 ) ;
76 /* Map each affine transformation in the fractal through the one passed in and call "draw_level" */
78 for ( i = 0; i < num_trans; i++ )
80 draw_level ( num-1, m00*affine[i].a00+m01*affine[i].a10, m00*affine[i].a01+m01*affine[i].a11,
81 m10*affine[i].a00+m11*affine[i].a10, m10*affine[i].a01+m11*affine[i].a11,
82 m00*affine[i].b0 +m01*affine[i].b1 + n0, m10*affine[i].b0 +m11*affine[i].b1 + n1 ) ;
90 glClear( GL_COLOR_BUFFER_BIT );
94 glScalef(2.5, 2.5, 2.5);
96 glColor4f(0.0, 0.0, 0.0, 1.0);
97 glBegin ( GL_LINES ) ;
98 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
106 Reshape(int width, int height)
109 glViewport ( 0, 0, width, height ) ;
110 glMatrixMode ( GL_PROJECTION ) ;
112 ar = (float) width / (float) height ;
114 glFrustum ( -ar, ar, -1.0, 1.0, 2.0, 100.0 ) ;
116 glFrustum ( -1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0 );
117 glMatrixMode ( GL_MODELVIEW ) ;
121 glTranslated ( xwin, ywin, -5.0 ) ;
125 Key(unsigned char key, int x, int y)
127 int need_redisplay = 1;
130 case 27: /* Escape key */
131 continue_in_main_loop = 0 ;
139 if ( num_levels > 0 )
143 case 'r' : case 'R' :
144 glMatrixMode ( GL_MODELVIEW ) ;
148 glTranslated ( xwin, ywin, -5.0 ) ;
160 Special(int key, int x, int y)
162 int need_redisplay = 1;
166 glMatrixMode ( GL_MODELVIEW ) ;
167 ywin += 0.1 * scale_factor ;
168 glTranslated ( 0.0, 0.1 * scale_factor, 0.0 ) ;
172 glMatrixMode ( GL_MODELVIEW ) ;
173 ywin -= 0.1 * scale_factor ;
174 glTranslated ( 0.0, -0.1 * scale_factor, 0.0 ) ;
178 glMatrixMode ( GL_MODELVIEW ) ;
179 xwin -= 0.1 * scale_factor ;
180 glTranslated ( -0.1 * scale_factor, 0.0, 0.0 ) ;
183 case GLUT_KEY_RIGHT :
184 glMatrixMode ( GL_MODELVIEW ) ;
185 xwin += 0.1 * scale_factor ;
186 glTranslated ( 0.1 * scale_factor, 0.0, 0.0 ) ;
189 case GLUT_KEY_PAGE_UP :
190 glMatrixMode ( GL_MODELVIEW ) ;
191 glTranslated ( -xwin, -ywin, 0.0 ) ;
192 glScaled ( 1.25, 1.25, 1.25 ) ;
193 glTranslated ( xwin, ywin, 0.0 ) ;
194 scale_factor *= 0.8 ;
197 case GLUT_KEY_PAGE_DOWN :
198 glMatrixMode ( GL_MODELVIEW ) ;
199 glTranslated ( -xwin, -ywin, 0.0 ) ;
200 glScaled ( 0.8, 0.8, 0.8 ) ;
201 glTranslated ( xwin, ywin, 0.0 ) ;
202 scale_factor *= 1.25 ;
215 checkedFGets ( char *s, int size, FILE *stream )
217 if ( fgets ( s, size, stream ) == NULL ) {
218 fprintf ( stderr, "fgets failed\n");
219 exit ( EXIT_FAILURE );
224 void readConfigFile ( char *fnme )
226 FILE *fptr = fopen ( fnme, "rt" ) ;
228 char inputline [ 256 ] ;
232 /* Read a header line */
233 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
235 /* Read a comment line */
236 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
238 /* Read the window title */
239 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
240 /* We assume here that this line will not exceed 79 characters plus a
241 newline (window_title is 80 characters long). That'll cause a buffer
242 overflow. For a simple program like this, though, we're letting it
245 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
247 /* Read a comment line */
248 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
250 /* Read the number of affine transformations */
251 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
252 sscanf ( inputline, "%d", &num_trans ) ;
254 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
256 /* Read a comment line */
257 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
259 for ( i = 0; i < num_trans; i++ )
261 /* Read an affine transformation definition */
262 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
263 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
264 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
267 else /* No data file, set a default */
269 printf ( "ERROR opening file <%s>\n", fnme ) ;
270 strcpy ( window_title, "Cantor Dust" ) ;
272 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
273 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
274 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
275 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
276 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
279 for ( i = 0; i < num_trans; i++ )
281 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
282 double determ ; /* Determinant of this matrix */
284 /* Calculate the stationary point */
286 m00 = 1.0 - affine[i].a00 ;
287 m01 = - affine[i].a01 ;
288 m10 = - affine[i].a10 ;
289 m11 = 1.0 - affine[i].a11 ;
291 determ = m00 * m11 - m01 * m10 ;
293 if ( fabs ( determ ) > 1.e-6 )
295 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
296 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
299 affine[i].statx = affine[i].staty = 0.0 ;
304 main(int argc, char *argv[])
308 glutInitWindowSize(500, 250);
309 glutInitWindowPosition ( 140, 140 );
310 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
311 glutInit(&argc, argv);
314 readConfigFile ( argv[1] ) ;
316 readConfigFile ( "fractals.dat" ) ;
318 fractal_window = glutCreateWindow( window_title );
320 glClearColor(1.0, 1.0, 1.0, 1.0);
322 glutReshapeFunc(Reshape);
323 glutKeyboardFunc(Key);
324 glutSpecialFunc(Special);
325 glutDisplayFunc(Display);
330 while ( continue_in_main_loop )
333 printf ( "Back from the 'freeglut' main loop\n" ) ;
335 return 0; /* ANSI C requires main to return int. */