ca22999c147f0dd083ad443dbc5e3255b806277a
[xscreensaver] / hacks / sierpinski.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sierpinski --- Sierpinski's triangle fractal */
3
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)sierpinski.c  4.05 97/09/19 xlockmore";
6 #endif
7
8 /* Copyright (c) 1996 by Desmond Daignault
9  *
10  * Permission to use, copy, modify, and distribute this software and its
11  * documentation for any purpose and without fee is hereby granted,
12  * provided that the above copyright notice appear in all copies and that
13  * both that copyright notice and this permission notice appear in
14  * supporting documentation.
15  *
16  * This file is provided AS IS with no warranties of any kind.  The author
17  * shall have no liability with respect to the infringement of copyrights,
18  * trade secrets or any patents by this file or any part thereof.  In no
19  * event will the author be liable for any lost revenue or profits or
20  * other special, indirect and consequential damages.
21  *
22  * Revision History:
23  * 18-Sep-97: 3D version Antti Kuntsi <kuntsi@iki.fi>.
24  * 20-May-97: Changed the name tri to sierpinski for more compatiblity
25  * 10-May-97: jwz@jwz.org: turned into a standalone program.
26  * 05-Sep-96: Desmond Daignault Datatimes Incorporated
27  *            <tekdd@dtol.datatimes.com> .
28  */
29
30 #ifdef STANDALONE
31 # define PROGCLASS                                      "Sierpinski"
32 # define HACK_INIT                                      init_sierpinski
33 # define HACK_DRAW                                      draw_sierpinski
34 # define sierpinski_opts                        xlockmore_opts
35 # define DEFAULTS       "*delay:                400000  \n"                     \
36                                         "*count:                2000    \n"                     \
37                                         "*cycles:               100     \n"                     \
38                                         "*ncolors:              64   \n"
39 # define BRIGHT_COLORS
40 # include "xlockmore.h"                         /* from the xscreensaver distribution */
41 #else  /* !STANDALONE */
42 # include "xlock.h"                                     /* from the xlockmore distribution */
43 #endif /* !STANDALONE */
44
45 ModeSpecOpt sierpinski_opts =
46 {0, NULL, 0, NULL, NULL};
47
48 #define MAXCORNERS 4
49
50 typedef struct {
51         int         width, height;
52         int         time;
53         int         px, py;
54         int         total_npoints;
55   int         corners;
56         int         npoints[MAXCORNERS];
57         unsigned long colors[MAXCORNERS];
58         XPoint     *pointBuffer[MAXCORNERS];
59         XPoint      vertex[MAXCORNERS];
60 } sierpinskistruct;
61
62 static sierpinskistruct *tris = NULL;
63
64 static void
65 startover(ModeInfo * mi)
66 {
67         int         j;
68         sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
69
70         if (MI_NPIXELS(mi) > 2) {
71     if (sp->corners == 3) {
72                 sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
73                 sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
74                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
75                 sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 +
76                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
77    } else if (sp->corners == 4) {
78                 sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
79                 sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
80                          NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
81                 sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 +
82                          NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
83                 sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 +
84                          NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
85    } else {
86      (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
87          }
88         }
89         for (j = 0; j < sp->corners; j++) {
90                 sp->vertex[j].x = NRAND(sp->width);
91                 sp->vertex[j].y = NRAND(sp->height);
92         }
93         sp->px = NRAND(sp->width);
94         sp->py = NRAND(sp->height);
95         sp->time = 0;
96         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
97 }
98
99 void
100 init_sierpinski(ModeInfo * mi)
101 {
102         sierpinskistruct *sp;
103         int         i;
104
105         if (tris == NULL) {
106                 if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi),
107                                          sizeof (sierpinskistruct))) == NULL)
108                         return;
109         }
110         sp = &tris[MI_SCREEN(mi)];
111
112         sp->width = MI_WIN_WIDTH(mi);
113         sp->height = MI_WIN_HEIGHT(mi);
114
115         sp->total_npoints = MI_BATCHCOUNT(mi);
116         if (sp->total_npoints < 1)
117                 sp->total_npoints = 1;
118   sp->corners = MI_SIZE(mi);
119         if (sp->corners < 3 || sp->corners > 4) {
120                 sp->corners = (LRAND() & 1) + 3;
121         }
122         for (i = 0; i < sp->corners; i++) {
123                 if (!sp->pointBuffer[i])
124                         sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints *
125                                                             sizeof (XPoint));
126         }
127         startover(mi);
128 }
129
130 void
131 draw_sierpinski(ModeInfo * mi)
132 {
133         Display    *display = MI_DISPLAY(mi);
134         GC          gc = MI_GC(mi);
135         sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
136         XPoint     *xp[MAXCORNERS];
137         int         i = 0, v;
138
139         if (MI_NPIXELS(mi) <= 2)
140                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
141         for (i = 0; i < sp->corners; i++)
142                 xp[i] = sp->pointBuffer[i];
143         for (i = 0; i < sp->total_npoints; i++) {
144                 v = NRAND(sp->corners);
145                 sp->px = (sp->px + sp->vertex[v].x) / 2;
146                 sp->py = (sp->py + sp->vertex[v].y) / 2;
147                 xp[v]->x = sp->px;
148                 xp[v]->y = sp->py;
149                 xp[v]++;
150                 sp->npoints[v]++;
151         }
152         for (i = 0; i < sp->corners; i++) {
153                 if (MI_NPIXELS(mi) > 2)
154                         XSetForeground(display, gc, MI_PIXEL(mi, sp->colors[i]));
155                 XDrawPoints(display, MI_WINDOW(mi), gc, sp->pointBuffer[i], sp->npoints[i],
156                             CoordModeOrigin);
157                 sp->npoints[i] = 0;
158         }
159         if (++sp->time >= MI_CYCLES(mi))
160                 startover(mi);
161 }
162
163 void
164 release_sierpinski(ModeInfo * mi)
165 {
166         if (tris != NULL) {
167                 int         screen, i;
168
169                 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
170                         for (i = 0; i < MAXCORNERS; i++)
171                                 if (tris[screen].pointBuffer[i] != NULL) {
172                                         (void) free((void *) tris[screen].pointBuffer[i]);
173                                 }
174                 }
175                 (void) free((void *) tris);
176                 tris = NULL;
177         }
178 }
179
180 void
181 refresh_sierpinski(ModeInfo * mi)
182 {
183         /* Do nothing, it will refresh by itself */
184 }