1 /* -*- Mode: C; tab-width: 4 -*- */
3 * braid --- random braids around a circle and then changes the color in
8 static const char sccsid[] = "@(#)braid.c 5.00 2000/11/01 xlockmore";
12 * Copyright (c) 1995 by John Neil.
14 * Permission to use, copy, modify, and distribute this software and its
15 * documentation for any purpose and without fee is hereby granted,
16 * provided that the above copyright notice appear in all copies and that
17 * both that copyright notice and this permission notice appear in
18 * supporting documentation.
20 * This file is provided AS IS with no warranties of any kind. The author
21 * shall have no liability with respect to the infringement of copyrights,
22 * trade secrets or any patents by this file or any part thereof. In no
23 * event will the author be liable for any lost revenue or profits or
24 * other special, indirect and consequential damages.
27 * 01-Nov-2000: Allocation checks
28 * 10-May-1997: Jamie Zawinski <jwz@jwz.org> compatible with xscreensaver
29 * 01-Sep-1995: color knotted components differently, J. Neil.
30 * 29-Aug-1995: Written. John Neil <neil@math.idbsu.edu>
35 # define DEFAULTS "*delay: 1000 \n" \
40 "*fpsSolid: true \n" \
42 # define UNIFORM_COLORS
43 # define reshape_braid 0
44 # define braid_handle_event 0
45 # include "xlockmore.h"
47 #else /* STANDALONE */
49 # define ENTRYPOINT /**/
50 #endif /* STANDALONE */
54 ENTRYPOINT ModeSpecOpt braid_opts = {0, NULL, 0, NULL, NULL};
57 ModStruct braid_description =
58 {"braid", "init_braid", "draw_braid", "release_braid",
59 "refresh_braid", "init_braid", (char *) NULL, &braid_opts,
60 1000, 15, 100, 1, 64, 1.0, "",
61 "Shows random braids and knots", 0, NULL};
65 #if defined( COLORROUND ) && defined( COLORCOMP )
70 #if !defined( COLORROUND ) && !defined( COLORCOMP )
72 /* to color in a circular pattern use COLORROUND */
75 /* to color by component use COLORCOMP */
80 #define MAXLENGTH 50 /* the maximum length of a braid word */
81 #define MINLENGTH 8 /* the minimum length of a braid word */
82 #define MAXSTRANDS 15 /* the maximum number of strands in the braid */
83 #define MINSTRANDS 3 /* the minimum number of strands in the braid */
84 #define SPINRATE 12.0 /* the rate at which the colors spin */
86 #define INTRAND(min,max) (NRAND((max+1)-(min))+(min))
87 #define FLOATRAND(min,max) ((min)+((double) LRAND()/((double) MAXRAND))*((max)-(min)))
91 int braidword[MAXLENGTH];
92 int components[MAXSTRANDS];
93 int startcomp[MAXLENGTH][MAXSTRANDS];
101 float top, bottom, left, right;
105 eraser_state *eraser;
109 static braidtype *braids = (braidtype *) NULL;
112 applyword(braidtype * braid, int string, int position)
117 for (i = position; i < braid->braidlength; i++) {
118 if (c == ABS(braid->braidword[i]))
120 else if (c == ABS(braid->braidword[i]) - 1)
123 for (i = 0; i < position; i++) {
124 if (c == ABS(braid->braidword[i]))
126 else if (c == ABS(braid->braidword[i]) - 1)
134 applywordto(braidtype * braid, int string, int position)
139 for (i = 0; i < position; i++) {
140 if (c == ABS(braid->braidword[i])) {
142 } else if (c == ABS(braid->braidword[i]) - 1) {
151 applywordbackto(braidtype * braid, int string, int position)
156 for (i = position - 1; i >= 0; i--) {
157 if (c == ABS(braid->braidword[i])) {
159 } else if (c == ABS(braid->braidword[i]) - 1) {
167 init_braid(ModeInfo * mi)
170 int used[MAXSTRANDS];
171 int i, count, comp, c;
174 if (braids == NULL) {
175 if ((braids = (braidtype *) calloc(MI_NUM_SCREENS(mi),
176 sizeof (braidtype))) == NULL)
179 braid = &braids[MI_SCREEN(mi)];
181 braid->center_x = MI_WIDTH(mi) / 2;
182 braid->center_y = MI_HEIGHT(mi) / 2;
185 /* jwz: go in the other direction sometimes. */
186 braid->color_direction = ((LRAND() & 1) ? 1 : -1);
192 min_length = (braid->center_x > braid->center_y) ?
193 braid->center_y : braid->center_x;
194 braid->min_radius = min_length * 0.30;
195 braid->max_radius = min_length * 0.90;
197 if (MI_COUNT(mi) < MINSTRANDS)
198 braid->nstrands = MINSTRANDS;
200 braid->nstrands = INTRAND(MINSTRANDS,
201 MAX(MIN(MIN(MAXSTRANDS, MI_COUNT(mi)),
202 (int) ((braid->max_radius - braid->min_radius) / 5.0)), MINSTRANDS));
203 braid->braidlength = INTRAND(MINLENGTH, MIN(MAXLENGTH, braid->nstrands * 6));
205 for (i = 0; i < braid->braidlength; i++) {
206 braid->braidword[i] =
207 INTRAND(1, braid->nstrands - 1) * (INTRAND(1, 2) * 2 - 3);
209 while (braid->braidword[i] == -braid->braidword[i - 1])
210 braid->braidword[i] = INTRAND(1, braid->nstrands - 1) * (INTRAND(1, 2) * 2 - 3);
213 while (braid->braidword[0] == -braid->braidword[braid->braidlength - 1])
214 braid->braidword[braid->braidlength - 1] =
215 INTRAND(1, braid->nstrands - 1) * (INTRAND(1, 2) * 2 - 3);
218 (void) memset((char *) used, 0, sizeof (used));
220 for (i = 0; i < braid->braidlength; i++)
221 used[ABS(braid->braidword[i])]++;
222 for (i = 0; i < braid->nstrands; i++)
223 count += (used[i] > 0) ? 1 : 0;
224 if (count < braid->nstrands - 1) {
225 braid->braidword[braid->braidlength] =
226 INTRAND(1, braid->nstrands - 1) * (INTRAND(1, 2) * 2 - 3);
227 while (braid->braidword[braid->braidlength] ==
228 -braid->braidword[braid->braidlength - 1] &&
229 braid->braidword[0] == -braid->braidword[braid->braidlength])
230 braid->braidword[braid->braidlength] =
231 INTRAND(1, braid->nstrands - 1) * (INTRAND(1, 2) * 2 - 3);
232 braid->braidlength++;
234 } while (count < braid->nstrands - 1 && braid->braidlength < MAXLENGTH);
236 braid->startcolor = (MI_NPIXELS(mi) > 2) ?
237 (float) NRAND(MI_NPIXELS(mi)) : 0.0;
238 /* XSetLineAttributes (display, MI_GC(mi), 2, LineSolid, CapRound,
241 (void) memset((char *) braid->components, 0, sizeof (braid->components));
244 braid->components[0] = 1;
248 i = applyword(braid, i, 0);
249 braid->components[i] = braid->components[comp];
252 for (i = 0; i < braid->nstrands; i++)
253 if (braid->components[i] == 0)
256 for (comp = 0; braid->components[comp] != 0; comp++);
257 braid->components[comp] = ++c;
261 braid->linewidth = MI_SIZE(mi);
263 if (braid->linewidth < 0)
264 braid->linewidth = NRAND(-braid->linewidth) + 1;
265 if (braid->linewidth * braid->linewidth * 8 > MIN(MI_WIDTH(mi), MI_HEIGHT(mi)))
266 braid->linewidth = MIN(1, (int) sqrt((double) MIN(MI_WIDTH(mi), MI_HEIGHT(mi)) / 8));
267 for (i = 0; i < braid->nstrands; i++)
268 if (!(braid->components[i] & 1))
269 braid->components[i] *= -1;
273 draw_braid(ModeInfo * mi)
275 Display *display = MI_DISPLAY(mi);
276 Window window = MI_WINDOW(mi);
277 int num_points = 500;
282 float x_1, y_1, x_2, y_2, r1, r2;
283 float color, color_use = 0.0, color_inc;
288 braid = &braids[MI_SCREEN(mi)];
292 braid->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), braid->eraser);
297 MI_IS_DRAWN(mi) = True;
298 XSetLineAttributes(display, MI_GC(mi), braid->linewidth,
300 (braid->linewidth <= 3 ? CapButt : CapRound),
303 theta = (2.0 * M_PI) / (float) (braid->braidlength);
304 t_inc = (2.0 * M_PI) / (float) num_points;
305 color_inc = (float) MI_NPIXELS(mi) * braid->color_direction /
307 braid->startcolor += SPINRATE * color_inc;
308 if (((int) braid->startcolor) >= MI_NPIXELS(mi))
309 braid->startcolor = 0.0;
311 r_diff = (braid->max_radius - braid->min_radius) / (float) (braid->nstrands);
313 color = braid->startcolor;
315 for (i = 0; i < braid->braidlength; i++) {
317 for (t = 0.0; t < theta; t += t_inc) {
320 if (((int) color) >= MI_NPIXELS(mi))
324 for (s = 0; s < braid->nstrands; s++) {
325 if (ABS(braid->braidword[i]) == s)
327 if (ABS(braid->braidword[i]) - 1 == s) {
330 if (MI_NPIXELS(mi) > 2) {
331 color_use = color + SPINRATE *
332 braid->components[applywordbackto(braid, s, i)] +
333 (psi + t) / 2.0 / M_PI * (float) MI_NPIXELS(mi);
334 while (((int) color_use) >= MI_NPIXELS(mi))
335 color_use -= (float) MI_NPIXELS(mi);
336 while (((int) color_use) < 0)
337 color_use += (float) MI_NPIXELS(mi);
341 if (MI_NPIXELS(mi) > 2) {
342 color_use += SPINRATE * color_inc;
343 while (((int) color_use) >= MI_NPIXELS(mi))
344 color_use -= (float) MI_NPIXELS(mi);
347 r1 = braid->min_radius + r_diff * (float) (s);
348 r2 = braid->min_radius + r_diff * (float) (s + 1);
349 if (braid->braidword[i] > 0 ||
350 (FABSF(t - theta / 2.0) > theta / 7.0)) {
351 x_1 = ((0.5 * (1.0 + SINF(t / theta * M_PI - M_PI_2)) * r2 +
352 0.5 * (1.0 + SINF((theta - t) / theta * M_PI - M_PI_2)) * r1)) *
353 COSF(t + psi) + braid->center_x;
354 y_1 = ((0.5 * (1.0 + SINF(t / theta * M_PI - M_PI_2)) * r2 +
355 0.5 * (1.0 + SINF((theta - t) / theta * M_PI - M_PI_2)) * r1)) *
356 SINF(t + psi) + braid->center_y;
357 x_2 = ((0.5 * (1.0 + SINF((t + t_inc) / theta * M_PI - M_PI_2)) * r2 +
358 0.5 * (1.0 + SINF((theta - t - t_inc) / theta * M_PI - M_PI_2)) * r1)) *
359 COSF(t + t_inc + psi) + braid->center_x;
360 y_2 = ((0.5 * (1.0 + SINF((t + t_inc) / theta * M_PI - M_PI_2)) * r2 +
361 0.5 * (1.0 + SINF((theta - t - t_inc) / theta * M_PI - M_PI_2)) * r1)) *
362 SINF(t + t_inc + psi) + braid->center_y;
363 if (MI_NPIXELS(mi) > 2)
364 XSetForeground(display, MI_GC(mi), MI_PIXEL(mi, (int) color_use));
366 XSetForeground(display, MI_GC(mi), MI_WHITE_PIXEL(mi));
368 XDrawLine(display, window, MI_GC(mi),
369 (int) (x_1), (int) (y_1), (int) (x_2), (int) (y_2));
372 if (MI_NPIXELS(mi) > 2) {
373 color_use = color + SPINRATE *
374 braid->components[applywordbackto(braid, s + 1, i)] +
375 (psi + t) / 2.0 / M_PI * (float) MI_NPIXELS(mi);
376 while (((int) color_use) >= MI_NPIXELS(mi))
377 color_use -= (float) MI_NPIXELS(mi);
378 while (((int) color_use) < 0)
379 color_use += (float) MI_NPIXELS(mi);
382 if (braid->braidword[i] < 0 ||
383 (FABSF(t - theta / 2.0) > theta / 7.0)) {
384 x_1 = ((0.5 * (1.0 + SINF(t / theta * M_PI - M_PI_2)) * r1 +
385 0.5 * (1.0 + SINF((theta - t) / theta * M_PI - M_PI_2)) * r2)) *
386 COSF(t + psi) + braid->center_x;
387 y_1 = ((0.5 * (1.0 + SINF(t / theta * M_PI - M_PI_2)) * r1 +
388 0.5 * (1.0 + SINF((theta - t) / theta * M_PI - M_PI_2)) * r2)) *
389 SINF(t + psi) + braid->center_y;
390 x_2 = ((0.5 * (1.0 + SINF((t + t_inc) / theta * M_PI - M_PI_2)) * r1 +
391 0.5 * (1.0 + SINF((theta - t - t_inc) / theta * M_PI - M_PI_2)) * r2)) *
392 COSF(t + t_inc + psi) + braid->center_x;
393 y_2 = ((0.5 * (1.0 + SINF((t + t_inc) / theta * M_PI - M_PI_2)) * r1 +
394 0.5 * (1.0 + SINF((theta - t - t_inc) / theta * M_PI - M_PI_2)) * r2)) *
395 SINF(t + t_inc + psi) + braid->center_y;
396 if (MI_NPIXELS(mi) > 2)
397 XSetForeground(display, MI_GC(mi), MI_PIXEL(mi, (int) color_use));
399 XSetForeground(display, MI_GC(mi), MI_WHITE_PIXEL(mi));
401 XDrawLine(display, window, MI_GC(mi),
402 (int) (x_1), (int) (y_1), (int) (x_2), (int) (y_2));
407 if (MI_NPIXELS(mi) > 2) {
408 color_use = color + SPINRATE *
409 braid->components[applywordbackto(braid, s, i)] +
410 (psi + t) / 2.0 / M_PI * (float) MI_NPIXELS(mi);
411 while (((int) color_use) >= MI_NPIXELS(mi))
412 color_use -= (float) MI_NPIXELS(mi);
413 while (((int) color_use) < 0)
414 color_use += (float) MI_NPIXELS(mi);
418 if (MI_NPIXELS(mi) > 2) {
419 color_use += SPINRATE * color_inc;
420 while (((int) color_use) >= MI_NPIXELS(mi))
421 color_use -= (float) MI_NPIXELS(mi);
424 r1 = braid->min_radius + r_diff * (float) (s);
425 x_1 = r1 * COSF(t + psi) + braid->center_x;
426 y_1 = r1 * SINF(t + psi) + braid->center_y;
427 x_2 = r1 * COSF(t + t_inc + psi) + braid->center_x;
428 y_2 = r1 * SINF(t + t_inc + psi) + braid->center_y;
429 if (MI_NPIXELS(mi) > 2)
430 XSetForeground(display, MI_GC(mi), MI_PIXEL(mi, (int) color_use));
432 XSetForeground(display, MI_GC(mi), MI_WHITE_PIXEL(mi));
434 XDrawLine(display, window, MI_GC(mi),
435 (int) (x_1), (int) (y_1), (int) (x_2), (int) (y_2));
440 XSetLineAttributes(display, MI_GC(mi), 1, LineSolid, CapNotLast, JoinRound);
442 if (++braid->age > MI_CYCLES(mi)) {
444 braid->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), braid->eraser);
451 release_braid(ModeInfo * mi)
453 if (braids != NULL) {
454 (void) free((void *) braids);
455 braids = (braidtype *) NULL;
460 refresh_braid(ModeInfo * mi)
465 XSCREENSAVER_MODULE ("Braid", braid)
467 #endif /* MODE_braid */