1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sierpinski --- Sierpinski's triangle fractal */
5 static const char sccsid[] = "@(#)sierpinski.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1996 by Desmond Daignault
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.
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.
23 * Dots initially appear where they "should not". Later they get
24 * "focused". This is correct behavior.
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> .
36 # define MODE_sierpinski
37 # define DEFAULTS "*delay: 400000 \n" \
41 "*fpsSolid: true \n" \
42 "*ignoreRotation: True \n" \
44 # define BRIGHT_COLORS
45 # define release_sierpinski 0
46 # define sierpinski_handle_event 0
47 # include "xlockmore.h" /* in xscreensaver distribution */
48 #else /* STANDALONE */
49 # include "xlock.h" /* in xlockmore distribution */
50 #endif /* STANDALONE */
52 #ifdef MODE_sierpinski
54 ENTRYPOINT ModeSpecOpt sierpinski_opts =
55 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
58 ModStruct sierpinski_description =
59 {"sierpinski", "init_sierpinski", "draw_sierpinski", (char *) NULL,
60 "refresh_sierpinski", "init_sierpinski", (char *) NULL, &sierpinski_opts,
61 400000, 2000, 100, 1, 64, 1.0, "",
62 "Shows Sierpinski's triangle", 0, NULL};
74 int npoints[MAXCORNERS];
75 unsigned long colors[MAXCORNERS];
76 XPoint *pointBuffer[MAXCORNERS];
77 XPoint vertex[MAXCORNERS];
80 static sierpinskistruct *tris = (sierpinskistruct *) NULL;
83 startover(ModeInfo * mi)
86 sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
88 if (MI_NPIXELS(mi) > 2) {
89 if (sp->corners == 3) {
90 sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
91 sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
92 NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
93 sp->colors[2] = (sp->colors[0] + 4 * MI_NPIXELS(mi) / 7 +
94 NRAND(2 * MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
95 } else if (sp->corners == 4) {
96 sp->colors[0] = (NRAND(MI_NPIXELS(mi)));
97 sp->colors[1] = (sp->colors[0] + MI_NPIXELS(mi) / 7 +
98 NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
99 sp->colors[2] = (sp->colors[0] + 3 * MI_NPIXELS(mi) / 7 +
100 NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
101 sp->colors[3] = (sp->colors[0] + 5 * MI_NPIXELS(mi) / 7 +
102 NRAND(MI_NPIXELS(mi) / 7 + 1)) % MI_NPIXELS(mi);
104 (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
107 for (j = 0; j < sp->corners; j++) {
108 sp->vertex[j].x = NRAND(sp->width);
109 sp->vertex[j].y = NRAND(sp->height);
111 sp->px = NRAND(sp->width);
112 sp->py = NRAND(sp->height);
119 free_sierpinski(ModeInfo * mi)
121 sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
124 for (corner = 0; corner < MAXCORNERS; corner++)
125 if (sp->pointBuffer[corner] != NULL) {
126 (void) free((void *) sp->pointBuffer[corner]);
127 sp->pointBuffer[corner] = (XPoint *) NULL;
132 init_sierpinski(ModeInfo * mi)
135 sierpinskistruct *sp;
137 MI_INIT (mi, tris, free_sierpinski);
138 sp = &tris[MI_SCREEN(mi)];
140 sp->width = MI_WIDTH(mi);
141 sp->height = MI_HEIGHT(mi);
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;
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) {
162 draw_sierpinski(ModeInfo * mi)
164 Display *display = MI_DISPLAY(mi);
166 XPoint *xp[MAXCORNERS];
168 sierpinskistruct *sp;
172 sp = &tris[MI_SCREEN(mi)];
173 if (sp->pointBuffer[0] == NULL)
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;
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],
197 if (++sp->time >= MI_CYCLES(mi))
202 reshape_sierpinski(ModeInfo * mi, int width, int height)
204 XClearWindow (MI_DISPLAY (mi), MI_WINDOW(mi));
205 init_sierpinski (mi);
209 refresh_sierpinski(ModeInfo * mi)
214 XSCREENSAVER_MODULE ("Sierpinski", sierpinski)
216 #endif /* MODE_sierpinski */