f6e18181dc93cd8d70b9733bb46c2b04511e5476
[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 # define BRIGHT_COLORS
42 # define reshape_sierpinski 0
43 # define sierpinski_handle_event 0
44 # include "xlockmore.h"         /* in xscreensaver distribution */
45 #else /* STANDALONE */
46 # include "xlock.h"             /* in xlockmore distribution */
47 #endif /* STANDALONE */
48
49 #ifdef MODE_sierpinski
50
51 ENTRYPOINT ModeSpecOpt sierpinski_opts =
52 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
53
54 #ifdef USE_MODULES
55 ModStruct   sierpinski_description =
56 {"sierpinski", "init_sierpinski", "draw_sierpinski", "release_sierpinski",
57  "refresh_sierpinski", "init_sierpinski", (char *) NULL, &sierpinski_opts,
58  400000, 2000, 100, 1, 64, 1.0, "",
59  "Shows Sierpinski's triangle", 0, NULL};
60
61 #endif
62
63 #define MAXCORNERS 4
64
65 typedef struct {
66         int         width, height;
67         int         time;
68         int         px, py;
69         int         total_npoints;
70         int         corners;
71         int         npoints[MAXCORNERS];
72         unsigned long colors[MAXCORNERS];
73         XPoint     *pointBuffer[MAXCORNERS];
74         XPoint      vertex[MAXCORNERS];
75 } sierpinskistruct;
76
77 static sierpinskistruct *tris = (sierpinskistruct *) NULL;
78
79 static void
80 startover(ModeInfo * mi)
81 {
82         int         j;
83         sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
84
85         if (MI_NPIXELS(mi) > 2) {
86                 if (sp->corners == 3) {
87                         sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
88                         sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
89                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
90                         sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 +
91                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
92                 } else if (sp->corners == 4) {
93                         sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
94                         sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
95                              NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
96                         sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 +
97                              NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
98                         sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 +
99                              NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
100                 } else {
101                         (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
102                 }
103         }
104         for (j = 0; j < sp->corners; j++) {
105                 sp->vertex[j].x = NRAND(sp->width);
106                 sp->vertex[j].y = NRAND(sp->height);
107         }
108         sp->px = NRAND(sp->width);
109         sp->py = NRAND(sp->height);
110         sp->time = 0;
111
112         MI_CLEARWINDOW(mi);
113 }
114
115 static void
116 free_sierpinski(sierpinskistruct *sp)
117 {
118         int corner;
119
120         for (corner = 0; corner < MAXCORNERS; corner++)
121                 if (sp->pointBuffer[corner] != NULL) {
122                         (void) free((void *) sp->pointBuffer[corner]);
123                         sp->pointBuffer[corner] = (XPoint *) NULL;
124                 }
125 }
126
127 ENTRYPOINT void
128 init_sierpinski(ModeInfo * mi)
129 {
130         int         i;
131         sierpinskistruct *sp;
132
133         if (tris == NULL) {
134                 if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi),
135                                          sizeof (sierpinskistruct))) == NULL)
136                         return;
137         }
138         sp = &tris[MI_SCREEN(mi)];
139
140         sp->width = MI_WIDTH(mi);
141         sp->height = MI_HEIGHT(mi);
142
143         sp->total_npoints = MI_COUNT(mi);
144         if (sp->total_npoints < 1)
145                 sp->total_npoints = 1;
146         sp->corners = MI_SIZE(mi);
147         if (sp->corners < 3 || sp->corners > 4) {
148                 sp->corners = (int) (LRAND() & 1) + 3;
149         }
150         for (i = 0; i < sp->corners; i++) {
151                 if (!sp->pointBuffer[i])
152                         if ((sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints *
153                                         sizeof (XPoint))) == NULL) {
154                                 free_sierpinski(sp);
155                                 return;
156                         }
157         }
158         startover(mi);
159 }
160
161 ENTRYPOINT void
162 draw_sierpinski(ModeInfo * mi)
163 {
164         Display    *display = MI_DISPLAY(mi);
165         GC          gc = MI_GC(mi);
166         XPoint     *xp[MAXCORNERS];
167         int         i, v;
168         sierpinskistruct *sp;
169
170         if (tris == NULL)
171                 return;
172         sp = &tris[MI_SCREEN(mi)];
173         if (sp->pointBuffer[0] == NULL)
174                 return;
175
176         MI_IS_DRAWN(mi) = True;
177         if (MI_NPIXELS(mi) <= 2)
178                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
179         for (i = 0; i < sp->corners; i++)
180                 xp[i] = sp->pointBuffer[i];
181         for (i = 0; i < sp->total_npoints; i++) {
182                 v = NRAND(sp->corners);
183                 sp->px = (sp->px + sp->vertex[v].x) / 2;
184                 sp->py = (sp->py + sp->vertex[v].y) / 2;
185                 xp[v]->x = sp->px;
186                 xp[v]->y = sp->py;
187                 xp[v]++;
188                 sp->npoints[v]++;
189         }
190         for (i = 0; i < sp->corners; i++) {
191                 if (MI_NPIXELS(mi) > 2)
192                         XSetForeground(display, gc, MI_PIXEL(mi, sp->colors[i]));
193                 XDrawPoints(display, MI_WINDOW(mi), gc, sp->pointBuffer[i], sp->npoints[i],
194                             CoordModeOrigin);
195                 sp->npoints[i] = 0;
196         }
197         if (++sp->time >= MI_CYCLES(mi))
198                 startover(mi);
199 }
200
201 ENTRYPOINT void
202 release_sierpinski(ModeInfo * mi)
203 {
204         if (tris != NULL) {
205                 int         screen;
206
207                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
208                         free_sierpinski(&tris[screen]);
209                 (void) free((void *) tris);
210                 tris = (sierpinskistruct *) NULL;
211         }
212 }
213
214 ENTRYPOINT void
215 refresh_sierpinski(ModeInfo * mi)
216 {
217         MI_CLEARWINDOW(mi);
218 }
219
220 XSCREENSAVER_MODULE ("Sierpinski", sierpinski)
221
222 #endif /* MODE_sierpinski */