1 /* piecewise, 21jan2003
2 * Geoffrey Irving <irving@caltech.edu>
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
15 #include "screenhack.h"
17 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
19 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
21 #if !defined( __GNUC__ ) && !defined(__cplusplus) && !defined(c_plusplus)
26 #define X_PI (180 * 64)
32 #define ARC_BUFFER_SIZE 256
35 typedef struct _tree {
36 struct _tree *l, *r; /* left and right children */
37 /* extra stuff would go here */
43 typedef struct _circle {
45 double x, y; /* position */
46 double dx, dy; /* velocity */
48 int visible; /* default visibility */
49 struct _fringe *lo, *hi; /* lo and hi fringes */
51 int ni; /* number of intersections */
52 int *i; /* sorted intersection list */
55 typedef struct _fringe {
56 struct _fringe *l, *r; /* left and right children for splay trees */
58 circle *c; /* associated circle */
59 int side; /* 0 for lo, 1 for hi */
61 int mni; /* size of intersection array */
62 int ni; /* number of intersections */
63 int *i; /* sorted intersection list */
67 typedef struct _event {
68 struct _event *l, *r; /* left and right children for splay tree */
70 int kind; /* type of event */
71 double x, y; /* position */
72 fringe *lo, *hi; /* fringes */
82 double fringe_start_cut_x;
83 double fringe_start_cut_y;
85 double fringe_double_cut_x;
86 double fringe_double_cut_y;
87 fringe *fringe_double_cut_lo;
88 fringe *fringe_double_cut_hi;
91 XArc arc_buffer[ARC_BUFFER_SIZE];
97 XWindowAttributes xgwa;
98 Pixmap b, ba, bb; /* double-buffering pixmap */
100 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
101 XdbeBackBuffer backb;
102 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
104 int count, delay, ncolors, colorspeed, color_index, flags, iterations;
105 int color_iterations;
109 typedef int (*cut)(struct state *, tree*); /* cut x is <, =, or > 0 given a <, =, or > x for some a */
113 /******** splaying code */
115 /* Top-down splay routine. Reference:
116 * "Self-adjusting Binary Search Trees", Sleator and Tarjan,
117 * JACM Volume 32, No 3, July 1985, pp 652-686.
118 * See page 668 for specific splay transformations */
120 static tree *splay(struct state *st, cut c, tree *t)
136 /* top-down splaying loop */
140 break; /*** success ***/
144 break; /*** trivial ***/
148 *rl = x; /*** zig ***/
156 *rl = x; /*** zig ***/
162 x->l = y->r; /*** zig-zig ***/
172 *rl = x; /*** zig ***/
177 else { /*** zig-zag ***/
190 break; /*** trivial ***/
194 *lr = x; /*** zig ***/
202 *lr = x; /*** zig ***/
208 x->r = y->l; /*** zig-zig ***/
218 *lr = x; /*** zig ***/
223 else { /*** zig-zag ***/
243 static tree *splay_min(tree *t)
258 break; /*** trivial ***/
262 *rl = x; /*** zig ***/
268 x->l = y->r; /*** zig-zig ***/
283 static tree *splay_max(tree *t)
298 break; /*** trivial ***/
302 *lr = x; /*** zig ***/
308 x->r = y->l; /*** zig-zig ***/
323 /******** circles and fringe */
325 static inline double fringe_x(fringe *f, double y)
329 d = sqrt(f->c->r * f->c->r - dy * dy);
330 return f->side ? f->c->x + d : f->c->x - d;
333 static inline void fringe_add_intersection(fringe *f, double x, double y)
336 if (f->mni < f->ni) {
338 f->i = realloc(f->i, sizeof(int) * f->mni);
340 f->i[f->ni-1] = rint(atan2(y - f->c->y, x - f->c->x) * X_PI / M_PI);
343 static circle *init_circles(struct state *st, int n, int w, int h)
345 int i, r0, dr, speed;
347 double minradius, maxradius;
348 fringe *s = malloc(sizeof(fringe) * n * 2); /* never freed */
349 circle *c = malloc(sizeof(circle) * n);
351 speed = get_integer_resource(st->dpy, "speed", "Speed");
352 minradius = get_float_resource(st->dpy, "minradius", "Float");
353 maxradius = get_float_resource(st->dpy, "maxradius", "Float");
354 if (maxradius < minradius)
355 maxradius = minradius;
357 r0 = ceil(minradius * h);
358 dr = floor(maxradius * h) - r0 + 1;
361 c[i].r = r0 + ((dr > 0) ? random() % dr : 0);
362 c[i].x = c[i].r + frand(w - 1 - 2 * c[i].r);
363 c[i].y = c[i].r + frand(h - 1 - 2 * c[i].r);
364 c[i].visible = random() & 1;
370 v = (1 + frand(0.5)) * speed / 10.0;
371 c[i].dx = v * cos(a);
372 c[i].dy = v * sin(a);
376 c[i].lo->c = c[i].hi->c = c+i;
379 c[i].lo->mni = c[i].lo->ni = c[i].hi->mni = c[i].hi->ni = 0;
380 c[i].lo->i = c[i].hi->i = 0;
386 /* this is a hack, but I guess that's what I writing anyways */
387 static void tweak_circle(circle *c)
389 c->x += frand(2) - 1;
390 c->y += frand(1) + 0.1;
393 static void move_circle(circle *c, int w, int h)
400 else if (c->x >= w - c->r) {
409 else if (c->y >= h - c->r) {
415 /******** event queue */
417 static int event_cut(struct state *st, event *e)
419 return st->event_cut_y == e->y ? 0 : st->event_cut_y < e->y ? -1 : 1;
422 static void event_insert(struct state *st, event **eq, event *e)
427 return; /* avoid leak */
430 st->event_cut_y = e->y;
431 *eq = (event*)splay(st, (cut)event_cut, (tree*)*eq);
433 if (e->y == (*eq)->y) {
434 if (!((e->lo == (*eq)->lo && e->hi == (*eq)->hi) || (e->lo == (*eq)->hi && e->hi == (*eq)->lo))) {
436 e->r = 0; /* doing this instead of dying might be dangerous */
440 free(e); /* don't leak! */
442 else if (e->y < (*eq)->y) {
456 static void circle_start_event(struct state *st, event **eq, circle *c)
459 s = malloc(sizeof(event));
465 event_insert(st, eq, s);
468 static void circle_finish_event(struct state *st, event **eq, circle *c)
471 f = malloc(sizeof(event));
477 event_insert(st, eq, f);
480 static event *event_next(event **eq)
486 e = (event*)splay_min((tree*)*eq);
492 static void event_shred(event *e)
501 /******** fringe intersection */
503 static inline int check_fringe_intersection(double ye, fringe *lo, fringe *hi, double x, double y)
505 return ye <= y && ((x < lo->c->x) ^ lo->side) && ((x < hi->c->x) ^ hi->side);
508 static void fringe_intersect(struct state *st, event **eq, double y, fringe *lo, fringe *hi)
511 double dx, dy, sd, rs, rd, d, sx, sy, rp, sqd;
512 double x1, y1, x2, y2;
517 dx = hi->c->x - lo->c->x;
518 dy = hi->c->y - lo->c->y;
519 sd = dx * dx + dy * dy;
524 rs = hi->c->r + lo->c->r;
525 rd = hi->c->r - lo->c->r;
526 d = (rd * rd - sd) * (sd - rs * rs);
534 sx = (lo->c->x + hi->c->x) / 2;
535 sy = (lo->c->y + hi->c->y) / 2;
536 x1 = sx + sd * (dy * sqd - dx * rp);
537 y1 = sy - sd * (dx * sqd + dy * rp);
538 x2 = sx - sd * (dy * sqd + dx * rp);
539 y2 = sy + sd * (dx * sqd - dy * rp);
541 #define CHECK(xi, yi) (y <= yi && ((xi < lo->c->x) ^ lo->side) && ((xi < hi->c->x) ^ hi->side))
543 #define ADD_CROSS(xi, yi, ilo, ihi) { \
544 e = malloc(sizeof(event)); /* #### LEAK */ \
546 e->x = xi; e->y = yi; \
547 e->lo = ilo; e->hi = ihi; \
548 event_insert(st, eq, e); \
554 ADD_CROSS(x1, y1, lo, hi);
555 ADD_CROSS(x2, y2, hi, lo);
558 ADD_CROSS(x1, y1, hi, lo);
559 ADD_CROSS(x2, y2, lo, hi);
563 ADD_CROSS(x1, y1, lo, hi);
565 else if (CHECK(x2, y2))
566 ADD_CROSS(x2, y2, lo, hi);
571 /******** fringe trees and event handling */
573 #define PANIC ((fringe*)1) /* by alignment, no fringe should every be 1 */
575 static fringe *check_lo(struct state *st, event **eq, double y, fringe *f, fringe *hi)
578 f = (fringe*)splay_max((tree*)f);
579 fringe_intersect(st, eq, y, f, hi);
584 static fringe *check_hi(struct state *st, event **eq, double y, fringe *lo, fringe *f)
587 f = (fringe*)splay_min((tree*)f);
588 fringe_intersect(st, eq, y, lo, f);
593 static int fringe_start_cut(struct state *st, fringe *f)
595 double x = fringe_x(f, st->fringe_start_cut_y);
596 return st->fringe_start_cut_x == x ? 0 : st->fringe_start_cut_x < x ? -1 : 1;
599 static fringe *fringe_start(struct state *st, event **eq, fringe *f, double x, double y, fringe *lo, fringe *hi)
604 circle_finish_event(st, eq, lo->c);
611 st->fringe_start_cut_x = x;
612 st->fringe_start_cut_y = y;
613 f = (fringe*)splay(st, (cut)fringe_start_cut, (tree*)f);
616 if (x == sx) { /* time to cheat my way out of handling degeneracies */
618 circle_start_event(st, eq, lo->c);
622 circle_finish_event(st, eq, lo->c);
623 f->l = check_lo(st, eq, y, f->l, lo);
624 fringe_intersect(st, eq, y, hi, f);
632 circle_finish_event(st, eq, lo->c);
633 fringe_intersect(st, eq, y, f, lo);
634 f->r = check_hi(st, eq, y, hi, f->r);
643 static int fringe_double_cut(struct state *st, fringe *f)
646 if (f == st->fringe_double_cut_lo || f == st->fringe_double_cut_hi)
648 x = fringe_x(f, st->fringe_double_cut_y);
649 return st->fringe_double_cut_x == x ? 0 : st->fringe_double_cut_x < x ? -1 : 1;
652 static int fringe_double_splay(struct state *st, fringe *f, double x, double y, fringe *lo, fringe *hi)
654 st->fringe_double_cut_x = x;
655 st->fringe_double_cut_y = y;
656 st->fringe_double_cut_lo = lo;
657 st->fringe_double_cut_hi = hi;
658 f = (fringe*)splay(st, (cut)fringe_double_cut, (tree*)f);
661 return (f->r = (fringe*)splay_min((tree*)f->r)) == hi;
663 return (f->l = (fringe*)splay_max((tree*)f->l)) == lo;
668 static fringe *fringe_cross(struct state *st, event **eq, fringe *f, double x, double y, fringe *lo, fringe *hi)
671 if (!fringe_double_splay(st, f, x, y, lo, hi))
673 l = check_lo(st, eq, y, lo->l, hi);
674 r = check_hi(st, eq, y, lo, hi->r);
682 static fringe *fringe_finish(struct state *st, event **eq, fringe *f, double x, double y, fringe *lo, fringe *hi)
684 if (!fringe_double_splay(st, f, x, y, lo, hi))
691 lo->l = (fringe*)splay_max((tree*)lo->l);
692 hi->r = (fringe*)splay_min((tree*)hi->r);
693 fringe_intersect(st, eq, y, lo->l, hi->r);
700 /******** plane sweep */
702 static void sweep(struct state *st, int n, circle *c)
709 #define CHECK_PANIC() \
713 for (i=0;i<n;i++) { \
715 c[i].lo->ni = c[i].hi->ni = 0; \
722 circle_start_event(st, &eq, c+i);
725 while ((e = event_next(&eq))) {
728 f = fringe_start(st, &eq, f, e->x, e->y, e->lo, e->hi);
731 f = fringe_cross(st, &eq, f, e->x, e->y, e->lo, e->hi);
733 fringe_add_intersection(e->lo, e->x, e->y);
734 fringe_add_intersection(e->hi, e->x, e->y);
737 f = fringe_finish(st, &eq, f, e->x, e->y, e->lo, e->hi);
745 /******** circle drawing */
747 static void adjust_circle_visibility(circle *c)
751 n = c->lo->ni + c->hi->ni;
752 in = malloc(sizeof(int) * n);
753 for (i=0;i<c->hi->ni;i++)
755 for (i=c->lo->ni-1;i>=0;i--)
756 in[n-i-1] = c->lo->i[i] > 0 ? c->lo->i[i] : c->lo->i[i] + 2 * X_PI;
757 c->lo->ni = c->hi->ni = 0;
761 while (i < n && j < c->ni) /* whee */
762 a = (in[i] < c->i[j] ? in[i++] : c->i[j++]) - a;
769 c->visible = !c->visible;
775 static void flush_arc_buffer(struct state *st, Drawable w, GC gc)
777 if (st->arc_buffer_count) {
778 XDrawArcs(st->dpy, w, gc, st->arc_buffer, st->arc_buffer_count);
779 st->arc_buffer_count = 0;
783 static void draw_circle(struct state *st, Drawable w, GC gc, circle *c)
786 adjust_circle_visibility(c);
788 xi = rint(c->x - c->r);
789 yi = rint(c->y - c->r);
792 #define ARC(p, a1, a2) { \
793 if (((p) & 1) ^ c->visible) { \
794 st->arc_buffer[st->arc_buffer_count].x = xi; \
795 st->arc_buffer[st->arc_buffer_count].y = yi; \
796 st->arc_buffer[st->arc_buffer_count].width = di; \
797 st->arc_buffer[st->arc_buffer_count].height = di; \
798 st->arc_buffer[st->arc_buffer_count].angle1 = -(a1); \
799 st->arc_buffer[st->arc_buffer_count].angle2 = (a1) - (a2); \
800 st->arc_buffer_count++; \
801 if (st->arc_buffer_count == ARC_BUFFER_SIZE) \
802 flush_arc_buffer(st, w, gc); \
809 ARC(0, c->i[c->ni-1], c->i[0] + 2 * X_PI)
810 for (i=1;i<c->ni;i++)
811 ARC(i, c->i[i-1], c->i[i])
814 /******** toplevel */
817 check_for_leaks (void)
820 static unsigned long early_brk = 0;
821 unsigned long max = 30 * 1024 * 1024; /* 30 MB */
822 int b = (unsigned long) sbrk(0);
825 else if (b > early_brk + max)
827 fprintf (stderr, "%s: leaked %lu MB -- aborting!\n",
828 progname, ((b - early_brk) >> 20));
831 #endif /* HAVE_SBRK */
835 piecewise_init (Display *dd, Window ww)
837 struct state *st = (struct state *) calloc (1, sizeof(*st));
841 st->count = get_integer_resource(st->dpy, "count", "Integer");
842 st->delay = get_integer_resource(st->dpy, "delay", "Integer");
843 st->ncolors = get_integer_resource(st->dpy, "ncolors", "Integer");
844 st->colorspeed = get_integer_resource(st->dpy, "colorspeed", "Integer");
845 st->dbuf = get_boolean_resource(st->dpy, "doubleBuffer", "Boolean");
847 # ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
851 st->color_iterations = st->colorspeed ? 100 / st->colorspeed : 100000;
852 if (!st->color_iterations)
853 st->color_iterations = 1;
855 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
856 st->colors = calloc(sizeof(XColor), st->ncolors);
858 if (get_boolean_resource(st->dpy, "mono", "Boolean")) {
861 st->colors[0].pixel = get_pixel_resource(st->dpy, st->xgwa.colormap, "foreground", "Foreground");
864 make_color_loop(st->dpy, st->xgwa.colormap, 0, 1, 1, 120, 1, 1, 240, 1, 1, st->colors, &st->ncolors, True, False);
870 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
871 st->b = st->backb = xdbe_get_backbuffer(st->dpy, st->window, XdbeUndefined);
872 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
875 st->ba = XCreatePixmap(st->dpy, st->window, st->xgwa.width, st->xgwa.height,st->xgwa.depth);
876 st->bb = XCreatePixmap(st->dpy, st->window, st->xgwa.width, st->xgwa.height,st->xgwa.depth);
884 st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap, "background", "Background");
885 st->erase_gc = XCreateGC (st->dpy, st->b, GCForeground, &st->gcv);
888 st->flags = GCForeground;
889 st->color_index = random() % st->ncolors;
890 st->gcv.foreground = st->colors[st->color_index].pixel;
891 st->draw_gc = XCreateGC(st->dpy, st->b, st->flags, &st->gcv);
893 /* initialize circles */
894 st->circles = init_circles(st, st->count, st->xgwa.width, st->xgwa.height);
902 piecewise_draw (Display *dpy, Window window, void *closure)
904 struct state *st = (struct state *) closure;
907 XFillRectangle (st->dpy, st->b, st->erase_gc, 0, 0, st->xgwa.width, st->xgwa.height);
909 sweep(st, st->count, st->circles);
910 for (i=0;i<st->count;i++) {
911 draw_circle(st, st->b, st->draw_gc, st->circles+i);
912 move_circle(st->circles+i, st->xgwa.width, st->xgwa.height);
914 flush_arc_buffer(st, st->b, st->draw_gc);
916 if (++st->iterations % st->color_iterations == 0) {
917 st->color_index = (st->color_index + 1) % st->ncolors;
918 XSetForeground(st->dpy, st->draw_gc, st->colors[st->color_index].pixel);
921 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
923 XdbeSwapInfo info[1];
924 info[0].swap_window = st->window;
925 info[0].swap_action = XdbeUndefined;
926 XdbeSwapBuffers (st->dpy, info, 1);
929 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
931 XCopyArea (st->dpy, st->b, st->window, st->erase_gc, 0, 0, st->xgwa.width, st->xgwa.height, 0, 0);
932 st->b = (st->b == st->ba ? st->bb : st->ba);
940 piecewise_reshape (Display *dpy, Window window, void *closure,
941 unsigned int w, unsigned int h)
946 piecewise_event (Display *dpy, Window window, void *closure, XEvent *event)
952 piecewise_free (Display *dpy, Window window, void *closure)
954 struct state *st = (struct state *) closure;
960 static const char *piecewise_defaults [] = {
961 ".background: black",
962 ".foreground: white",
972 "*doubleBuffer: True",
973 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
975 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
979 static XrmOptionDescRec piecewise_options [] = {
980 { "-delay", ".delay", XrmoptionSepArg, 0 },
981 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
982 { "-speed", ".speed", XrmoptionSepArg, 0 },
983 { "-colorspeed", ".colorspeed", XrmoptionSepArg, 0 },
985 { "-count", ".count", XrmoptionSepArg, 0 },
986 { "-minradius", ".minradius", XrmoptionSepArg, 0 },
987 { "-maxradius", ".maxradius", XrmoptionSepArg, 0 },
989 { "-db", ".doubleBuffer", XrmoptionNoArg, "True" },
990 { "-no-db", ".doubleBuffer", XrmoptionNoArg, "False" },
995 XSCREENSAVER_MODULE ("Piecewise", piecewise)