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>
28 double a00, a01, a10, a11 ; /* Transformation matrix */
29 double b0, b1 ; /* Constant vector added on */
30 double statx, staty ; /* Coordinates of the stationary point */
34 /* Number of levels to draw the fractal */
35 static int num_levels = 0 ;
37 /* The definition of the fractal */
38 static int num_trans ;
39 static AffineTrans *affine ;
41 /* the window title */
42 char window_title [ 80 ] ;
44 /* The amount the view is translated and scaled */
45 double xwin = 0.0, ywin = 0.0 ;
46 double scale_factor = 1.0 ;
48 static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
50 /* Draw a fractal transformed by "M", "N" as passed in */
55 double x0 = m00 * affine[0].statx + m01 * affine[0].staty + n0 ;
56 double y0 = m10 * affine[0].statx + m11 * affine[0].staty + n1 ;
58 for ( i = 1; i < num_trans; i++ )
60 double x1 = m00 * affine[i].statx + m01 * affine[i].staty + n0 ;
61 double y1 = m10 * affine[i].statx + m11 * affine[i].staty + n1 ;
63 glVertex2d ( x0, y0 ) ;
64 glVertex2d ( x1, y1 ) ;
72 /* Map each affine transformation in the fractal through the one passed in and call "draw_level" */
74 for ( i = 0; i < num_trans; i++ )
76 draw_level ( num-1, m00*affine[i].a00+m01*affine[i].a10, m00*affine[i].a01+m01*affine[i].a11,
77 m10*affine[i].a00+m11*affine[i].a10, m10*affine[i].a01+m11*affine[i].a11,
78 m00*affine[i].b0 +m01*affine[i].b1 + n0, m10*affine[i].b0 +m11*affine[i].b1 + n1 ) ;
86 glClear( GL_COLOR_BUFFER_BIT );
90 glScalef(2.5, 2.5, 2.5);
92 glColor4f(0.0, 0.0, 0.0, 1.0);
93 glBegin ( GL_LINES ) ;
94 draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
102 Reshape(int width, int height)
105 glViewport ( 0, 0, width, height ) ;
106 glMatrixMode ( GL_PROJECTION ) ;
108 ar = (float) width / (float) height ;
110 glFrustum ( -ar, ar, -1.0, 1.0, 2.0, 100.0 ) ;
112 glFrustum ( -1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0 );
113 glMatrixMode ( GL_MODELVIEW ) ;
117 glTranslated ( xwin, ywin, -5.0 ) ;
121 Key(unsigned char key, int x, int y)
124 case 27: /* Escape key */
125 glutLeaveMainLoop () ;
133 if ( num_levels > 0 )
137 case 'r' : case 'R' :
138 glMatrixMode ( GL_MODELVIEW ) ;
142 glTranslated ( xwin, ywin, -5.0 ) ;
151 Special(int key, int x, int y)
155 glMatrixMode ( GL_MODELVIEW ) ;
156 ywin += 0.1 * scale_factor ;
157 glTranslated ( 0.0, 0.1 * scale_factor, 0.0 ) ;
161 glMatrixMode ( GL_MODELVIEW ) ;
162 ywin -= 0.1 * scale_factor ;
163 glTranslated ( 0.0, -0.1 * scale_factor, 0.0 ) ;
167 glMatrixMode ( GL_MODELVIEW ) ;
168 xwin -= 0.1 * scale_factor ;
169 glTranslated ( -0.1 * scale_factor, 0.0, 0.0 ) ;
172 case GLUT_KEY_RIGHT :
173 glMatrixMode ( GL_MODELVIEW ) ;
174 xwin += 0.1 * scale_factor ;
175 glTranslated ( 0.1 * scale_factor, 0.0, 0.0 ) ;
178 case GLUT_KEY_PAGE_UP :
179 glMatrixMode ( GL_MODELVIEW ) ;
180 glTranslated ( -xwin, -ywin, 0.0 ) ;
181 glScaled ( 1.25, 1.25, 1.25 ) ;
182 glTranslated ( xwin, ywin, 0.0 ) ;
183 scale_factor *= 0.8 ;
186 case GLUT_KEY_PAGE_DOWN :
187 glMatrixMode ( GL_MODELVIEW ) ;
188 glTranslated ( -xwin, -ywin, 0.0 ) ;
189 glScaled ( 0.8, 0.8, 0.8 ) ;
190 glTranslated ( xwin, ywin, 0.0 ) ;
191 scale_factor *= 1.25 ;
198 void readConfigFile ( char *fnme )
200 FILE *fptr = fopen ( fnme, "rt" ) ;
202 char inputline [ 256 ] ;
204 /* Read a header line */
205 fgets ( inputline, 256, fptr ) ;
207 /* Read a comment line */
208 fgets ( inputline, 256, fptr ) ;
210 /* Read the window title */
211 fgets ( inputline, 256, fptr ) ;
212 /* We assume here that this line will not exceed 79 characters plus a
213 newline (window_title is 80 characters long). That'll cause a buffer
214 overflow. For a simple program like this, though, we're letting it
217 sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
219 /* Read a comment line */
220 fgets ( inputline, 256, fptr ) ;
222 /* Read the number of affine transformations */
223 fgets ( inputline, 256, fptr ) ;
224 sscanf ( inputline, "%d", &num_trans ) ;
226 affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
228 /* Read a comment line */
229 fgets ( inputline, 256, fptr ) ;
231 for ( i = 0; i < num_trans; i++ )
233 double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
234 double determ ; /* Determinant of this matrix */
236 /* Read an affine transformation definition */
237 fgets ( inputline, 256, fptr ) ;
238 sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
239 &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
241 /* Calculate the stationary point */
243 m00 = 1.0 - affine[i].a00 ;
244 m01 = - affine[i].a01 ;
245 m10 = - affine[i].a10 ;
246 m11 = 1.0 - affine[i].a11 ;
248 determ = m00 * m11 - m01 * m10 ;
250 if ( fabs ( determ ) > 1.e-6 )
252 affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
253 affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
256 affine[i].statx = affine[i].staty = 0.0 ;
261 main(int argc, char *argv[])
265 glutInitWindowSize(500, 250);
266 glutInitWindowPosition ( 140, 140 );
267 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
268 glutInit(&argc, argv);
271 readConfigFile ( argv[1] ) ;
273 readConfigFile ( "fractals.dat" ) ;
275 fractal_window = glutCreateWindow( window_title );
277 glClearColor(1.0, 1.0, 1.0, 1.0);
279 glutReshapeFunc(Reshape);
280 glutKeyboardFunc(Key);
281 glutSpecialFunc(Special);
282 glutDisplayFunc(Display);
286 printf ( "Back from the 'freeglut' main loop\n" ) ;
288 return 0; /* ANSI C requires main to return int. */