fractals.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* fractals.c */
  2. /*
  3. * Program to draw a fractal by Michael Barnsley's deterministic algorithm.
  4. * 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
  7. * (3) To draw:
  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
  10. */
  11. /*
  12. * User Commands:
  13. * +,- - increment/decrement number of levels
  14. * PgUp, PgDn - increase/decrease scaling
  15. * Arrow keys - translate viewing section
  16. * r - reset view
  17. * Escape - quit
  18. */
  19. #include <GL/freeglut.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #define FGH_PI 3.14159265358979323846
  25. typedef struct
  26. {
  27. double a00, a01, a10, a11 ; /* Transformation matrix */
  28. double b0, b1 ; /* Constant vector added on */
  29. double statx, staty ; /* Coordinates of the stationary point */
  30. }
  31. AffineTrans ;
  32. /* Number of levels to draw the fractal */
  33. static int num_levels = 4 ;
  34. /* The definition of the fractal */
  35. static int num_trans ;
  36. static AffineTrans *affine ;
  37. /* Flag telling us to keep executing the main loop */
  38. static int continue_in_main_loop = 1;
  39. /* the window title */
  40. char window_title [ 80 ] ;
  41. /* The amount the view is translated and scaled */
  42. double xwin = 0.0, ywin = 0.0 ;
  43. double scale_factor = 1.0 ;
  44. static void draw_level ( int num, double m00, double m01, double m10, double m11, double n0, double n1 )
  45. {
  46. /* Draw a fractal transformed by "M", "N" as passed in */
  47. int i ;
  48. if ( num == 0 )
  49. {
  50. double x0 = m00 * affine[0].statx + m01 * affine[0].staty + n0 ;
  51. double y0 = m10 * affine[0].statx + m11 * affine[0].staty + n1 ;
  52. for ( i = 1; i < num_trans; i++ )
  53. {
  54. double x1 = m00 * affine[i].statx + m01 * affine[i].staty + n0 ;
  55. double y1 = m10 * affine[i].statx + m11 * affine[i].staty + n1 ;
  56. glVertex2d ( x0, y0 ) ;
  57. glVertex2d ( x1, y1 ) ;
  58. x0 = x1 ;
  59. y0 = y1 ;
  60. }
  61. }
  62. else
  63. {
  64. /* Map each affine transformation in the fractal through the one passed in and call "draw_level" */
  65. for ( i = 0; i < num_trans; i++ )
  66. {
  67. draw_level ( num-1, m00*affine[i].a00+m01*affine[i].a10, m00*affine[i].a01+m01*affine[i].a11,
  68. m10*affine[i].a00+m11*affine[i].a10, m10*affine[i].a01+m11*affine[i].a11,
  69. m00*affine[i].b0 +m01*affine[i].b1 + n0, m10*affine[i].b0 +m11*affine[i].b1 + n1 ) ;
  70. }
  71. }
  72. }
  73. static void
  74. Display(void)
  75. {
  76. glClear( GL_COLOR_BUFFER_BIT );
  77. /* the curve */
  78. glPushMatrix();
  79. glScalef(2.5, 2.5, 2.5);
  80. glColor4f(0.0, 0.0, 0.0, 1.0);
  81. glBegin ( GL_LINES ) ;
  82. draw_level ( num_levels, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 );
  83. glEnd () ;
  84. glPopMatrix();
  85. glutSwapBuffers();
  86. }
  87. static void
  88. Reshape(int width, int height)
  89. {
  90. float ar;
  91. glViewport ( 0, 0, width, height ) ;
  92. glMatrixMode ( GL_PROJECTION ) ;
  93. glLoadIdentity();
  94. ar = (float) width / (float) height ;
  95. if( ar > 1 )
  96. glFrustum ( -ar, ar, -1.0, 1.0, 2.0, 100.0 ) ;
  97. else
  98. glFrustum ( -1.0, 1.0, -1/ar, 1/ar, 2.0, 100.0 );
  99. glMatrixMode ( GL_MODELVIEW ) ;
  100. glLoadIdentity () ;
  101. xwin = -1.0 ;
  102. ywin = 0.0 ;
  103. glTranslated ( xwin, ywin, -5.0 ) ;
  104. }
  105. static void
  106. Key(unsigned char key, int x, int y)
  107. {
  108. int need_redisplay = 1;
  109. switch (key) {
  110. case 27: /* Escape key */
  111. continue_in_main_loop = 0 ;
  112. break;
  113. case '+' :
  114. ++num_levels ;
  115. break ;
  116. case '-' :
  117. if ( num_levels > 0 )
  118. --num_levels ;
  119. break ;
  120. case 'r' : case 'R' :
  121. glMatrixMode ( GL_MODELVIEW ) ;
  122. glLoadIdentity();
  123. xwin = -1.0 ;
  124. ywin = 0.0 ;
  125. glTranslated ( xwin, ywin, -5.0 ) ;
  126. break ;
  127. default:
  128. need_redisplay = 0;
  129. break;
  130. }
  131. if (need_redisplay)
  132. glutPostRedisplay();
  133. }
  134. static void
  135. Special(int key, int x, int y)
  136. {
  137. int need_redisplay = 1;
  138. switch (key) {
  139. case GLUT_KEY_UP :
  140. glMatrixMode ( GL_MODELVIEW ) ;
  141. ywin += 0.1 * scale_factor ;
  142. glTranslated ( 0.0, 0.1 * scale_factor, 0.0 ) ;
  143. break ;
  144. case GLUT_KEY_DOWN :
  145. glMatrixMode ( GL_MODELVIEW ) ;
  146. ywin -= 0.1 * scale_factor ;
  147. glTranslated ( 0.0, -0.1 * scale_factor, 0.0 ) ;
  148. break ;
  149. case GLUT_KEY_LEFT :
  150. glMatrixMode ( GL_MODELVIEW ) ;
  151. xwin -= 0.1 * scale_factor ;
  152. glTranslated ( -0.1 * scale_factor, 0.0, 0.0 ) ;
  153. break ;
  154. case GLUT_KEY_RIGHT :
  155. glMatrixMode ( GL_MODELVIEW ) ;
  156. xwin += 0.1 * scale_factor ;
  157. glTranslated ( 0.1 * scale_factor, 0.0, 0.0 ) ;
  158. break ;
  159. case GLUT_KEY_PAGE_UP :
  160. glMatrixMode ( GL_MODELVIEW ) ;
  161. glTranslated ( -xwin, -ywin, 0.0 ) ;
  162. glScaled ( 1.25, 1.25, 1.25 ) ;
  163. glTranslated ( xwin, ywin, 0.0 ) ;
  164. scale_factor *= 0.8 ;
  165. break ;
  166. case GLUT_KEY_PAGE_DOWN :
  167. glMatrixMode ( GL_MODELVIEW ) ;
  168. glTranslated ( -xwin, -ywin, 0.0 ) ;
  169. glScaled ( 0.8, 0.8, 0.8 ) ;
  170. glTranslated ( xwin, ywin, 0.0 ) ;
  171. scale_factor *= 1.25 ;
  172. break ;
  173. default:
  174. need_redisplay = 0;
  175. break;
  176. }
  177. if (need_redisplay)
  178. glutPostRedisplay();
  179. }
  180. static void
  181. checkedFGets ( char *s, int size, FILE *stream )
  182. {
  183. if ( fgets ( s, size, stream ) == NULL ) {
  184. fprintf ( stderr, "fgets failed\n");
  185. exit ( EXIT_FAILURE );
  186. }
  187. }
  188. void readConfigFile ( char *fnme )
  189. {
  190. FILE *fptr = fopen ( fnme, "rt" ) ;
  191. int i ;
  192. char inputline [ 256 ] ;
  193. if ( fptr )
  194. {
  195. /* Read a header line */
  196. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  197. /* Read a comment line */
  198. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  199. /* Read the window title */
  200. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  201. /* We assume here that this line will not exceed 79 characters plus a
  202. newline (window_title is 80 characters long). That'll cause a buffer
  203. overflow. For a simple program like this, though, we're letting it
  204. slide!
  205. */
  206. sscanf ( inputline, "%[a-zA-Z0-9!@#$%^&*()+=/\\_-\" ]", window_title ) ;
  207. /* Read a comment line */
  208. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  209. /* Read the number of affine transformations */
  210. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  211. sscanf ( inputline, "%d", &num_trans ) ;
  212. affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
  213. /* Read a comment line */
  214. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  215. for ( i = 0; i < num_trans; i++ )
  216. {
  217. /* Read an affine transformation definition */
  218. checkedFGets ( inputline, sizeof ( inputline ), fptr ) ;
  219. sscanf ( inputline, "%lf %lf %lf %lf %lf %lf", &affine[i].a00, &affine[i].a01,
  220. &affine[i].a10, &affine[i].a11, &affine[i].b0, &affine[i].b1 ) ;
  221. }
  222. }
  223. else /* No data file, set a default */
  224. {
  225. printf ( "ERROR opening file <%s>\n", fnme ) ;
  226. strcpy ( window_title, "Koch Snowflake" ) ;
  227. num_trans = 4 ;
  228. affine = (AffineTrans *)malloc ( num_trans * sizeof(AffineTrans) ) ;
  229. affine[0].a00 = 1/3. ; affine[0].a01 = 0.00 ; affine[0].a10 = 0.00 ; affine[0].a11 = 1/3. ;
  230. affine[0].b0 = 0.0 ; affine[0].b1 = 0.0 ;
  231. affine[1].a00 = 1/6. ; affine[1].a01 = -1/3.*sin(FGH_PI/3.) ; affine[1].a10 = 1/3.*sin(FGH_PI/3.) ; affine[1].a11 = 1/6. ;
  232. affine[1].b0 = 1/3. ; affine[1].b1 = 0.0 ;
  233. affine[2].a00 = 1/6. ; affine[2].a01 = -1/3.*sin(-FGH_PI/3.) ; affine[2].a10 = 1/3.*sin(-FGH_PI/3.) ; affine[2].a11 = 1/6. ;
  234. affine[2].b0 = 0.5 ; affine[2].b1 = sqrt(3)/6. ;
  235. affine[3].a00 = 1/3. ; affine[3].a01 = 0.00 ; affine[3].a10 = 0.00 ; affine[3].a11 = 1/3. ;
  236. affine[3].b0 = 2/3. ; affine[3].b1 = 0.0 ;
  237. }
  238. for ( i = 0; i < num_trans; i++ )
  239. {
  240. double m00, m01, m10, m11 ; /* Matrix "I" minus "A" */
  241. double determ ; /* Determinant of this matrix */
  242. /* 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 ;
  247. determ = m00 * m11 - m01 * m10 ;
  248. if ( fabs ( determ ) > 1.e-6 )
  249. {
  250. affine[i].statx = ( m11 * affine[i].b0 - m01 * affine[i].b1 ) / determ ;
  251. affine[i].staty = ( -m10 * affine[i].b0 + m00 * affine[i].b1 ) / determ ;
  252. }
  253. else
  254. affine[i].statx = affine[i].staty = 0.0 ;
  255. }
  256. }
  257. int
  258. main(int argc, char *argv[])
  259. {
  260. glutInitWindowSize(500, 250);
  261. glutInitWindowPosition ( 140, 140 );
  262. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
  263. glutInit(&argc, argv);
  264. if ( argc > 1 )
  265. readConfigFile ( argv[1] ) ;
  266. else
  267. readConfigFile ( "fractals.dat" ) ;
  268. glutCreateWindow( window_title );
  269. glClearColor(1.0, 1.0, 1.0, 1.0);
  270. glutReshapeFunc(Reshape);
  271. glutKeyboardFunc(Key);
  272. glutSpecialFunc(Special);
  273. glutDisplayFunc(Display);
  274. #ifdef WIN32
  275. #endif
  276. while ( continue_in_main_loop )
  277. glutMainLoopEvent();
  278. printf ( "Back from the 'freeglut' main loop\n" ) ;
  279. return 0; /* ANSI C requires main to return int. */
  280. }