flame.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: flame.c /main/3 1995/11/02 16:07:01 rswiston $ */
  24. /*
  25. */
  26. /* *
  27. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  28. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  29. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  30. * (c) Copyright 1993, 1994 Novell, Inc. *
  31. */
  32. /*-
  33. * flame.c - recursive fractal cosmic flames.
  34. *
  35. * Copyright (c) 1991 by Patrick J. Naughton.
  36. *
  37. * See dtscreen.c for copying information.
  38. *
  39. * Revision History:
  40. * 27-Jun-91: vary number of functions used.
  41. * 24-Jun-91: fixed portability problem with integer mod (%).
  42. * 06-Jun-91: Written. (received from Scott Graves, spot@cs.cmu.edu).
  43. */
  44. #include "dtscreen.h"
  45. #include <math.h>
  46. #include <stdlib.h>
  47. #define MAXTOTAL 10000
  48. #define MAXBATCH 10
  49. #define MAXLEV 4
  50. typedef struct {
  51. double f[2][3][MAXLEV];/* three non-homogeneous transforms */
  52. int max_levels;
  53. int cur_level;
  54. int snum;
  55. int anum;
  56. int width, height;
  57. int num_points;
  58. int total_points;
  59. int pixcol;
  60. perwindow *pwin;
  61. XPoint pts[MAXBATCH];
  62. } flamestruct;
  63. static short
  64. halfrandom(int mv)
  65. {
  66. static short lasthalf = 0;
  67. unsigned long r;
  68. if (lasthalf) {
  69. r = lasthalf;
  70. lasthalf = 0;
  71. } else {
  72. r = random();
  73. lasthalf = r >> 16;
  74. }
  75. return r % mv;
  76. }
  77. void
  78. initflame(perwindow *pwin)
  79. {
  80. XWindowAttributes xwa;
  81. flamestruct *fs;
  82. if (pwin->data) free(pwin->data);
  83. pwin->data = (void *)malloc(sizeof(flamestruct));
  84. memset(pwin->data, '\0', sizeof(flamestruct));
  85. fs = (flamestruct *)pwin->data;
  86. srandom(time((time_t *) 0));
  87. XGetWindowAttributes(dsp, pwin->w, &xwa);
  88. fs->width = xwa.width;
  89. fs->height = xwa.height;
  90. fs->max_levels = batchcount;
  91. fs->pwin = pwin;
  92. XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
  93. XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0, fs->width, fs->height);
  94. if (pwin->perscreen->npixels > 2) {
  95. fs->pixcol = halfrandom(pwin->perscreen->npixels);
  96. XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[fs->pixcol]);
  97. } else {
  98. XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
  99. }
  100. }
  101. static Bool
  102. recurse(flamestruct *fs, double x, double y, int l)
  103. {
  104. int i;
  105. double nx, ny;
  106. if (l == fs->max_levels) {
  107. fs->total_points++;
  108. if (fs->total_points > MAXTOTAL) /* how long each fractal runs */
  109. return False;
  110. if (x > -1.0 && x < 1.0 && y > -1.0 && y < 1.0) {
  111. fs->pts[fs->num_points].x = (int) ((fs->width / 2) * (x + 1.0));
  112. fs->pts[fs->num_points].y = (int) ((fs->height / 2) * (y + 1.0));
  113. fs->num_points++;
  114. if (fs->num_points > MAXBATCH) { /* point buffer size */
  115. XDrawPoints(dsp, fs->pwin->w, fs->pwin->gc, fs->pts,
  116. fs->num_points, CoordModeOrigin);
  117. fs->num_points = 0;
  118. }
  119. }
  120. } else {
  121. for (i = 0; i < fs->snum; i++) {
  122. nx = fs->f[0][0][i] * x + fs->f[0][1][i] * y + fs->f[0][2][i];
  123. ny = fs->f[1][0][i] * x + fs->f[1][1][i] * y + fs->f[1][2][i];
  124. if (i < fs->anum) {
  125. nx = sin(nx);
  126. ny = sin(ny);
  127. }
  128. if (!recurse(fs, nx, ny, l + 1))
  129. return False;
  130. }
  131. }
  132. return True;
  133. }
  134. void
  135. drawflame(perwindow *pwin)
  136. {
  137. flamestruct *fs = (flamestruct *)pwin->data;
  138. int i, j, k;
  139. static int alt = 0;
  140. if (!(fs->cur_level++ % fs->max_levels)) {
  141. XClearWindow(dsp, fs->pwin->w);
  142. alt = !alt;
  143. } else {
  144. if (pwin->perscreen->npixels > 2) {
  145. XSetForeground(dsp, pwin->gc,
  146. pwin->perscreen->pixels[fs->pixcol]);
  147. if (--fs->pixcol < 0)
  148. fs->pixcol = pwin->perscreen->npixels - 1;
  149. }
  150. }
  151. /* number of functions */
  152. fs->snum = 2 + (fs->cur_level % (MAXLEV - 1));
  153. /* how many of them are of alternate form */
  154. if (alt)
  155. fs->anum = 0;
  156. else
  157. fs->anum = halfrandom(fs->snum) + 2;
  158. /* 6 dtfs per function */
  159. for (k = 0; k < fs->snum; k++) {
  160. for (i = 0; i < 2; i++)
  161. for (j = 0; j < 3; j++)
  162. fs->f[i][j][k] = ((double) (random() & 1023) / 512.0 - 1.0);
  163. }
  164. fs->num_points = 0;
  165. fs->total_points = 0;
  166. (void) recurse(fs, 0.0, 0.0, 0);
  167. XDrawPoints(dsp, pwin->w, pwin->gc,
  168. fs->pts, fs->num_points, CoordModeOrigin);
  169. }