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@netscape.com: 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 "*count: 2000 \n" \
39 # include "xlockmore.h" /* from the xscreensaver distribution */
40 #else /* !STANDALONE */
41 # include "xlock.h" /* from the xlockmore distribution */
42 #endif /* !STANDALONE */
44 ModeSpecOpt sierpinski_opts =
45 {0, NULL, 0, NULL, NULL};
55 int npoints[MAXCORNERS];
56 unsigned long colors[MAXCORNERS];
57 XPoint *pointBuffer[MAXCORNERS];
58 XPoint vertex[MAXCORNERS];
61 static sierpinskistruct *tris = NULL;
64 startover(ModeInfo * mi)
67 sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
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);
85 (void) fprintf(stderr, "colors not set for %d corners\n", sp->corners);
88 for (j = 0; j < sp->corners; j++) {
89 sp->vertex[j].x = NRAND(sp->width);
90 sp->vertex[j].y = NRAND(sp->height);
92 sp->px = NRAND(sp->width);
93 sp->py = NRAND(sp->height);
95 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
99 init_sierpinski(ModeInfo * mi)
101 sierpinskistruct *sp;
105 if ((tris = (sierpinskistruct *) calloc(MI_NUM_SCREENS(mi),
106 sizeof (sierpinskistruct))) == NULL)
109 sp = &tris[MI_SCREEN(mi)];
111 sp->width = MI_WIN_WIDTH(mi);
112 sp->height = MI_WIN_HEIGHT(mi);
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 *
127 draw_sierpinski(ModeInfo * mi)
129 Display *display = MI_DISPLAY(mi);
131 sierpinskistruct *sp = &tris[MI_SCREEN(mi)];
135 xp = (XPoint **) malloc (sp->corners * sizeof (XPoint *));
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;
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],
157 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 */