1 /* -*- Mode: C; tab-width: 4 -*-
2 * tri --- Sierpinski triangle fractal.
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)tri.c 4.00 97/01/01 xlockmore";
8 /* Copyright (c) 1988-91 by Patrick J. Naughton.
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 * 10-May-97: jwz@netscape.com: turned into a standalone program.
24 * 05-Sep-96: Desmond Daignault Datatimes Incorporated
25 * <tekdd@dtol.datatimes.com> .
29 # define PROGCLASS "Sierpinski"
30 # define HACK_INIT init_tri
31 # define HACK_DRAW draw_tri
32 # define tri_opts xlockmore_opts
33 # define DEFAULTS "*count: 2000 \n" \
37 # include "xlockmore.h" /* from the xscreensaver distribution */
38 #else /* !STANDALONE */
39 # include "xlock.h" /* from the xlockmore distribution */
40 #endif /* !STANDALONE */
42 ModeSpecOpt tri_opts = {
43 0, NULL, 0, NULL, NULL };
51 unsigned long colors[3];
52 XPoint *pointBuffer[3];
56 static tristruct *tris = NULL;
59 startover(ModeInfo * mi)
62 tristruct *tp = &tris[MI_SCREEN(mi)];
64 if (MI_NPIXELS(mi) > 2) {
65 tp->colors[0] = (NRAND(MI_NPIXELS(mi)));
66 tp->colors[1] = (tp->colors[0] + MI_NPIXELS(mi) / 7 +
67 NRAND(2 * MI_NPIXELS(mi) / 7)) % MI_NPIXELS(mi);
68 tp->colors[2] = (tp->colors[0] + 4 * MI_NPIXELS(mi) / 7 +
69 NRAND(2 * MI_NPIXELS(mi) / 7)) % MI_NPIXELS(mi);
71 for (j = 0; j < 3; j++) {
72 tp->vertex[j].x = NRAND(tp->width);
73 tp->vertex[j].y = NRAND(tp->height);
75 tp->px = NRAND(tp->width);
76 tp->py = NRAND(tp->height);
78 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
82 init_tri(ModeInfo * mi)
88 if ((tris = (tristruct *) calloc(MI_NUM_SCREENS(mi),
89 sizeof (tristruct))) == NULL)
92 tp = &tris[MI_SCREEN(mi)];
94 tp->width = MI_WIN_WIDTH(mi);
95 tp->height = MI_WIN_HEIGHT(mi);
97 tp->total_npoints = MI_BATCHCOUNT(mi);
98 if (tp->total_npoints < 1)
99 tp->total_npoints = 1;
100 for (i = 0; i < 3; i++) {
101 if (!tp->pointBuffer[i])
102 tp->pointBuffer[i] = (XPoint *) malloc(tp->total_npoints *
109 draw_tri(ModeInfo * mi)
111 Display *display = MI_DISPLAY(mi);
113 tristruct *tp = &tris[MI_SCREEN(mi)];
117 if (MI_NPIXELS(mi) <= 2)
118 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
119 for (i = 0; i < 3; i++)
120 xp[i] = tp->pointBuffer[i];
121 for (i = 0; i < tp->total_npoints; i++) {
123 tp->px = (tp->px + tp->vertex[v].x) / 2;
124 tp->py = (tp->py + tp->vertex[v].y) / 2;
130 for (i = 0; i < 3; i++) {
131 if (MI_NPIXELS(mi) > 2)
132 XSetForeground(display, gc, MI_PIXEL(mi, tp->colors[i]));
133 XDrawPoints(display, MI_WINDOW(mi), gc, tp->pointBuffer[i], tp->npoints[i],
137 if (++tp->time >= MI_CYCLES(mi))
142 release_tri(ModeInfo * mi)
147 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
148 for (i = 0; i < 3; i++)
149 if (tris[screen].pointBuffer[i] != NULL) {
150 (void) free((void *) tris[screen].pointBuffer[i]);
153 (void) free((void *) tris);
159 refresh_tri(ModeInfo * mi)
161 /* Do nothing, it will refresh by itself */