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 ;
211 void readConfigFile ( char *fnme )
213 FILE *fptr = fopen ( fnme, "rt" ) ;
215 char inputline [ 256 ] ;
219 /* Read a header line */
220 fgets ( inputline, 256, fptr ) ;
222 /* Read a comment line */
223 fgets ( inputline, 256, fptr ) ;
225 /* Read the window title */
226 fgets ( inputline, 256, fptr ) ;
227 /* We assume here that this line will not exceed 79 characters plus a
228 newline (window_title is 80 characters long). That'll cause a buffer
229 overflow. For a simple program like this, though, we're letting it
232 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
234 /* Read a comment line */
235 fgets ( inputline, 256, fptr ) ;
237 /* Read the number of affine transformations */
238 fgets ( inputline, 256, fptr ) ;
239 sscanf ( inputline, "%d", &num_trans ) ;
241 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
243 /* Read a comment line */
244 fgets ( inputline, 256, fptr ) ;
246 for ( i = 0; i < num_trans; i++ )
248 /* Read an affine transformation definition */
249 fgets ( inputline, 256, fptr ) ;
250 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
251 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
254 else /* No data file, set a default */
256 printf ( "ERROR opening file <%s>\n", fnme ) ;
257 strcpy ( window_title, "Cantor Dust" ) ;
259 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
260 affine[0].a00 = 0.25 ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 0.25 ;
261 affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
262 affine[1].a00 = 0.25 ; affine[1].a01 = 0.00 ; affine[1].a10 = 0.00 ; affine[1].a11 = 0.25 ;
263 affine[1].b0 = 0.5 ; affine[1].b1 = 0.0 ;
266 for ( i = 0; i < num_trans; i++ )
268 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
269 double determ ; /* Determinant of this matrix */
271 /* Calculate the stationary point */
273 m00 = 1.0 - affine[i].a00 ;
274 m01 = - affine[i].a01 ;
275 m10 = - affine[i].a10 ;
276 m11 = 1.0 - affine[i].a11 ;
278 determ = m00 * m11 - m01 * m10 ;
280 if ( fabs ( determ ) > 1.e-6 )
282 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
283 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
286 affine[i].statx = affine[i].staty = 0.0 ;
291 main(int argc, char *argv[])
295 glutInitWindowSize(500, 250);
296 glutInitWindowPosition ( 140, 140 );
297 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
298 glutInit(&argc, argv);
301 readConfigFile ( argv[1] ) ;
303 readConfigFile ( "fractals.dat" ) ;
305 fractal_window = glutCreateWindow( window_title );
307 glClearColor(1.0, 1.0, 1.0, 1.0);
309 glutReshapeFunc(Reshape);
310 glutKeyboardFunc(Key);
311 glutSpecialFunc(Special);
312 glutDisplayFunc(Display);
316 printf ( "Back from the 'freeglut' main loop\n" ) ;
318 return 0; /* ANSI C requires main to return int. */