2 /**************************************************************************
5 * MODULE OF xscreensaver
7 * DESCRIPTION Bilinear interpolation for morphing line shapes.
9 * WRITTEN BY Sverre H. Huseby Glenn T. Lines
10 * Maridalsvn. 122, leil 101 Frysjavn. 3, 5. etg.
11 * N-0461 Oslo N-0883 Oslo
14 * Phone: +47 22 71 99 08 Phone: +47 22 23 71 99
15 * E-mail: sverrehu@ifi.uio.no E-mail: gtl@si.sintef.no
17 * The original idea, and the bilinear interpolation
18 * mathematics used, emerged in the head of the wise
21 * MODIFICATIONS march 1995
22 * * Converted from an MS-Windows program to X Window.
24 **************************************************************************/
30 #include "screenhack.h"
32 /**************************************************************************
34 * P R I V A T E D A T A *
36 **************************************************************************/
38 /* Define MARGINS to make some space around the figure */
42 #define TWO_PI (2.0 * M_PI)
43 #define RND(x) (random() % (x))
45 cFig = 0, /* Number of figure arrays. */
46 cPoint, /* Number of points in each array. */
47 nWork, /* Current work array number. */
48 nFrom, /* Current from array number. */
49 nTo; /* Current to array number. */
51 delay; /* usecs to wait between updates. */
53 *aWork[2], /* Working arrays. */
54 *a[MAXFIGS], /* The figure arrays. */
55 *aTmp, /* Used as source when interrupting morph */
56 *aPrev, /* Previous points displayed. */
57 *aCurr, /* The current points displayed. */
58 *aFrom, /* Figure converting from. */
59 *aTo; /* Figure converting to. */
73 /**************************************************************************
75 * P U B L I C D A T A *
77 **************************************************************************/
79 char *progclass = "LMorph";
82 "LMorph.background: black",
83 "LMorph.foreground: green",
90 XrmOptionDescRec options [] = {
91 { "-points", ".points", XrmoptionSepArg, 0 },
92 { "-steps", ".steps", XrmoptionSepArg, 0 },
93 { "-delay", ".delay", XrmoptionSepArg, 0 },
95 int options_size = (sizeof (options) / sizeof (options[0]));
99 /**************************************************************************
101 * P R I V A T E F U N C T I O N S *
103 **************************************************************************/
105 static void *xmalloc(size)
110 if ((ret = malloc(size)) == NULL) {
111 fprintf(stderr, "lmorph: out of memory\n");
122 * Hm. for some reason the second line (using RAND_MAX) didn't
123 * work on some machines, so I always use the first.
127 return (double) (random() & 0x7FFF) / 0x7FFF;
129 return ((double) random()) / RAND_MAX;
135 static void initPointArrays()
137 XWindowAttributes wa;
139 mx, my, /* Max screen coordinates. */
140 mp, /* Max point number. */
143 double scalex, scaley;
145 XGetWindowAttributes(dpy, window, &wa);
150 aWork[0] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
151 aWork[1] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
152 aTmp = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
158 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
160 for (q = 0; q < s; q++) {
161 a[cFig][q].x = ((double) q / s) * mx;
163 a[cFig][s + q].x = mx;
164 a[cFig][s + q].y = ((double) q / s) * my;
165 a[cFig][2 * s + q].x = mx - ((double) q / s) * mx;
166 a[cFig][2 * s + q].y = my;
167 a[cFig][3 * s + q].x = 0;
168 a[cFig][3 * s + q].y = my - ((double) q / s) * my;
170 for (q = 4 * s; q < cPoint; q++)
171 a[cFig][q].x = a[cFig][q].y = 0;
172 a[cFig][mp].x = a[cFig][0].x;
173 a[cFig][mp].y = a[cFig][0].y;
179 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
180 for (q = 0; q < cPoint; q++) {
181 a[cFig][q].x = ((double) q / cPoint) * mx;
182 a[cFig][q].y = (1.0 - sin(((double) q / mp) * TWO_PI)) * my / 2.0;
189 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
192 for (q = 0; q < cPoint; q++) {
193 a[cFig][q].x = mx / 2 + rx * sin(1 * TWO_PI * (double) q / mp);
194 a[cFig][q].y = my / 2 + ry * cos(3 * TWO_PI * (double) q / mp);
196 a[cFig][mp].x = a[cFig][0].x;
197 a[cFig][mp].y = a[cFig][0].y;
203 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
206 for (q = 0; q < cPoint; q++) {
207 a[cFig][q].x = mx / 2 + ry * sin(3 * TWO_PI * (double) q / mp);
208 a[cFig][q].y = my / 2 + ry * cos(1 * TWO_PI * (double) q / mp);
210 a[cFig][mp].x = a[cFig][0].x;
211 a[cFig][mp].y = a[cFig][0].y;
217 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
220 for (q = 0; q < cPoint; q++) {
221 a[cFig][q].x = mx / 2 + ry * (1 - 0.1 * frnd())
222 * sin(TWO_PI * (double) q / mp);
223 a[cFig][q].y = my / 2 + ry * (1 - 0.1 * frnd())
224 * cos(TWO_PI * (double) q / mp);
226 a[cFig][mp].x = a[cFig][0].x;
227 a[cFig][mp].y = a[cFig][0].y;
233 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
236 for (q = 0; q < cPoint; q++) {
237 a[cFig][q].x = mx / 2 + ry * (0.8 - 0.2 * sin(30 * TWO_PI * q / mp))
238 * sin(TWO_PI * (double) q / mp);
239 a[cFig][q].y = my / 2 + ry * (0.8 - 0.2 * sin(30 * TWO_PI * q / mp))
240 * cos(TWO_PI * (double) q / mp);
242 a[cFig][mp].x = a[cFig][0].x;
243 a[cFig][mp].y = a[cFig][0].y;
249 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
252 for (q = 0; q < cPoint; q++) {
253 a[cFig][q].x = mx / 2 + ry * sin(TWO_PI * (double) q / mp);
254 a[cFig][q].y = my / 2 + ry * cos(TWO_PI * (double) q / mp);
256 a[cFig][mp].x = a[cFig][0].x;
257 a[cFig][mp].y = a[cFig][0].y;
263 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
266 for (q = 0; q < cPoint; q++) {
267 a[cFig][q].x = mx / 2 + rx * cos(TWO_PI * (double) q / mp);
268 a[cFig][q].y = my / 2 + ry * sin(TWO_PI * (double) q / mp);
270 a[cFig][mp].x = a[cFig][0].x;
271 a[cFig][mp].y = a[cFig][0].y;
277 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
278 for (q = 0; q < cPoint; q++) {
279 a[cFig][q].x = ((double) q / mp) * mx;
280 a[cFig][q].y = (1.0 - cos(((double) q / mp) * 3 * TWO_PI)) * my / 2.0;
287 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
290 for (q = 0; q < cPoint; q++) {
291 a[cFig][q].x = mx / 2 + rx * sin(2 * TWO_PI * (double) q / mp);
292 a[cFig][q].y = my / 2 + ry * cos(3 * TWO_PI * (double) q / mp);
294 a[cFig][mp].x = a[cFig][0].x;
295 a[cFig][mp].y = a[cFig][0].y;
301 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
304 for (q = 0; q < cPoint; q++) {
305 a[cFig][q].x = mx / 2 + ry * sin(5 * TWO_PI * (double) q / mp)
307 a[cFig][q].y = my / 2 + ry * cos(5 * TWO_PI * (double) q / mp)
315 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
318 for (q = 0; q < cPoint; q++) {
319 a[cFig][q].x = mx / 2 + ry * sin(6 * TWO_PI * (double) q / mp)
321 a[cFig][q].y = my / 2 - ry * cos(6 * TWO_PI * (double) q / mp)
329 a[cFig] = (XPoint *) xmalloc(cPoint * sizeof(XPoint));
330 for (q = 0; q < cPoint; q++) {
331 a[cFig][q].x = ((double) q / mp) * mx;
332 a[cFig][q].y = (1.0 - sin(((double) q / mp) * 5 * TWO_PI)) * my / 2.0;
338 * Make some space around the figures.
340 marginx = (mx + 1) / 10;
341 marginy = (my + 1) / 10;
342 scalex = (double) ((mx + 1) - 2.0 * marginx) / (mx + 1.0);
343 scaley = (double) ((my + 1) - 2.0 * marginy) / (my + 1.0);
344 for (q = 0; q < cFig; q++)
345 for (w = 0; w < cPoint; w++) {
346 a[q][w].x = marginx + a[q][w].x * scalex;
347 a[q][w].y = marginy + a[q][w].y * scaley;
354 static void createPoints()
357 XPoint *pa = aCurr, *pa1 = aFrom, *pa2 = aTo;
361 lg = 8192L * gam, l1g = 8192L * (1.0 - gam);
362 for (q = 0; q < cPoint; q++) {
363 pa->x = (short) ((l1g * pa1->x + lg * pa2->x) / 8192L);
364 pa->y = (short) ((l1g * pa1->y + lg * pa2->y) / 8192L);
372 static void drawImage()
375 XPoint *old0, *old1, *new0, *new1;
378 * Problem: update the window without too much flickering. I do
379 * this by handling each linesegment separately. First remove a
380 * line, then draw the new line. The problem is that this leaves
381 * small black pixels on the figure. To fix this, I draw the
382 * entire figure using XDrawLines() afterwards.
389 for (q = cPoint - 1; q; q--) {
390 XDrawLine(dpy, window, gcClear,
391 old0->x, old0->y, old1->x, old1->y);
392 XDrawLine(dpy, window, gcDraw,
393 new0->x, new0->y, new1->x, new1->y);
400 XDrawLines(dpy, window, gcDraw, aCurr, cPoint, CoordModeOrigin);
404 static void initLMorph()
408 XWindowAttributes wa;
411 cPoint = get_integer_resource("points", "Integer");
412 steps = get_integer_resource("steps", "Integer");
413 delay = get_integer_resource("delay", "Integer");
416 steps = (random() % 400) + 100;
418 delta_gam = 1.0 / steps;
419 XGetWindowAttributes(dpy, window, &wa);
421 gcv.foreground = get_pixel_resource("foreground", "Foreground", dpy, cmap);
422 gcDraw = XCreateGC(dpy, window, GCForeground, &gcv);
423 XSetForeground(dpy, gcDraw, gcv.foreground);
424 gcv.foreground = get_pixel_resource("background", "Background", dpy, cmap);
425 gcClear = XCreateGC(dpy, window, GCForeground, &gcv);
426 XClearWindow(dpy, window);
430 aCurr = aWork[nWork = 0];
436 static void animateLMorph()
438 if (gam > maxGamma) {
440 if (maxGamma == 1.0) {
444 memcpy(aTmp, aCurr, cPoint * sizeof(XPoint));
450 } while (nTo == nFrom);
454 * Reverse the array to get more variation.
459 for (i1 = 0, i2 = cPoint - 1; i1 < cPoint / 2; i1++, i2--) {
466 * It may be nice to interrupt the next run.
469 maxGamma = 0.1 + 0.7 * (RND(1001) / 1000.0);
477 aCurr = aWork[nWork ^= 1];
484 /**************************************************************************
486 * P U B L I C F U N C T I O N S *
488 **************************************************************************/
490 void screenhack(disp, win)
499 screenhack_usleep(delay);