1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sierpinski --- Sierpinski's triangle fractal */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)sierpinski.c 4.05 97/09/19 xlockmore";
8 /* Copyright (c) 1996 by Desmond Daignault
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.
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.
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> .
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" \
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 */
45 ModeSpecOpt sierpinski_opts =
46 {0, NULL, 0, NULL, NULL};
56 int npoints[MAXCORNERS];
57 unsigned long colors[MAXCORNERS];
58 XPoint *pointBuffer[MAXCORNERS];
59 XPoint vertex[MAXCORNERS];
62 static sierpinskistruct *tris = NULL;
65 startover(ModeInfo * mi)
68 sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
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);
86 (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
89 for (j = 0; j < sp->corners; j++) {
90 sp->vertex[j].x = NRAND(sp->width);
91 sp->vertex[j].y = NRAND(sp->height);
93 sp->px = NRAND(sp->width);
94 sp->py = NRAND(sp->height);
96 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
100 init_sierpinski(ModeInfo * mi)
102 sierpinskistruct *sp;
106 if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi),
107 sizeof (sierpinskistruct))) == NULL)
110 sp = &tris[MI_SCREEN(mi)];
112 sp->width = MI_WIN_WIDTH(mi);
113 sp->height = MI_WIN_HEIGHT(mi);
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;
122 for (i = 0; i < sp->corners; i++) {
123 if (!sp->pointBuffer[i])
124 sp->pointBuffer[i] = (XPoint *) malloc(sp->total_npoints *
131 draw_sierpinski(ModeInfo * mi)
133 Display *display = MI_DISPLAY(mi);
135 sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
136 XPoint *xp[MAXCORNERS];
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;
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],
159 if (++sp->time >= MI_CYCLES(mi))
164 release_sierpinski(ModeInfo * mi)
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]);
175 (void) free((void *) tris);
181 refresh_sierpinski(ModeInfo * mi)
183 /* Do nothing, it will refresh by itself */