ftp://ftp.sunet.se/pub/os/Linux/distributions/ultrapenguin/ultrapenguin-1.1/SRPMS...
[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@netscape.com: 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       "*count:                2000    \n"                     \
36                                         "*cycles:               100     \n"                     \
37                                         "*delay:                400000  \n"                     \
38                                         "*ncolors:              64   \n"
39 # include "xlockmore.h"                         /* from the xscreensaver distribution */
40 #else  /* !STANDALONE */
41 # include "xlock.h"                                     /* from the xlockmore distribution */
42 #endif /* !STANDALONE */
43
44 ModeSpecOpt sierpinski_opts =
45 {0, NULL, 0, NULL, NULL};
46
47 #define MAXCORNERS 4
48
49 typedef struct {
50         int         width, height;
51         int         time;
52         int         px, py;
53         int         total_npoints;
54   int         corners;
55         int         npoints[MAXCORNERS];
56         unsigned long colors[MAXCORNERS];
57         XPoint     *pointBuffer[MAXCORNERS];
58         XPoint      vertex[MAXCORNERS];
59 } sierpinskistruct;
60
61 static sierpinskistruct *tris = NULL;
62
63 static void
64 startover(ModeInfo * mi)
65 {
66         int         j;
67         sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
68
69         if (MI_NPIXELS(mi) > 2) {
70     if (sp->corners == 3) {
71                 sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
72                 sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
73                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
74                 sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 +
75                          NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
76    } else if (sp->corners == 4) {
77                 sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
78                 sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
79                          NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
80                 sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 +
81                          NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
82                 sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 +
83                          NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
84    } else {
85      (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
86          }
87         }
88         for (j = 0; j < sp->corners; j++) {
89                 sp->vertex[j].x = NRAND(sp->width);
90                 sp->vertex[j].y = NRAND(sp->height);
91         }
92         sp->px = NRAND(sp->width);
93         sp->py = NRAND(sp->height);
94         sp->time = 0;
95         XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
96 }
97
98 void
99 init_sierpinski(ModeInfo * mi)
100 {
101         sierpinskistruct *sp;
102         int         i;
103
104         if (tris == NULL) {
105                 if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi),
106                                          sizeof (sierpinskistruct))) == NULL)
107                         return;
108         }
109         sp = &tris[MI_SCREEN(mi)];
110
111         sp->width = MI_WIN_WIDTH(mi);
112         sp->height = MI_WIN_HEIGHT(mi);
113
114         sp->total_npoints = MI_BATCHCOUNT(mi);
115         if (sp->total_npoints < 1)
116                 sp->total_npoints = 1;
117   sp->corners = (LRAND() & 1) + 3;
118         for (i = 0; i < sp->corners; i++) {
119                 if (!sp->pointBuffer[i])
120                         sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints *
121                                                             sizeof (XPoint));
122         }
123         startover(mi);
124 }
125
126 void
127 draw_sierpinski(ModeInfo * mi)
128 {
129         Display    *display = MI_DISPLAY(mi);
130         GC          gc = MI_GC(mi);
131         sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
132         XPoint     **xp;
133         int         i = 0, v;
134
135     xp = (XPoint **) malloc (sp->corners * sizeof (XPoint *));
136     
137         if (MI_NPIXELS(mi) <= 2)
138                 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
139         for (i = 0; i < sp->corners; i++)
140                 xp[i] = sp->pointBuffer[i];
141         for (i = 0; i < sp->total_npoints; i++) {
142                 v = NRAND(sp->corners);
143                 sp->px = (sp->px + sp->vertex[v].x) / 2;
144                 sp->py = (sp->py + sp->vertex[v].y) / 2;
145                 xp[v]->x = sp->px;
146                 xp[v]->y = sp->py;
147                 xp[v]++;
148                 sp->npoints[v]++;
149         }
150         for (i = 0; i < sp->corners; i++) {
151                 if (MI_NPIXELS(mi) > 2)
152                         XSetForeground(display, gc, MI_PIXEL(mi, sp->colors[i]));
153                 XDrawPoints(display, MI_WINDOW(mi), gc, sp->pointBuffer[i], sp->npoints[i],
154                             CoordModeOrigin);
155                 sp->npoints[i] = 0;
156         }
157         if (++sp->time >= MI_CYCLES(mi))
158                 startover(mi);
159
160     free (xp);
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 }