1 /* xscreensaver, Copyright (c) 1992, 1995 Jamie Zawinski <jwz@netscape.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
13 #include "screenhack.h"
15 static double sins [360];
16 static double coss [360];
18 static GC draw_gc, erase_gc;
19 static unsigned int default_fg_pixel;
22 init_helix (dpy, window)
28 XWindowAttributes xgwa;
30 XGetWindowAttributes (dpy, window, &xgwa);
32 gcv.foreground = default_fg_pixel =
33 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
34 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
35 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
36 erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
38 for (i = 0; i < 360; i++)
40 sins [i] = sin ((((double) i) / 180.0) * M_PI);
41 coss [i] = cos ((((double) i) / 180.0) * M_PI);
56 return (a < 0 ? -a : a);
61 radius1, radius2, d_angle,
62 factor1, factor2, factor3, factor4)
65 int radius1, radius2, d_angle;
66 int factor1, factor2, factor3, factor4;
68 XWindowAttributes xgwa;
71 int x1, y1, x2, y2, angle, limit;
74 XClearWindow (dpy, window);
75 XGetWindowAttributes (dpy, window, &xgwa);
86 limit = 1 + (360 / gcd (360, d_angle));
88 for (i = 0; i < limit; i++)
91 #define pmod(x,y) (tmp = (x % y), (tmp >= 0 ? tmp : tmp + y))
92 x1 = xmid + (((double) radius1) * sins [pmod ((angle * factor1), 360)]);
93 y1 = ymid + (((double) radius2) * coss [pmod ((angle * factor2), 360)]);
94 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
95 x2 = xmid + (((double) radius2) * sins [pmod ((angle * factor3), 360)]);
96 y2 = ymid + (((double) radius1) * coss [pmod ((angle * factor4), 360)]);
97 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
103 #define min(a,b) ((a)<(b)?(a):(b))
106 random_helix (dpy, window)
112 int radius, radius1, radius2, d_angle, factor1, factor2, factor3, factor4;
115 int i, got_color = 0;
116 XWindowAttributes xgwa;
117 XGetWindowAttributes (dpy, window, &xgwa);
119 height = xgwa.height;
120 cmap = xgwa.colormap;
122 radius = min (width, height) / 2;
130 divisor = ((frand (3.0) + 1) * (((random() & 1) * 2) - 1));
132 if ((random () & 1) == 0)
135 radius2 = radius / divisor;
140 radius1 = radius / divisor;
143 while (gcd (360, d_angle) >= 2)
144 d_angle = random () % 360;
146 #define random_factor() \
147 (((random() % 7) ? ((random() & 1) + 1) : 3) \
148 * (((random() & 1) * 2) - 1))
150 while (gcd (gcd (gcd (factor1, factor2), factor3), factor4) != 1)
152 factor1 = random_factor ();
153 factor2 = random_factor ();
154 factor3 = random_factor ();
155 factor4 = random_factor ();
159 XSetForeground (dpy, draw_gc, default_fg_pixel);
162 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
163 &color.red, &color.green, &color.blue);
164 if ((got_color = XAllocColor (dpy, cmap, &color)))
165 XSetForeground (dpy, draw_gc, color.pixel);
167 XSetForeground (dpy, draw_gc, default_fg_pixel);
169 helix (dpy, window, radius1, radius2, d_angle,
170 factor1, factor2, factor3, factor4);
175 for (i = 0; i < height; i++)
177 int y = (random () % height);
178 XDrawLine (dpy, window, erase_gc, 0, y, width, y);
183 XClearWindow (dpy, window);
184 if (got_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
190 char *progclass = "Helix";
192 char *defaults [] = {
193 "Helix.background: black", /* to placate SGI */
197 XrmOptionDescRec options [] = { { 0, } };
198 int options_size = 0;
201 screenhack (dpy, window)
205 init_helix (dpy, window);
207 random_helix (dpy, window);