2 * InterAggregate (dagraz@gmail.com)
3 * Based on code from complexification.net Intersection Aggregate
4 * http://www.complexification.net/gallery/machines/interAggregate/index.php
6 * Intersection Aggregate code:
8 * Albuquerque, New Mexico
11 * Also based on substrate, a port of j.tarbell's Substrate Art done
12 * by dragorn@kismetwireless.net
15 * Interesting command line options, all of which serve to
16 * concentrate the painting in some way:
18 * -percent-orbits 100 -base-orbits 50 -base-on-center -growth-delay 0
20 * Paint should be concentrated in the center of the canvas, orbiting
21 * about it. -percent-orbits 100 implies -base-on-center, so that's
25 * -percent-orbits 99 -base-orbits 50 -growth-delay 0
27 * Like the above example, but the 'center' will rove about the screen.
29 * -percent-orbits 98 -base-orbits 50 -growth-delay 0
31 * Like the above example, but there will be two roving centers.
35 * -fix alpha blending / byte ordering
40 * Directly based the hacks of:
42 * xscreensaver, Copyright (c) 1997, 1998, 2002 Jamie Zawinski <jwz@jwz.org>
44 * Permission to use, copy, modify, distribute, and sell this software and its
45 * documentation for any purpose is hereby granted without fee, provided that
46 * the above copyright notice appear in all copies and that both that
47 * copyright notice and this permission notice appear in supporting
48 * documentation. No representations are made about the suitability of this
49 * software for any purpose. It is provided "as is" without express or
54 #include "screenhack.h"
57 /* this program goes faster if some functions are inline. The following is
58 * borrowed from ifs.c */
59 #if !defined( __GNUC__ ) && !defined(__cplusplus) && !defined(c_plusplus)
65 #define MIN(x,y) ((x < y) ? x : y)
69 #define MAX(x,y) ((x < y) ? y : x)
72 static const char *interaggregate_defaults[] =
81 "*growthDelay: 18000",
86 "*baseOnCenter: False",
87 "*drawCenters: False",
91 static XrmOptionDescRec interaggregate_options[] =
93 {"-background", ".background", XrmoptionSepArg, 0},
94 {"-foreground", ".foreground", XrmoptionSepArg, 0},
95 {"-max-cycles", ".maxCycles", XrmoptionSepArg, 0},
96 {"-growth-delay", ".growthDelay", XrmoptionSepArg, 0},
97 {"-num-circles", ".numCircles", XrmoptionSepArg, 0},
98 {"-percent-orbits", ".percentOrbits", XrmoptionSepArg, 0},
99 {"-base-orbits", ".baseOrbits", XrmoptionSepArg, 0},
100 {"-base-on-center", ".baseOnCenter", XrmoptionNoArg, "true"},
101 {"-draw-centers", ".drawCenters", XrmoptionNoArg, "true"},
105 /* Raw colormap extracted from pollockEFF.gif */
107 char *rgb_colormap[] =
109 "#FFFFFF", /* white */
110 "#000000", /* black */
111 "#000000", /* more black */
113 "#4e3e2e", /* olive */
115 "#694d35", /* camel */
121 static const char *rgb_colormap[] =
123 "#FFFFFF", /* white */
124 "#000000", /* more black */
125 "#000000", /* more black */
126 "#4e3e2e", /* olive */
127 "#694d35", /* camel */
133 /* black white brown olive grey camel */
135 typedef enum { LINEAR, ORBIT } PathType;
146 typedef struct _circle
155 /* for a linear path */
159 /* for orbital path */
164 struct _circle* center;
167 SandPainter* painters;
184 /* used for orbits circling the center of the screen */
185 Circle center_of_universe;
187 /* Raw map of pixels we need to keep for alpha blending */
188 unsigned long int *off_img;
192 unsigned long *parsedcolors;
193 unsigned long fgcolor;
194 unsigned long bgcolor;
204 /* for profiling whatnot */
205 int possible_intersections;
206 int intersection_count;
210 static struct field *
213 struct field *f = (struct field*) malloc(sizeof(struct field));
216 fprintf(stderr, "%s: Failed to allocate field!\n", progname);
224 f->percent_orbits = 0;
226 f->base_on_center = False;
229 f->parsedcolors = NULL;
238 f->draw_centers = False;
240 f->possible_intersections = 0;
241 f->intersection_count = 0;
246 /* Quick references to pixels in the offscreen map and in the crack grid */
247 #define ref_pixel(f, x, y) ((f)->off_img[(y) * (f)->width + (x)])
249 #define in_bounds(f, x, y) ((x >= 0) && (x < f->width) && (y >= 0) && (y < f->height))
251 /* Consider rewriting with XQueryColor, or ImageByteOrder */
253 static inline void point2rgb(int depth, unsigned long c, int *r, int *g, int *b)
260 /* This program idiotically does not go through a color map, so
261 we have to hardcode in knowledge of how jwxyz.a packs pixels!
262 Fix it to go through st->colors[st->ncolors] instead!
264 *r = (c & 0x00ff0000) >> 16;
265 *g = (c & 0x0000ffff) >> 8;
266 *b = (c & 0x000000ff);
269 *g = (c & 0xff00) >> 8;
270 *r = (c & 0xff0000) >> 16;
274 *b = (c & 0x1f) << 3;
275 *g = ((c >> 5) & 0x3f) << 2;
276 *r = ((c >> 11) & 0x1f) << 3;
279 *b = (c & 0x1f) << 3;
280 *g = ((c >> 5) & 0x1f) << 3;
281 *r = ((c >> 10) & 0x1f) << 3;
286 static inline unsigned long rgb2point(int depth, int r, int g, int b)
288 unsigned long ret = 0;
295 /* This program idiotically does not go through a color map, so
296 we have to hardcode in knowledge of how jwxyz.a packs pixels!
297 Fix it to go through st->colors[st->ncolors] instead!
299 ret = 0xFF000000 | (r << 16) | (g << 8) | b;
301 ret |= (r << 16) | (g << 8) | b;
305 ret = ((r>>3) << 11) | ((g>>2)<<5) | (b>>3);
308 ret = ((r>>3) << 10) | ((g>>3)<<5) | (b>>3);
315 /* alpha blended point drawing -- this is Not Right and will likely fail on
316 * non-intel platforms as it is now, needs fixing */
317 static inline unsigned long trans_point(int x1, int y1, unsigned long myc, double a,
322 ref_pixel(f, x1, y1) = myc;
327 int or=0, og=0, ob=0;
332 c = ref_pixel(f, x1, y1);
334 point2rgb(f->visdepth, c, &or, &og, &ob);
335 point2rgb(f->visdepth, myc, &r, &g, &b);
337 nr = or + (r - or) * a;
338 ng = og + (g - og) * a;
339 nb = ob + (b - ob) * a;
341 c = rgb2point(f->visdepth, nr, ng, nb);
343 ref_pixel(f, x1, y1) = c;
349 static inline void drawPoint(int x, int y, unsigned long color, double intensity,
350 Display *dpy, Window window, GC fgc, struct field *f)
355 while ( x >= f->width ) x -= f->width;
356 while ( x < 0 ) x += f->width;
358 while ( y >= f->height ) y -= f->height;
359 while ( y < 0 ) y += f->height;
361 /* if ( in_bounds(f, x, y) ) ... */
363 c = trans_point(x, y, color, intensity, f);
365 XSetForeground(dpy, fgc, c);
366 XDrawPoint(dpy, window, fgc, x, y);
369 static inline void paint(SandPainter* painter, double ax, double ay, double bx, double by,
370 Display *dpy, Window window, GC fgc,
373 /* the sand painter */
378 /* XXX try adding tpoint here, like orig */
380 /* jitter the painter's values */
381 painter->gain += frand(0.05) - 0.025;
383 if ( painter->gain > f->max_gain )
384 painter->gain = -f->max_gain;
385 else if ( painter->gain < -f->max_gain )
386 painter->gain = f->max_gain;
388 painter->p += frand(0.1) - 0.05;
390 if ( 0 < painter->p )
392 else if ( painter->p > 1.0 )
395 /* replace 0.1 with 1 / f->grains */
396 inc = painter->gain * 0.1;
399 for(i = 0; i <= 10; ++i)
403 double intensity = 0.1 - 0.009 * i;
405 sp = sin(painter->p + sandp);
406 drawx = ax + (bx - ax) * sp;
407 drawy = ay + (by - ay) * sp;
409 drawPoint(drawx, drawy, painter->color,
411 dpy, window, fgc, f);
413 sm = sin(painter->p - sandp);
414 drawx = ax + (bx - ax) * sm;
415 drawy = ay + (by - ay) * sm;
417 drawPoint(drawx, drawy, painter->color,
419 dpy, window, fgc, f);
425 static void build_colors(struct field *f, Display *dpy, XWindowAttributes *xgwa)
430 /* Count the colors in our map and assign them in a horrifically inefficient
431 * manner but it only happens once */
433 for( f->numcolors = 0;
434 rgb_colormap[f->numcolors] != NULL;
440 f->parsedcolors = (unsigned long *) calloc(f->numcolors,
441 sizeof(unsigned long));
442 if ( f->parsedcolors == NULL )
444 fprintf(stderr, "%s: Failed to allocate parsedcolors\n",
449 for(i = 0; i < f->numcolors; ++i)
451 if (!XParseColor(dpy, xgwa->colormap,
452 rgb_colormap[i], &tmpcolor))
454 fprintf(stderr, "%s: couldn't parse color %s\n", progname,
459 if (!XAllocColor(dpy, xgwa->colormap, &tmpcolor))
461 fprintf(stderr, "%s: couldn't allocate color %s\n", progname,
466 f->parsedcolors[i] = tmpcolor.pixel;
471 /* used when the window is resized */
472 static void build_img(struct field *f)
479 f->off_img = (unsigned long *) calloc(f->width * f->height,
480 sizeof(unsigned long));
483 if ( f->off_img == NULL )
485 fprintf(stderr, "%s: Failed to allocate off_img\n",
490 memset(f->off_img, f->bgcolor,
491 sizeof(unsigned long) * f->width * f->height);
494 static void free_circles(struct field *f)
498 if ( f->circles != NULL )
500 for(i = 0; i < f->num_circles; ++i)
502 free (f->circles[i].painters);
510 static void build_field(Display *dpy, Window window, XWindowAttributes xgwa, GC fgc,
519 f->center_of_universe.x = f->width / 2.0;
520 f->center_of_universe.y = f->height / 2.0;
521 f->center_of_universe.r = MAX(f->width, f->height) / 2.0;
523 num_orbits = (f->percent_orbits * f->num_circles) / 100;
524 orbit_start = f->num_circles - num_orbits;
525 base_orbits = orbit_start + (num_orbits * f->base_orbits) / 100;
529 f->circles = (Circle*) calloc(f->num_circles, sizeof(Circle));
530 if ( f->circles == NULL )
532 fprintf(stderr, "%s: Failed to allocate off_img\n",
537 for(i = 0; i < f->num_circles; ++i)
540 Circle *circle = f->circles + i;
542 /* make this a pref */
544 if ( i >= orbit_start )
545 circle->path_type = ORBIT;
547 circle->path_type = LINEAR;
550 if ( circle->path_type == LINEAR )
552 circle->x = frand(f->width);
553 circle->y = frand(f->height);
555 circle->dx = frand(0.5) - 0.25;
556 circle->dy = frand(0.5) - 0.25;
557 /* circle->dy = 0; */
558 /* circle->r = f->height * (0.05 + frand(0.1)); */
559 circle->radius = 5 + frand(55);
561 /* in case we want orbits based on lines */
562 circle->r = MIN(f->width, f->height) / 2.0;
563 circle->center = NULL;
567 if (i < base_orbits )
569 if ( f->base_on_center )
570 circle->center = &f->center_of_universe;
573 circle->center = f->circles +
574 ((int)frand(orbit_start - 0.1));
577 circle->r = 1 + frand(MIN(f->width, f->height) / 2.0);
579 /* circle->radius = 5 + frand(55); */
583 /* give a preference for the earlier circles */
585 double p = frand(0.9);
587 circle->center = f->circles + (int) (p*i);
589 circle->r = 1 + 0.5 * circle->center->r + 0.5 * frand(circle->center->r);
590 /* circle->r = 1 + frand(circle->center->r / 2); */
593 /* circle->radius = MAX(5, frand(circle->r)); */
594 /* circle->radius = 5 + frand(55); */
597 circle->radius = 5 + frand(MIN(55, circle->r));
598 circle->dtheta = (frand(0.5) - 0.25) / circle->r;
599 circle->theta = frand(2 * M_PI);
601 circle->x = circle->r * cos(circle->theta) + circle->center->x;
602 circle->y = circle->r * sin(circle->theta) + circle->center->y;
606 /* make this a command line option */
607 circle->num_painters = 3;
608 circle->painters = (SandPainter*) calloc(circle->num_painters,
609 sizeof(SandPainter));
610 if ( circle->painters == NULL )
612 fprintf(stderr, "%s: failed to allocate painters", progname);
616 for(j = 0; j < circle->num_painters; ++j)
618 SandPainter *painter = circle->painters + j;
620 painter->gain = frand(0.09) + 0.01;
621 painter->p = frand(1.0);
623 f->parsedcolors[(int)(frand(0.999) * f->numcolors)];
628 static void moveCircles(struct field *f)
632 for(i = 0; i < f->num_circles; ++i)
634 Circle *circle = f->circles + i;
636 if ( circle->path_type == LINEAR )
638 circle->x += circle->dx;
639 circle->y += circle->dy;
642 if ( circle->x < -circle->radius )
643 circle->x = f->width + circle->radius;
644 else if ( circle->x >= f->width + circle->radius )
645 circle->x = -circle->radius;
647 if ( circle->y < -circle->radius )
648 circle->y = f->height + circle->radius;
649 else if ( circle->y >= f->height + circle->radius )
650 circle->y = -circle->radius;
652 if ( circle->x < 0 ) circle->x += f->width;
653 else if ( circle->x >= f->width ) circle->x -= f->width;
655 if ( circle->y < 0 ) circle->y += f->height;
656 else if ( circle->y >= f->height ) circle->y -= f->height;
659 else /* (circle->path_type == ORBIT) */
661 circle->theta += circle->dtheta;
663 if ( circle->theta < 0 ) circle->theta += 2 * M_PI;
664 else if ( circle->theta > 2 * M_PI ) circle->theta -= 2 * M_PI;
666 circle->x = circle->r * cos(circle->theta) + circle->center->x;
667 circle->y = circle->r * sin(circle->theta) + circle->center->y;
670 if ( circle->x < -circle->radius )
671 circle->x += f->width + 2 * circle->radius;
672 else if ( circle->x >= f->width + circle->radius )
673 circle->x -= f->width + 2 * circle->radius;
675 if ( circle->y < -circle->radius )
676 circle->y += f->height + 2 * circle->radius;
677 else if ( circle->y >= f->height + circle->radius )
678 circle->y -= f->height + 2 * circle->radius;
680 if ( circle->x < 0 ) circle->x += f->width;
681 else if ( circle->x >= f->width ) circle->x -= f->width;
683 if ( circle->y < 0 ) circle->y += f->height;
684 else if ( circle->y >= f->height ) circle->y -= f->height;
690 static void drawIntersections(Display *dpy, Window window, GC fgc, struct field *f)
694 /* One might be tempted to think 'hey, this is a crude algorithm
695 * that is going to check each of the n (n-1) / 2 possible
696 * intersections! Why not try bsp trees, quad trees, etc, etc,
699 * In practice the time spent drawing the intersection of two
700 * circles dwarfs the time takes to check for intersection.
701 * Profiling on a 640x480 screen with 100 circles shows possible
702 * speed gains to be only a couple of percent.
704 * But hey, if you're bored, go have fun. Let me know how it
709 for(i = 0; i < f->num_circles; ++i)
711 Circle *c1 = f->circles + i;
713 if ( !f->draw_centers )
715 /* the default branch */
717 for(j = i + 1; j < f->num_circles; ++j)
719 double d, dsqr, dx, dy;
720 Circle *c2 = f->circles + j;
723 ++f->possible_intersections;
728 dsqr = dx * dx + dy * dy;
731 if ( (fabs(dx) < (c1->radius + c2->radius)) &&
732 (fabs(dy) < (c1->radius + c2->radius)) &&
733 ( d < (c1->radius + c2->radius) ) &&
734 ( d > fabs(c1->radius - c2->radius) ) )
736 double d1, d2, r1sqr;
743 /* woo-hoo. the circles are neither outside nor
744 * inside each other. they intersect.
746 * Now, compute the coordinates of the points of
751 ++f->intersection_count;
754 /* unit vector in direction of c1 to c2 */
758 r1sqr = c1->radius * c1->radius;
760 /* distance from c1's center midpoint of intersection
763 d1 = 0.5 * (r1sqr - c2->radius * c2->radius + dsqr) / d;
765 midpx = c1->x + d1 * bx;
766 midpy = c1->y + d1 * by;
768 /* distance from midpoint to points of intersection */
770 d2 = sqrt(r1sqr - d1 * d1);
772 int1x = midpx + d2 * by;
773 int1y = midpy - d2 * bx;
775 int2x = midpx - d2 * by;
776 int2y = midpy + d2 * bx;
778 for(s = 0; s < c1->num_painters; ++s)
780 paint(c1->painters + s, int1x, int1y, int2x, int2y,
781 dpy, window, fgc, f);
786 else /* f->draw_centers */
788 XDrawPoint(dpy, window, fgc, c1->x, c1->y);
797 unsigned int max_cycles;
801 XWindowAttributes xgwa;
808 interaggregate_init (Display *dpy, Window window)
810 struct state *st = (struct state *) calloc (1, sizeof(*st));
814 struct timeval tm1, tm2;
820 st->f = init_field();
821 st->growth_delay = (get_integer_resource(st->dpy, "growthDelay", "Integer"));
822 st->max_cycles = (get_integer_resource(st->dpy, "maxCycles", "Integer"));
823 st->f->num_circles = (get_integer_resource(st->dpy, "numCircles", "Integer"));
824 st->f->percent_orbits = (get_integer_resource(st->dpy, "percentOrbits", "Integer"));
825 st->f->base_orbits = (get_integer_resource(st->dpy, "baseOrbits", "Integer"));
826 st->f->base_on_center = (get_boolean_resource(st->dpy, "baseOnCenter", "Boolean"));
827 st->f->draw_centers = (get_boolean_resource(st->dpy, "drawCenters", "Boolean"));
829 if (st->f->num_circles <= 1)
831 fprintf(stderr, "%s: Minimum number of circles is 2\n",
836 if ( (st->f->percent_orbits < 0) || (st->f->percent_orbits > 100) )
838 fprintf(stderr, "%s: percent-oribts must be between 0 and 100\n",
843 if ( (st->f->base_orbits < 0) || (st->f->base_orbits > 100) )
845 fprintf(stderr, "%s: base-oribts must be between 0 and 100\n",
850 if ( st->f->percent_orbits == 100 )
851 st->f->base_on_center = True;
853 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
855 build_colors(st->f, st->dpy, &st->xgwa);
857 st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
858 "foreground", "Foreground");
859 st->gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
860 "background", "Background");
862 st->fgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv);
864 st->f->height = st->xgwa.height;
865 st->f->width = st->xgwa.width;
866 st->f->visdepth = st->xgwa.depth;
867 st->f->fgcolor = st->gcv.foreground;
868 st->f->bgcolor = st->gcv.background;
870 /* Initialize stuff */
871 build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
874 gettimeofday(&tm1, NULL);
883 interaggregate_draw (Display *dpy, Window window, void *closure)
885 struct state *st = (struct state *) closure;
887 if ((st->f->cycles % 10) == 0)
889 /* Restart if the window size changes */
890 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
892 if (st->f->height != st->xgwa.height || st->f->width != st->xgwa.width)
894 st->f->height = st->xgwa.height;
895 st->f->width = st->xgwa.width;
896 st->f->visdepth = st->xgwa.depth;
898 build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
899 XSetForeground(st->dpy, st->fgc, st->gcv.background);
900 XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
901 XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
906 drawIntersections(st->dpy, st->window, st->fgc, st->f);
911 if (st->f->cycles >= st->max_cycles && st->max_cycles != 0)
913 build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
914 XSetForeground(st->dpy, st->fgc, st->gcv.background);
915 XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
916 XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
921 gettimeofday(&tm2, NULL);
923 tdiff = (tm2.tv_sec - tm1.tv_sec)
924 + (tm2.tv_usec - tm1.tv_usec) * 0.00001;
928 fprintf(stderr, "fps: %d %f %f\n",
929 frames, tdiff, frames / tdiff );
931 fprintf(stderr, "intersections: %d %d %f\n",
932 f->intersection_count, f->possible_intersections,
933 ((double)f->intersection_count) /
934 f->possible_intersections);
936 fprintf(stderr, "fpi: %f\n",
937 ((double)frames) / f->intersection_count );
940 tm1.tv_sec = tm2.tv_sec;
941 tm1.tv_usec = tm2.tv_usec;
943 f->intersection_count = f->possible_intersections = 0;
947 return st->growth_delay;
952 interaggregate_reshape (Display *dpy, Window window, void *closure,
953 unsigned int w, unsigned int h)
958 interaggregate_event (Display *dpy, Window window, void *closure, XEvent *event)
964 interaggregate_free (Display *dpy, Window window, void *closure)
966 struct state *st = (struct state *) closure;
971 XSCREENSAVER_MODULE ("Interaggregate", interaggregate)