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 = 0 ;
38 /* The definition of the fractal */
39 static int num_trans ;
40 static AffineTrans *affine ;
42 /* the window title */
43 char window_title [ 80 ] ;
45 /* The amount the view is translated and scaled */
46 double xwin = 0.0, ywin = 0.0 ;
47 double scale_factor = 1.0 ;
49 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
51 /* Draw a fractal transformed by "M", "N" as passed in */
56 double x0 = m00 * affine[0].statx + m01 * affine[0].staty + n0 ;
57 double y0 = m10 * affine[0].statx + m11 * affine[0].staty + n1 ;
59 for ( i = 1; i < num_trans; i++ )
61 double x1 = m00 * affine[i].statx + m01 * affine[i].staty + n0 ;
62 double y1 = m10 * affine[i].statx + m11 * affine[i].staty + n1 ;
64 glVertex2d ( x0, y0 ) ;
65 glVertex2d ( x1, y1 ) ;
73 /* Map each affine transformation in the fractal through the one passed in and call "draw_level" */
75 for ( i = 0; i < num_trans; i++ )
77 draw_level ( num-1, m00*affine[i].a00+m01*affine[i].a10, m00*affine[i].a01+m01*affine[i].a11,
78 m10*affine[i].a00+m11*affine[i].a10, m10*affine[i].a01+m11*affine[i].a11,
79 m00*affine[i].b0 +m01*affine[i].b1 + n0, m10*affine[i].b0 +m11*affine[i].b1 + n1 ) ;
87 glClear( GL_COLOR_BUFFER_BIT );
91 glScalef(2.5, 2.5, 2.5);
93 glColor4f(0.0, 0.0, 0.0, 1.0);
94 glBegin ( GL_LINES ) ;
95 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
103 Reshape(int width, int height)
106 glViewport ( 0, 0, width, height ) ;
107 glMatrixMode ( GL_PROJECTION ) ;
109 ar = (float) width / (float) height ;
111 glFrustum ( -ar, ar, -1.0, 1.0, 2.0, 100.0 ) ;
113 glFrustum ( -1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0 );
114 glMatrixMode ( GL_MODELVIEW ) ;
118 glTranslated ( xwin, ywin, -5.0 ) ;
122 Key(unsigned char key, int x, int y)
124 int need_redisplay = 1;
127 case 27: /* Escape key */
128 glutLeaveMainLoop () ;
136 if ( num_levels > 0 )
140 case 'r' : case 'R' :
141 glMatrixMode ( GL_MODELVIEW ) ;
145 glTranslated ( xwin, ywin, -5.0 ) ;
157 Special(int key, int x, int y)
159 int need_redisplay = 1;
163 glMatrixMode ( GL_MODELVIEW ) ;
164 ywin += 0.1 * scale_factor ;
165 glTranslated ( 0.0, 0.1 * scale_factor, 0.0 ) ;
169 glMatrixMode ( GL_MODELVIEW ) ;
170 ywin -= 0.1 * scale_factor ;
171 glTranslated ( 0.0, -0.1 * scale_factor, 0.0 ) ;
175 glMatrixMode ( GL_MODELVIEW ) ;
176 xwin -= 0.1 * scale_factor ;
177 glTranslated ( -0.1 * scale_factor, 0.0, 0.0 ) ;
180 case GLUT_KEY_RIGHT :
181 glMatrixMode ( GL_MODELVIEW ) ;
182 xwin += 0.1 * scale_factor ;
183 glTranslated ( 0.1 * scale_factor, 0.0, 0.0 ) ;
186 case GLUT_KEY_PAGE_UP :
187 glMatrixMode ( GL_MODELVIEW ) ;
188 glTranslated ( -xwin, -ywin, 0.0 ) ;
189 glScaled ( 1.25, 1.25, 1.25 ) ;
190 glTranslated ( xwin, ywin, 0.0 ) ;
191 scale_factor *= 0.8 ;
194 case GLUT_KEY_PAGE_DOWN :
195 glMatrixMode ( GL_MODELVIEW ) ;
196 glTranslated ( -xwin, -ywin, 0.0 ) ;
197 glScaled ( 0.8, 0.8, 0.8 ) ;
198 glTranslated ( xwin, ywin, 0.0 ) ;
199 scale_factor *= 1.25 ;
212 checkedFGets ( char *s, int size, FILE *stream )
214 if ( fgets ( s, size, stream ) == NULL ) {
215 fprintf ( stderr, "fgets failed\n");
216 exit ( EXIT_FAILURE );
221 void readConfigFile ( char *fnme )
223 FILE *fptr = fopen ( fnme, "rt" ) ;
225 char inputline [ 256 ] ;
229 /* Read a header line */
230 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
232 /* Read a comment line */
233 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
235 /* Read the window title */
236 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
237 /* We assume here that this line will not exceed 79 characters plus a
238 newline (window_title is 80 characters long). That'll cause a buffer
239 overflow. For a simple program like this, though, we're letting it
242 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
244 /* Read a comment line */
245 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
247 /* Read the number of affine transformations */
248 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
249 sscanf ( inputline, "%d", &num_trans ) ;
251 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
253 /* Read a comment line */
254 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
256 for ( i = 0; i < num_trans; i++ )
258 /* Read an affine transformation definition */
259 checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
260 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
261 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
264 else /* No data file, set a default */
266 printf ( "ERROR opening file <%s>\n", fnme ) ;
267 strcpy ( window_title, "Cantor Dust" ) ;
269 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
270 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
271 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
272 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
273 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
276 for ( i = 0; i < num_trans; i++ )
278 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
279 double determ ; /* Determinant of this matrix */
281 /* Calculate the stationary point */
283 m00 = 1.0 - affine[i].a00 ;
284 m01 = - affine[i].a01 ;
285 m10 = - affine[i].a10 ;
286 m11 = 1.0 - affine[i].a11 ;
288 determ = m00 * m11 - m01 * m10 ;
290 if ( fabs ( determ ) > 1.e-6 )
292 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
293 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
296 affine[i].statx = affine[i].staty = 0.0 ;
301 main(int argc, char *argv[])
305 glutInitWindowSize(500, 250);
306 glutInitWindowPosition ( 140, 140 );
307 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
308 glutInit(&argc, argv);
311 readConfigFile ( argv[1] ) ;
313 readConfigFile ( "fractals.dat" ) ;
315 fractal_window = glutCreateWindow( window_title );
317 glClearColor(1.0, 1.0, 1.0, 1.0);
319 glutReshapeFunc(Reshape);
320 glutKeyboardFunc(Key);
321 glutSpecialFunc(Special);
322 glutDisplayFunc(Display);
326 printf ( "Back from the 'freeglut' main loop\n" ) ;
328 return 0; /* ANSI C requires main to return int. */