1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@lucid.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
12 #include "screenhack.h"
18 static double sins [360];
19 static double coss [360];
21 static GC draw_gc, erase_gc;
22 static unsigned int default_fg_pixel;
25 init_helix (dpy, window)
31 XWindowAttributes xgwa;
33 XGetWindowAttributes (dpy, window, &xgwa);
35 gcv.foreground = default_fg_pixel =
36 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
37 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
38 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
39 erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
41 for (i = 0; i < 360; i++)
43 sins [i] = sin ((((double) i) / 180.0) * M_PI);
44 coss [i] = cos ((((double) i) / 180.0) * M_PI);
59 return (a < 0 ? -a : a);
64 radius1, radius2, d_angle,
65 factor1, factor2, factor3, factor4)
68 int radius1, radius2, d_angle;
69 int factor1, factor2, factor3, factor4;
71 XWindowAttributes xgwa;
74 int x1, y1, x2, y2, angle, limit;
77 XClearWindow (dpy, window);
78 XGetWindowAttributes (dpy, window, &xgwa);
89 limit = 1 + (360 / gcd (360, d_angle));
91 for (i = 0; i < limit; i++)
94 #define pmod(x,y) (tmp = (x % y), (tmp >= 0 ? tmp : tmp + y))
95 x1 = xmid + (((double) radius1) * sins [pmod ((angle * factor1), 360)]);
96 y1 = ymid + (((double) radius2) * coss [pmod ((angle * factor2), 360)]);
97 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
98 x2 = xmid + (((double) radius2) * sins [pmod ((angle * factor3), 360)]);
99 y2 = ymid + (((double) radius1) * coss [pmod ((angle * factor4), 360)]);
100 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
106 #define min(a,b) ((a)<(b)?(a):(b))
109 random_helix (dpy, window)
115 int radius, radius1, radius2, d_angle, factor1, factor2, factor3, factor4;
118 int i, got_color = 0;
119 XWindowAttributes xgwa;
120 XGetWindowAttributes (dpy, window, &xgwa);
122 height = xgwa.height;
123 cmap = xgwa.colormap;
125 radius = min (width, height) / 2;
133 divisor = ((frand (3.0) + 1) * (((random() & 1) * 2) - 1));
135 if ((random () & 1) == 0)
138 radius2 = radius / divisor;
143 radius1 = radius / divisor;
146 while (gcd (360, d_angle) >= 2)
147 d_angle = random () % 360;
149 #define random_factor() \
150 (((random() % 7) ? ((random() & 1) + 1) : 3) \
151 * (((random() & 1) * 2) - 1))
153 while (gcd (gcd (gcd (factor1, factor2), factor3), factor4) != 1)
155 factor1 = random_factor ();
156 factor2 = random_factor ();
157 factor3 = random_factor ();
158 factor4 = random_factor ();
162 XSetForeground (dpy, draw_gc, default_fg_pixel);
165 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
166 &color.red, &color.green, &color.blue);
167 if ((got_color = XAllocColor (dpy, cmap, &color)))
168 XSetForeground (dpy, draw_gc, color.pixel);
170 XSetForeground (dpy, draw_gc, default_fg_pixel);
172 helix (dpy, window, radius1, radius2, d_angle,
173 factor1, factor2, factor3, factor4);
178 for (i = 0; i < height; i++)
180 int y = (random () % height);
181 XDrawLine (dpy, window, erase_gc, 0, y, width, y);
186 XClearWindow (dpy, window);
187 if (got_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
193 char *progclass = "Helix";
195 char *defaults [] = {
196 "*background: black",
200 XrmOptionDescRec options [] = { 0 };
201 int options_size = 0;
204 screenhack (dpy, window)
208 init_helix (dpy, window);
210 random_helix (dpy, window);