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 #define X_PI (180 * 64)
23 /******** splaying code */
25 typedef struct _tree {
26 struct _tree *l, *r; /* left and right children */
27 /* extra stuff would go here */
30 typedef int (*cut)(tree*); /* cut x is <, =, or > 0 given a <, =, or > x for some a */
32 /* Top-down splay routine. Reference:
33 * "Self-adjusting Binary Search Trees", Sleator and Tarjan,
34 * JACM Volume 32, No 3, July 1985, pp 652-686.
35 * See page 668 for specific splay transformations */
37 tree *splay(cut c, tree *t) {
52 /* top-down splaying loop */
56 break; /*** success ***/
60 break; /*** trivial ***/
64 *rl = x; /*** zig ***/
72 *rl = x; /*** zig ***/
78 x->l = y->r; /*** zig-zig ***/
88 *rl = x; /*** zig ***/
93 else { /*** zig-zag ***/
106 break; /*** trivial ***/
110 *lr = x; /*** zig ***/
118 *lr = x; /*** zig ***/
124 x->r = y->l; /*** zig-zig ***/
134 *lr = x; /*** zig ***/
139 else { /*** zig-zag ***/
159 tree *splay_min(tree *t) {
173 break; /*** trivial ***/
177 *rl = x; /*** zig ***/
183 x->l = y->r; /*** zig-zig ***/
198 tree *splay_max(tree *t) {
212 break; /*** trivial ***/
216 *lr = x; /*** zig ***/
222 x->r = y->l; /*** zig-zig ***/
237 /******** circles and fringe */
241 typedef struct _circle {
243 double x, y; /* position */
244 double dx, dy; /* velocity */
246 int visible; /* default visibility */
247 struct _fringe *lo, *hi; /* lo and hi fringes */
249 int ni; /* number of intersections */
250 int *i; /* sorted intersection list */
253 typedef struct _fringe {
254 struct _fringe *l, *r; /* left and right children for splay trees */
256 circle *c; /* associated circle */
257 int side; /* 0 for lo, 1 for hi */
259 int mni; /* size of intersection array */
260 int ni; /* number of intersections */
261 int *i; /* sorted intersection list */
264 inline double fringe_x(fringe *f, double y) {
267 d = sqrt(f->c->r * f->c->r - dy * dy);
268 return f->side ? f->c->x + d : f->c->x - d;
271 inline void fringe_add_intersection(fringe *f, double x, double y) {
273 if (f->mni < f->ni) {
275 f->i = realloc(f->i, sizeof(int) * f->mni);
277 f->i[f->ni-1] = rint(atan2(y - f->c->y, x - f->c->x) * X_PI / M_PI);
280 circle *init_circles(int n, int w, int h) {
281 int i, r0, dr, speed;
283 double minradius, maxradius;
284 fringe *s = malloc(sizeof(fringe) * n * 2); /* never freed */
285 circle *c = malloc(sizeof(circle) * n);
287 speed = get_integer_resource("speed", "Speed");
288 minradius = get_float_resource("minradius", "Float");
289 maxradius = get_float_resource("maxradius", "Float");
290 if (maxradius < minradius)
291 maxradius = minradius;
293 r0 = ceil(minradius * h);
294 dr = floor(maxradius * h) - r0 + 1;
297 c[i].r = r0 + random() % dr;
298 c[i].x = c[i].r + frand(w - 1 - 2 * c[i].r);
299 c[i].y = c[i].r + frand(h - 1 - 2 * c[i].r);
300 c[i].visible = random() & 1;
306 v = (1 + frand(0.5)) * speed / 10.0;
307 c[i].dx = v * cos(a);
308 c[i].dy = v * sin(a);
312 c[i].lo->c = c[i].hi->c = c+i;
315 c[i].lo->mni = c[i].lo->ni = c[i].hi->mni = c[i].hi->ni = 0;
316 c[i].lo->i = c[i].hi->i = 0;
322 /* this is a hack, but I guess that's what I writing anyways */
323 void tweak_circle(circle *c) {
324 c->x += frand(2) - 1;
325 c->y += frand(1) + 0.1;
328 void move_circle(circle *c, int w, int h) {
334 else if (c->x >= w - c->r) {
343 else if (c->y >= h - c->r) {
349 /******** event queue */
355 typedef struct _event {
356 struct _event *l, *r; /* left and right children for splay tree */
358 int kind; /* type of event */
359 double x, y; /* position */
360 fringe *lo, *hi; /* fringes */
363 static double event_cut_y;
365 int event_cut(event *e) {
366 return event_cut_y == e->y ? 0 : event_cut_y < e->y ? -1 : 1;
369 void event_insert(event **eq, event *e) {
373 return; /* avoid leak */
377 *eq = (event*)splay((cut)event_cut, (tree*)*eq);
379 if (e->y == (*eq)->y) {
380 if (!((e->lo == (*eq)->lo && e->hi == (*eq)->hi) || (e->lo == (*eq)->hi && e->hi == (*eq)->lo))) {
382 e->r = 0; /* doing this instead of dying might be dangerous */
386 free(e); /* don't leak! */
388 else if (e->y < (*eq)->y) {
402 void circle_start_event(event **eq, circle *c) {
404 s = malloc(sizeof(event));
413 void circle_finish_event(event **eq, circle *c) {
415 f = malloc(sizeof(event));
424 event *event_next(event **eq) {
429 e = (event*)splay_min((tree*)*eq);
435 void event_shred(event *e) {
443 /******** fringe intersection */
445 inline int check_fringe_intersection(double ye, fringe *lo, fringe *hi, double x, double y) {
446 return ye <= y && ((x < lo->c->x) ^ lo->side) && ((x < hi->c->x) ^ hi->side);
449 void fringe_intersect(event **eq, double y, fringe *lo, fringe *hi) {
451 double dx, dy, sd, rs, rd, d, sx, sy, rp, sqd;
452 double x1, y1, x2, y2;
457 dx = hi->c->x - lo->c->x;
458 dy = hi->c->y - lo->c->y;
459 sd = dx * dx + dy * dy;
464 rs = hi->c->r + lo->c->r;
465 rd = hi->c->r - lo->c->r;
466 d = (rd * rd - sd) * (sd - rs * rs);
474 sx = (lo->c->x + hi->c->x) / 2;
475 sy = (lo->c->y + hi->c->y) / 2;
476 x1 = sx + sd * (dy * sqd - dx * rp);
477 y1 = sy - sd * (dx * sqd + dy * rp);
478 x2 = sx - sd * (dy * sqd + dx * rp);
479 y2 = sy + sd * (dx * sqd - dy * rp);
481 #define CHECK(xi, yi) (y <= yi && ((xi < lo->c->x) ^ lo->side) && ((xi < hi->c->x) ^ hi->side))
483 #define ADD_CROSS(xi, yi, ilo, ihi) { \
484 e = malloc(sizeof(event)); /* #### LEAK */ \
486 e->x = xi; e->y = yi; \
487 e->lo = ilo; e->hi = ihi; \
488 event_insert(eq, e); \
494 ADD_CROSS(x1, y1, lo, hi);
495 ADD_CROSS(x2, y2, hi, lo);
498 ADD_CROSS(x1, y1, hi, lo);
499 ADD_CROSS(x2, y2, lo, hi);
503 ADD_CROSS(x1, y1, lo, hi);
505 else if (CHECK(x2, y2))
506 ADD_CROSS(x2, y2, lo, hi);
511 /******** fringe trees and event handling */
513 #define PANIC ((fringe*)1) /* by alignment, no fringe should every be 1 */
515 fringe *check_lo(event **eq, double y, fringe *f, fringe *hi) {
517 f = (fringe*)splay_max((tree*)f);
518 fringe_intersect(eq, y, f, hi);
523 fringe *check_hi(event **eq, double y, fringe *lo, fringe *f) {
525 f = (fringe*)splay_min((tree*)f);
526 fringe_intersect(eq, y, lo, f);
531 double fringe_start_cut_x;
532 double fringe_start_cut_y;
534 int fringe_start_cut(fringe *f) {
535 double x = fringe_x(f, fringe_start_cut_y);
536 return fringe_start_cut_x == x ? 0 : fringe_start_cut_x < x ? -1 : 1;
539 fringe *fringe_start(event **eq, fringe *f, double x, double y, fringe *lo, fringe *hi) {
543 circle_finish_event(eq, lo->c);
550 fringe_start_cut_x = x;
551 fringe_start_cut_y = y;
552 f = (fringe*)splay((cut)fringe_start_cut, (tree*)f);
555 if (x == sx) { /* time to cheat my way out of handling degeneracies */
557 circle_start_event(eq, lo->c);
561 circle_finish_event(eq, lo->c);
562 f->l = check_lo(eq, y, f->l, lo);
563 fringe_intersect(eq, y, hi, f);
571 circle_finish_event(eq, lo->c);
572 fringe_intersect(eq, y, f, lo);
573 f->r = check_hi(eq, y, hi, f->r);
582 double fringe_double_cut_x;
583 double fringe_double_cut_y;
584 fringe *fringe_double_cut_lo;
585 fringe *fringe_double_cut_hi;
587 int fringe_double_cut(fringe *f) {
589 if (f == fringe_double_cut_lo || f == fringe_double_cut_hi)
591 x = fringe_x(f, fringe_double_cut_y);
592 return fringe_double_cut_x == x ? 0 : fringe_double_cut_x < x ? -1 : 1;
595 int fringe_double_splay(fringe *f, double x, double y, fringe *lo, fringe *hi) {
596 fringe_double_cut_x = x;
597 fringe_double_cut_y = y;
598 fringe_double_cut_lo = lo;
599 fringe_double_cut_hi = hi;
600 f = (fringe*)splay((cut)fringe_double_cut, (tree*)f);
603 return (f->r = (fringe*)splay_min((tree*)f->r)) == hi;
605 return (f->l = (fringe*)splay_max((tree*)f->l)) == lo;
610 fringe *fringe_cross(event **eq, fringe *f, double x, double y, fringe *lo, fringe *hi) {
612 if (!fringe_double_splay(f, x, y, lo, hi))
614 l = check_lo(eq, y, lo->l, hi);
615 r = check_hi(eq, y, lo, hi->r);
623 fringe *fringe_finish(event **eq, fringe *f, double x, double y, fringe *lo, fringe *hi) {
624 if (!fringe_double_splay(f, x, y, lo, hi))
631 lo->l = (fringe*)splay_max((tree*)lo->l);
632 hi->r = (fringe*)splay_min((tree*)hi->r);
633 fringe_intersect(eq, y, lo->l, hi->r);
640 /******** plane sweep */
642 void sweep(int n, circle *c) {
648 #define CHECK_PANIC() \
652 for (i=0;i<n;i++) { \
654 c[i].lo->ni = c[i].hi->ni = 0; \
661 circle_start_event(&eq, c+i);
664 while ((e = event_next(&eq))) {
667 f = fringe_start(&eq, f, e->x, e->y, e->lo, e->hi);
670 f = fringe_cross(&eq, f, e->x, e->y, e->lo, e->hi);
672 fringe_add_intersection(e->lo, e->x, e->y);
673 fringe_add_intersection(e->hi, e->x, e->y);
676 f = fringe_finish(&eq, f, e->x, e->y, e->lo, e->hi);
684 /******** circle drawing */
686 void adjust_circle_visibility(circle *c) {
689 n = c->lo->ni + c->hi->ni;
690 in = malloc(sizeof(int) * n);
691 for (i=0;i<c->hi->ni;i++)
693 for (i=c->lo->ni-1;i>=0;i--)
694 in[n-i-1] = c->lo->i[i] > 0 ? c->lo->i[i] : c->lo->i[i] + 2 * X_PI;
695 c->lo->ni = c->hi->ni = 0;
699 while (i < n && j < c->ni) /* whee */
700 a = (in[i] < c->i[j] ? in[i++] : c->i[j++]) - a;
707 c->visible = !c->visible;
713 #define ARC_BUFFER_SIZE 256
714 int arc_buffer_count = 0;
715 XArc arc_buffer[ARC_BUFFER_SIZE];
717 void flush_arc_buffer(Display *dpy, Drawable w, GC gc) {
718 if (arc_buffer_count) {
719 XDrawArcs(dpy, w, gc, arc_buffer, arc_buffer_count);
720 arc_buffer_count = 0;
724 void draw_circle(Display *dpy, Drawable w, GC gc, circle *c) {
726 adjust_circle_visibility(c);
728 xi = rint(c->x - c->r);
729 yi = rint(c->y - c->r);
732 #define ARC(p, a1, a2) { \
733 if (((p) & 1) ^ c->visible) { \
734 arc_buffer[arc_buffer_count].x = xi; \
735 arc_buffer[arc_buffer_count].y = yi; \
736 arc_buffer[arc_buffer_count].width = di; \
737 arc_buffer[arc_buffer_count].height = di; \
738 arc_buffer[arc_buffer_count].angle1 = -(a1); \
739 arc_buffer[arc_buffer_count].angle2 = (a1) - (a2); \
740 arc_buffer_count++; \
741 if (arc_buffer_count == ARC_BUFFER_SIZE) \
742 flush_arc_buffer(dpy, w, gc); \
749 ARC(0, c->i[c->ni-1], c->i[0] + 2 * X_PI)
750 for (i=1;i<c->ni;i++)
751 ARC(i, c->i[i-1], c->i[i])
754 /******** toplevel */
756 char *progclass = "Piecewise";
758 char *defaults [] = {
759 ".background: black",
760 ".foreground: white",
770 "*doubleBuffer: True",
771 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
773 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
777 XrmOptionDescRec options [] = {
778 { "-delay", ".delay", XrmoptionSepArg, 0 },
779 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
780 { "-speed", ".speed", XrmoptionSepArg, 0 },
781 { "-colorspeed", ".colorspeed", XrmoptionSepArg, 0 },
783 { "-count", ".count", XrmoptionSepArg, 0 },
784 { "-minradius", ".minradius", XrmoptionSepArg, 0 },
785 { "-maxradius", ".maxradius", XrmoptionSepArg, 0 },
787 { "-db", ".doubleBuffer", XrmoptionNoArg, "True" },
788 { "-no-db", ".doubleBuffer", XrmoptionNoArg, "False" },
793 check_for_leaks (void)
796 static unsigned long early_brk = 0;
797 unsigned long max = 30 * 1024 * 1024; /* 30 MB */
798 int b = (unsigned long) sbrk(0);
801 else if (b > early_brk + max)
803 fprintf (stderr, "%s: leaked %lu MB -- aborting!\n",
804 progname, ((b - early_brk) >> 20));
807 #endif /* HAVE_SBRK */
811 void screenhack(Display *dpy, Window window) {
816 GC erase_gc, draw_gc;
817 XWindowAttributes xgwa;
818 Pixmap b = 0, ba = 0, bb = 0; /* double-buffering pixmap */
820 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
821 XdbeBackBuffer backb = 0;
822 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
824 int count, delay, ncolors, colorspeed, color_index, flags, iterations;
825 int color_iterations;
828 count = get_integer_resource("count", "Integer");
829 delay = get_integer_resource("delay", "Integer");
830 ncolors = get_integer_resource("ncolors", "Integer");
831 colorspeed = get_integer_resource("colorspeed", "Integer");
832 dbuf = get_boolean_resource("doubleBuffer", "Boolean");
834 color_iterations = colorspeed ? 100 / colorspeed : 100000;
835 if (!color_iterations)
836 color_iterations = 1;
838 XGetWindowAttributes(dpy, window, &xgwa);
839 colors = calloc(sizeof(XColor), ncolors);
841 if (get_boolean_resource("mono", "Boolean")) {
844 colors[0].pixel = get_pixel_resource("foreground", "Foreground", dpy, xgwa.colormap);
847 make_color_loop(dpy, xgwa.colormap, 0, 1, 1, 120, 1, 1, 240, 1, 1, colors, &ncolors, True, False);
853 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
854 b = backb = xdbe_get_backbuffer(dpy, window, XdbeUndefined);
855 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
858 ba = XCreatePixmap(dpy, window, xgwa.width, xgwa.height,xgwa.depth);
859 bb = XCreatePixmap(dpy, window, xgwa.width, xgwa.height,xgwa.depth);
867 gcv.foreground = get_pixel_resource("background", "Background", dpy, xgwa.colormap);
868 erase_gc = XCreateGC (dpy, b, GCForeground, &gcv);
871 flags = GCForeground;
872 color_index = random() % ncolors;
873 gcv.foreground = colors[color_index].pixel;
874 draw_gc = XCreateGC(dpy, b, flags, &gcv);
876 /* initialize circles */
877 circles = init_circles(count, xgwa.width, xgwa.height);
881 XFillRectangle (dpy, b, erase_gc, 0, 0, xgwa.width, xgwa.height);
883 sweep(count, circles);
884 for (i=0;i<count;i++) {
885 draw_circle(dpy, b, draw_gc, circles+i);
886 move_circle(circles+i, xgwa.width, xgwa.height);
888 flush_arc_buffer(dpy, b, draw_gc);
890 if (++iterations % color_iterations == 0) {
891 color_index = (color_index + 1) % ncolors;
892 XSetForeground(dpy, draw_gc, colors[color_index].pixel);
895 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
897 XdbeSwapInfo info[1];
898 info[0].swap_window = window;
899 info[0].swap_action = XdbeUndefined;
900 XdbeSwapBuffers (dpy, info, 1);
903 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
905 XCopyArea (dpy, b, window, erase_gc, 0, 0, xgwa.width, xgwa.height, 0, 0);
906 b = (b == ba ? bb : ba);
910 screenhack_handle_events(dpy);