5e7eedae7aa321198355fa28422e956dea637d04
[xscreensaver] / hacks / sierpinski.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sierpinski --- Sierpinski's triangle fractal */
3
4 #if 0
5 static const char sccsid[] = "@(#)sierpinski.c  5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1996 by Desmond Daignault
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * Dots initially appear where they "should not".  Later they get
24  * "focused".  This is correct behavior.
25  *
26  * Revision History:
27  * 01-Nov-2000: Allocation checks
28  * 18-Sep-1997: 3D version Antti Kuntsi <kuntsi@iki.fi>.
29  * 20-May-1997: Changed the name tri to sierpinski for more compatiblity
30  * 10-May-1997: Jamie Zawinski <jwz@jwz.org> compatible with xscreensaver
31  * 05-Sep-1996: Desmond Daignault Datatimes Incorporated
32  *            <tekdd@dtol.datatimes.com> .
33  */
34
35 #ifdef STANDALONE
36 # define MODE_sierpinski
37 # define DEFAULTS       "*delay: 400000 \n" \
38                                         "*count: 2000 \n" \
39                                         "*cycles: 100 \n" \
40                                         "*ncolors: 64 \n" \
41                                         "*fpsSolid: true \n" \
42
43 # define BRIGHT_COLORS
44 # define sierpinski_handle_event 0
45 # include "xlockmore.h"         /* in xscreensaver distribution */
46 #else /* STANDALONE */
47 # include "xlock.h"             /* in xlockmore distribution */
48 #endif /* STANDALONE */
49
50 #ifdef MODE_sierpinski
51
52 ENTRYPOINT ModeSpecOpt sierpinski_opts =
53 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
54
55 #ifdef USE_MODULES
56 ModStruct   sierpinski_description =
57 {"sierpinski", "init_sierpinski", "draw_sierpinski", "release_sierpinski",
58  "refresh_sierpinski", "init_sierpinski", (char *) NULL, &sierpinski_opts,
59  400000, 2000, 100, 1, 64, 1.0, "",
60  "Shows Sierpinski's triangle", 0, NULL};
61
62 #endif
63
64 #define MAXCORNERS 4
65
66 typedef struct {
67         int         width, height;
68         int         time;
69         int         px, py;
70         int         total_npoints;
71         int         corners;
72         int         npoints[MAXCORNERS];
73         unsigned long colors[MAXCORNERS];
74         XPoint     *pointBuffer[MAXCORNERS];
75         XPoint      vertex[MAXCORNERS];
76 } sierpinskistruct;
77
78 static sierpinskistruct *tris = (sierpinskistruct *) NULL;
79
80 static void
81 startover(ModeInfo * mi)
82 {
83         int         j;
84         sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
85
86         if (MI_NPIXELS(mi) > 2) {
87                 if (sp->corners == 3) {
88                         sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
89                         sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
90                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
91                         sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 +
92                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
93                 } else if (sp->corners == 4) {
94                         sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
95                         sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
96                              NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
97                         sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 +
98                              NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
99                         sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 +
100                              NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
101                 } else {
102                         (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
103                 }
104         }
105         for (j = 0; j < sp->corners; j++) {
106                 sp->vertex[j].x = NRAND(sp->width);
107                 sp->vertex[j].y = NRAND(sp->height);
108         }
109         sp->px = NRAND(sp->width);
110         sp->py = NRAND(sp->height);
111         sp->time = 0;
112
113         MI_CLEARWINDOW(mi);
114 }
115
116 static void
117 free_sierpinski(sierpinskistruct *sp)
118 {
119         int corner;
120
121         for (corner = 0; corner < MAXCORNERS; corner++)
122                 if (sp->pointBuffer[corner] != NULL) {
123                         (void) free((void *) sp->pointBuffer[corner]);
124                         sp->pointBuffer[corner] = (XPoint *) NULL;
125                 }
126 }
127
128 ENTRYPOINT void
129 init_sierpinski(ModeInfo * mi)
130 {
131         int         i;
132         sierpinskistruct *sp;
133
134         if (tris == NULL) {
135                 if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi),
136                                          sizeof (sierpinskistruct))) == NULL)
137                         return;
138         }
139         sp = &tris[MI_SCREEN(mi)];
140
141         sp->width = MI_WIDTH(mi);
142         sp->height = MI_HEIGHT(mi);
143
144         sp->total_npoints = MI_COUNT(mi);
145         if (sp->total_npoints < 1)
146                 sp->total_npoints = 1;
147         sp->corners = MI_SIZE(mi);
148         if (sp->corners < 3 || sp->corners > 4) {
149                 sp->corners = (int) (LRAND() & 1) + 3;
150         }
151         for (i = 0; i < sp->corners; i++) {
152                 if (!sp->pointBuffer[i])
153                         if ((sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints *
154                                         sizeof (XPoint))) == NULL) {
155                                 free_sierpinski(sp);
156                                 return;
157                         }
158         }
159         startover(mi);
160 }
161
162 ENTRYPOINT void
163 draw_sierpinski(ModeInfo * mi)
164 {
165         Display    *display = MI_DISPLAY(mi);
166         GC          gc = MI_GC(mi);
167         XPoint     *xp[MAXCORNERS];
168         int         i, v;
169         sierpinskistruct *sp;
170
171         if (tris == NULL)
172                 return;
173         sp = &tris[MI_SCREEN(mi)];
174         if (sp->pointBuffer[0] == NULL)
175                 return;
176
177         MI_IS_DRAWN(mi) = True;
178         if (MI_NPIXELS(mi) <= 2)
179                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
180         for (i = 0; i < sp->corners; i++)
181                 xp[i] = sp->pointBuffer[i];
182         for (i = 0; i < sp->total_npoints; i++) {
183                 v = NRAND(sp->corners);
184                 sp->px = (sp->px + sp->vertex[v].x) / 2;
185                 sp->py = (sp->py + sp->vertex[v].y) / 2;
186                 xp[v]->x = sp->px;
187                 xp[v]->y = sp->py;
188                 xp[v]++;
189                 sp->npoints[v]++;
190         }
191         for (i = 0; i < sp->corners; i++) {
192                 if (MI_NPIXELS(mi) > 2)
193                         XSetForeground(display, gc, MI_PIXEL(mi, sp->colors[i]));
194                 XDrawPoints(display, MI_WINDOW(mi), gc, sp->pointBuffer[i], sp->npoints[i],
195                             CoordModeOrigin);
196                 sp->npoints[i] = 0;
197         }
198         if (++sp->time >= MI_CYCLES(mi))
199                 startover(mi);
200 }
201
202 ENTRYPOINT void
203 reshape_sierpinski(ModeInfo * mi, int width, int height)
204 {
205   XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
206   init_sierpinski (mi);
207 }
208
209 ENTRYPOINT void
210 release_sierpinski(ModeInfo * mi)
211 {
212         if (tris != NULL) {
213                 int         screen;
214
215                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
216                         free_sierpinski(&tris[screen]);
217                 (void) free((void *) tris);
218                 tris = (sierpinskistruct *) NULL;
219         }
220 }
221
222 ENTRYPOINT void
223 refresh_sierpinski(ModeInfo * mi)
224 {
225         MI_CLEARWINDOW(mi);
226 }
227
228 XSCREENSAVER_MODULE ("Sierpinski", sierpinski)
229
230 #endif /* MODE_sierpinski */