1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.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"
14 #include <math.h> /* for M_PI */
17 static double sins [360];
18 static double coss [360];
20 static GC draw_gc, erase_gc;
21 static unsigned int default_fg_pixel;
24 init_helix (dpy, window)
30 XWindowAttributes xgwa;
32 XGetWindowAttributes (dpy, window, &xgwa);
34 gcv.foreground = default_fg_pixel =
35 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
36 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
37 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
38 erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
40 for (i = 0; i < 360; i++)
42 sins [i] = sin ((((double) i) / 180.0) * M_PI);
43 coss [i] = cos ((((double) i) / 180.0) * M_PI);
58 return (a < 0 ? -a : a);
63 radius1, radius2, d_angle,
64 factor1, factor2, factor3, factor4)
67 int radius1, radius2, d_angle;
68 int factor1, factor2, factor3, factor4;
70 XWindowAttributes xgwa;
73 int x1, y1, x2, y2, angle, limit;
76 XClearWindow (dpy, window);
77 XGetWindowAttributes (dpy, window, &xgwa);
88 limit = 1 + (360 / gcd (360, d_angle));
90 for (i = 0; i < limit; i++)
93 #define pmod(x,y) (tmp = (x % y), (tmp >= 0 ? tmp : tmp + y))
94 x1 = xmid + (((double) radius1) * sins [pmod ((angle * factor1), 360)]);
95 y1 = ymid + (((double) radius2) * coss [pmod ((angle * factor2), 360)]);
96 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
97 x2 = xmid + (((double) radius2) * sins [pmod ((angle * factor3), 360)]);
98 y2 = ymid + (((double) radius1) * coss [pmod ((angle * factor4), 360)]);
99 XDrawLine (dpy, window, draw_gc, x1, y1, x2, y2);
105 #define min(a,b) ((a)<(b)?(a):(b))
108 random_helix (dpy, window)
114 int radius, radius1, radius2, d_angle, factor1, factor2, factor3, factor4;
117 int i, got_color = 0;
118 XWindowAttributes xgwa;
119 XGetWindowAttributes (dpy, window, &xgwa);
121 height = xgwa.height;
122 cmap = xgwa.colormap;
124 radius = min (width, height) / 2;
132 divisor = ((frand (3.0) + 1) * (((random() & 1) * 2) - 1));
134 if ((random () & 1) == 0)
137 radius2 = radius / divisor;
142 radius1 = radius / divisor;
145 while (gcd (360, d_angle) >= 2)
146 d_angle = random () % 360;
148 #define random_factor() \
149 (((random() % 7) ? ((random() & 1) + 1) : 3) \
150 * (((random() & 1) * 2) - 1))
152 while (gcd (gcd (gcd (factor1, factor2), factor3), factor4) != 1)
154 factor1 = random_factor ();
155 factor2 = random_factor ();
156 factor3 = random_factor ();
157 factor4 = random_factor ();
161 XSetForeground (dpy, draw_gc, default_fg_pixel);
164 hsv_to_rgb (random () % 360, frand (1.0), frand (0.5) + 0.5,
165 &color.red, &color.green, &color.blue);
166 if ((got_color = XAllocColor (dpy, cmap, &color)))
167 XSetForeground (dpy, draw_gc, color.pixel);
169 XSetForeground (dpy, draw_gc, default_fg_pixel);
171 helix (dpy, window, radius1, radius2, d_angle,
172 factor1, factor2, factor3, factor4);
177 for (i = 0; i < height; i++)
179 int y = (random () % height);
180 XDrawLine (dpy, window, erase_gc, 0, y, width, y);
185 XClearWindow (dpy, window);
186 if (got_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
192 char *progclass = "Helix";
194 char *defaults [] = {
195 "Helix.background: black", /* to placate SGI */
199 XrmOptionDescRec options [] = { 0 };
200 int options_size = 0;
203 screenhack (dpy, window)
207 init_helix (dpy, window);
209 random_helix (dpy, window);