1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997
2 * Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
13 /* Algorithm from a Mac program by Chris Tate, written in 1988 or so. */
15 /* 18-Sep-97: Johannes Keukelaar (johannes@nada.kth.se): Improved screen
17 * 10-May-97: merged ellipse code by Dan Stromberg <strombrg@nis.acs.uci.edu>
18 * as found in xlockmore 4.03a10.
23 #include "screenhack.h"
26 static double sins [360];
27 static double coss [360];
30 static unsigned int default_fg_pixel;
31 static int sleep_time;
34 init_helix (Display *dpy, Window window)
38 XWindowAttributes xgwa;
40 XGetWindowAttributes (dpy, window, &xgwa);
42 gcv.foreground = default_fg_pixel =
43 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
44 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
45 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
47 for (i = 0; i < 360; i++)
49 sins [i] = sin ((((double) i) / 180.0) * M_PI);
50 coss [i] = cos ((((double) i) / 180.0) * M_PI);
64 return (a < 0 ? -a : a);
68 helix (Display *dpy, Window window,
69 int radius1, int radius2, int d_angle,
70 int factor1, int factor2, int factor3, int factor4)
72 XWindowAttributes xgwa;
75 int x1, y1, x2, y2, angle, limit;
78 XClearWindow (dpy, window);
79 XGetWindowAttributes (dpy, window, &xgwa);
90 limit = 1 + (360 / gcd (360, d_angle));
92 for (i = 0; i < limit; i++)
95 #define pmod(x,y) (tmp=((x) % (y)), (tmp >= 0 ? tmp : (tmp + (y))))
97 x1 = xmid + (((double) radius1) * sins [pmod ((angle * factor1), 360)]);
98 y1 = ymid + (((double) radius2) * coss [pmod ((angle * factor2), 360)]);
99 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
100 x2 = xmid + (((double) radius2) * sins [pmod ((angle * factor3), 360)]);
101 y2 = ymid + (((double) radius1) * coss [pmod ((angle * factor4), 360)]);
103 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
110 trig (Display *dpy, Window window,
111 int d_angle, int factor1, int factor2,
112 int offset, int d_angle_offset, int dir, int density)
114 XWindowAttributes xgwa;
121 XClearWindow (dpy, window);
122 XGetWindowAttributes (dpy, window, &xgwa);
123 cmap = xgwa.colormap;
125 height = xgwa.height;
130 while (d_angle >= -360 && d_angle <= 360)
132 angle = d_angle + d_angle_offset;
133 x1 = (sins [pmod(angle * factor1, 360)] * xmid) + xmid;
134 y1 = (coss [pmod(angle * factor1, 360)] * ymid) + ymid;
135 x2 = (sins [pmod(angle * factor2 + offset, 360)] * xmid) + xmid;
136 y2 = (coss [pmod(angle * factor2 + offset, 360)] * ymid) + ymid;
137 XDrawLine(dpy, window, draw_gc, x1, y1, x2, y2);
138 tmp = (int) 360 / (2 * density * factor1 * factor2);
139 if (tmp == 0) /* Do not want it getting stuck... */
140 tmp = 1; /* Would not need if floating point */
141 d_angle += dir * tmp;
145 #define min(a,b) ((a)<(b)?(a):(b))
148 random_helix (Display *dpy, Window window, XColor *color, Bool *got_color)
152 int radius, radius1, radius2, d_angle, factor1, factor2, factor3, factor4;
154 XWindowAttributes xgwa;
155 XGetWindowAttributes (dpy, window, &xgwa);
157 height = xgwa.height;
158 cmap = xgwa.colormap;
160 radius = min (width, height) / 2;
168 divisor = ((frand (3.0) + 1) * (((random() & 1) * 2) - 1));
170 if ((random () & 1) == 0)
173 radius2 = radius / divisor;
178 radius1 = radius / divisor;
181 while (gcd (360, d_angle) >= 2)
182 d_angle = random () % 360;
184 #define random_factor() \
185 (((random() % 7) ? ((random() & 1) + 1) : 3) \
186 * (((random() & 1) * 2) - 1))
188 while (gcd (gcd (gcd (factor1, factor2), factor3), factor4) != 1)
190 factor1 = random_factor ();
191 factor2 = random_factor ();
192 factor3 = random_factor ();
193 factor4 = random_factor ();
197 XSetForeground (dpy, draw_gc, default_fg_pixel);
200 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
201 &color->red, &color->green, &color->blue);
202 if ((*got_color = XAllocColor (dpy, cmap, color)))
203 XSetForeground (dpy, draw_gc, color->pixel);
205 XSetForeground (dpy, draw_gc, default_fg_pixel);
207 helix (dpy, window, radius1, radius2, d_angle,
208 factor1, factor2, factor3, factor4);
212 random_trig (Display *dpy, Window window, XColor *color, Bool *got_color)
216 int radius, d_angle, factor1, factor2;
217 int offset, d_angle_offset, dir, density;
219 XWindowAttributes xgwa;
220 XGetWindowAttributes (dpy, window, &xgwa);
222 height = xgwa.height;
223 cmap = xgwa.colormap;
225 radius = min (width, height) / 2;
228 factor1 = (random() % 8) + 1;
230 factor2 = (random() % 8) + 1;
231 } while (factor1 == factor2);
233 dir = (random() & 1) ? 1 : -1;
234 d_angle_offset = random() % 360;
235 offset = ((random() % ((360 / 4) - 1)) + 1) / 4;
236 density = 1 << ((random() % 4) + 4); /* Higher density, higher angles */
239 XSetForeground (dpy, draw_gc, default_fg_pixel);
242 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
243 &color->red, &color->green, &color->blue);
244 if ((*got_color = XAllocColor (dpy, cmap, color)))
245 XSetForeground (dpy, draw_gc, color->pixel);
247 XSetForeground (dpy, draw_gc, default_fg_pixel);
249 trig (dpy, window, d_angle, factor1, factor2,
250 offset, d_angle_offset, dir, density);
254 random_helix_or_trig (Display *dpy, Window window)
256 Bool free_color = False;
259 XWindowAttributes xgwa;
261 XGetWindowAttributes (dpy, window, &xgwa);
263 height = xgwa.height;
264 cmap = xgwa.colormap;
267 random_helix(dpy, window, &color, &free_color);
269 random_trig(dpy, window, &color, &free_color);
272 screenhack_handle_events (dpy);
273 sleep ( sleep_time );
275 screenhack_handle_events (dpy);
276 erase_full_window(dpy, window);
278 if (free_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
280 screenhack_handle_events (dpy);
285 char *progclass = "Helix";
287 char *defaults [] = {
288 ".background: black",
293 XrmOptionDescRec options [] = {
294 { "-delay", ".delay", XrmoptionSepArg, 0 },
297 int options_size = (sizeof (options) / sizeof (options[0]));
300 screenhack (Display *dpy, Window window)
302 sleep_time = get_integer_resource("delay", "Integer");
303 init_helix (dpy, window);
305 random_helix_or_trig (dpy, window);