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;
296 /* This program idiotically does not go through a color map, so
297 we have to hardcode in knowledge of how jwxyz.a packs pixels!
298 Fix it to go through st->colors[st->ncolors] instead!
300 ret = 0xFF000000 | (r << 16) | (g << 8) | b;
302 ret |= (r << 16) | (g << 8) | b;
306 ret = ((r>>3) << 11) | ((g>>2)<<5) | (b>>3);
309 ret = ((r>>3) << 10) | ((g>>3)<<5) | (b>>3);
316 /* alpha blended point drawing -- this is Not Right and will likely fail on
317 * non-intel platforms as it is now, needs fixing */
318 static inline unsigned long trans_point(int x1, int y1, unsigned long myc, double a,
323 ref_pixel(f, x1, y1) = myc;
328 int or=0, og=0, ob=0;
333 c = ref_pixel(f, x1, y1);
335 point2rgb(f->visdepth, c, &or, &og, &ob);
336 point2rgb(f->visdepth, myc, &r, &g, &b);
338 nr = or + (r - or) * a;
339 ng = og + (g - og) * a;
340 nb = ob + (b - ob) * a;
342 c = rgb2point(f->visdepth, nr, ng, nb);
344 ref_pixel(f, x1, y1) = c;
350 static inline void drawPoint(int x, int y, unsigned long color, double intensity,
351 Display *dpy, Window window, GC fgc, struct field *f)
356 while ( x >= f->width ) x -= f->width;
357 while ( x < 0 ) x += f->width;
359 while ( y >= f->height ) y -= f->height;
360 while ( y < 0 ) y += f->height;
362 /* if ( in_bounds(f, x, y) ) ... */
364 c = trans_point(x, y, color, intensity, f);
366 XSetForeground(dpy, fgc, c);
367 XDrawPoint(dpy, window, fgc, x, y);
370 static inline void paint(SandPainter* painter, double ax, double ay, double bx, double by,
371 Display *dpy, Window window, GC fgc,
374 /* the sand painter */
379 /* XXX try adding tpoint here, like orig */
381 /* jitter the painter's values */
382 painter->gain += frand(0.05) - 0.025;
384 if ( painter->gain > f->max_gain )
385 painter->gain = -f->max_gain;
386 else if ( painter->gain < -f->max_gain )
387 painter->gain = f->max_gain;
389 painter->p += frand(0.1) - 0.05;
391 if ( 0 < painter->p )
393 else if ( painter->p > 1.0 )
396 /* replace 0.1 with 1 / f->grains */
397 inc = painter->gain * 0.1;
400 for(i = 0; i <= 10; ++i)
404 double intensity = 0.1 - 0.009 * i;
406 sp = sin(painter->p + sandp);
407 drawx = ax + (bx - ax) * sp;
408 drawy = ay + (by - ay) * sp;
410 drawPoint(drawx, drawy, painter->color,
412 dpy, window, fgc, f);
414 sm = sin(painter->p - sandp);
415 drawx = ax + (bx - ax) * sm;
416 drawy = ay + (by - ay) * sm;
418 drawPoint(drawx, drawy, painter->color,
420 dpy, window, fgc, f);
426 static void build_colors(struct field *f, Display *dpy, XWindowAttributes *xgwa)
431 /* Count the colors in our map and assign them in a horrifically inefficient
432 * manner but it only happens once */
434 for( f->numcolors = 0;
435 rgb_colormap[f->numcolors] != NULL;
441 f->parsedcolors = (unsigned long *) calloc(f->numcolors,
442 sizeof(unsigned long));
443 if ( f->parsedcolors == NULL )
445 fprintf(stderr, "%s: Failed to allocate parsedcolors\n",
450 for(i = 0; i < f->numcolors; ++i)
452 if (!XParseColor(dpy, xgwa->colormap,
453 rgb_colormap[i], &tmpcolor))
455 fprintf(stderr, "%s: couldn't parse color %s\n", progname,
460 if (!XAllocColor(dpy, xgwa->colormap, &tmpcolor))
462 fprintf(stderr, "%s: couldn't allocate color %s\n", progname,
467 f->parsedcolors[i] = tmpcolor.pixel;
472 /* used when the window is resized */
473 static void build_img(struct field *f)
480 f->off_img = (unsigned long *) calloc(f->width * f->height,
481 sizeof(unsigned long));
484 if ( f->off_img == NULL )
486 fprintf(stderr, "%s: Failed to allocate off_img\n",
491 memset(f->off_img, f->bgcolor,
492 sizeof(unsigned long) * f->width * f->height);
495 static void free_circles(struct field *f)
499 if ( f->circles != NULL )
501 for(i = 0; i < f->num_circles; ++i)
503 free (f->circles[i].painters);
511 static void build_field(Display *dpy, Window window, XWindowAttributes xgwa, GC fgc,
520 f->center_of_universe.x = f->width / 2.0;
521 f->center_of_universe.y = f->height / 2.0;
522 f->center_of_universe.r = MAX(f->width, f->height) / 2.0;
524 num_orbits = (f->percent_orbits * f->num_circles) / 100;
525 orbit_start = f->num_circles - num_orbits;
526 base_orbits = orbit_start + (num_orbits * f->base_orbits) / 100;
530 f->circles = (Circle*) calloc(f->num_circles, sizeof(Circle));
531 if ( f->circles == NULL )
533 fprintf(stderr, "%s: Failed to allocate off_img\n",
538 for(i = 0; i < f->num_circles; ++i)
541 Circle *circle = f->circles + i;
543 /* make this a pref */
545 if ( i >= orbit_start )
546 circle->path_type = ORBIT;
548 circle->path_type = LINEAR;
551 if ( circle->path_type == LINEAR )
553 circle->x = frand(f->width);
554 circle->y = frand(f->height);
556 circle->dx = frand(0.5) - 0.25;
557 circle->dy = frand(0.5) - 0.25;
558 /* circle->dy = 0; */
559 /* circle->r = f->height * (0.05 + frand(0.1)); */
560 circle->radius = 5 + frand(55);
562 /* in case we want orbits based on lines */
563 circle->r = MIN(f->width, f->height) / 2.0;
564 circle->center = NULL;
568 if (i < base_orbits )
570 if ( f->base_on_center )
571 circle->center = &f->center_of_universe;
574 circle->center = f->circles +
575 ((int)frand(orbit_start - 0.1));
578 circle->r = 1 + frand(MIN(f->width, f->height) / 2.0);
580 /* circle->radius = 5 + frand(55); */
584 /* give a preference for the earlier circles */
586 double p = frand(0.9);
588 circle->center = f->circles + (int) (p*i);
590 circle->r = 1 + 0.5 * circle->center->r + 0.5 * frand(circle->center->r);
591 /* circle->r = 1 + frand(circle->center->r / 2); */
594 /* circle->radius = MAX(5, frand(circle->r)); */
595 /* circle->radius = 5 + frand(55); */
598 circle->radius = 5 + frand(MIN(55, circle->r));
599 circle->dtheta = (frand(0.5) - 0.25) / circle->r;
600 circle->theta = frand(2 * M_PI);
602 circle->x = circle->r * cos(circle->theta) + circle->center->x;
603 circle->y = circle->r * sin(circle->theta) + circle->center->y;
607 /* make this a command line option */
608 circle->num_painters = 3;
609 circle->painters = (SandPainter*) calloc(circle->num_painters,
610 sizeof(SandPainter));
611 if ( circle->painters == NULL )
613 fprintf(stderr, "%s: failed to allocate painters", progname);
617 for(j = 0; j < circle->num_painters; ++j)
619 SandPainter *painter = circle->painters + j;
621 painter->gain = frand(0.09) + 0.01;
622 painter->p = frand(1.0);
624 f->parsedcolors[(int)(frand(0.999) * f->numcolors)];
629 static void moveCircles(struct field *f)
633 for(i = 0; i < f->num_circles; ++i)
635 Circle *circle = f->circles + i;
637 if ( circle->path_type == LINEAR )
639 circle->x += circle->dx;
640 circle->y += circle->dy;
643 if ( circle->x < -circle->radius )
644 circle->x = f->width + circle->radius;
645 else if ( circle->x >= f->width + circle->radius )
646 circle->x = -circle->radius;
648 if ( circle->y < -circle->radius )
649 circle->y = f->height + circle->radius;
650 else if ( circle->y >= f->height + circle->radius )
651 circle->y = -circle->radius;
653 if ( circle->x < 0 ) circle->x += f->width;
654 else if ( circle->x >= f->width ) circle->x -= f->width;
656 if ( circle->y < 0 ) circle->y += f->height;
657 else if ( circle->y >= f->height ) circle->y -= f->height;
660 else /* (circle->path_type == ORBIT) */
662 circle->theta += circle->dtheta;
664 if ( circle->theta < 0 ) circle->theta += 2 * M_PI;
665 else if ( circle->theta > 2 * M_PI ) circle->theta -= 2 * M_PI;
667 circle->x = circle->r * cos(circle->theta) + circle->center->x;
668 circle->y = circle->r * sin(circle->theta) + circle->center->y;
671 if ( circle->x < -circle->radius )
672 circle->x += f->width + 2 * circle->radius;
673 else if ( circle->x >= f->width + circle->radius )
674 circle->x -= f->width + 2 * circle->radius;
676 if ( circle->y < -circle->radius )
677 circle->y += f->height + 2 * circle->radius;
678 else if ( circle->y >= f->height + circle->radius )
679 circle->y -= f->height + 2 * circle->radius;
681 if ( circle->x < 0 ) circle->x += f->width;
682 else if ( circle->x >= f->width ) circle->x -= f->width;
684 if ( circle->y < 0 ) circle->y += f->height;
685 else if ( circle->y >= f->height ) circle->y -= f->height;
691 static void drawIntersections(Display *dpy, Window window, GC fgc, struct field *f)
695 /* One might be tempted to think 'hey, this is a crude algorithm
696 * that is going to check each of the n (n-1) / 2 possible
697 * intersections! Why not try bsp trees, quad trees, etc, etc,
700 * In practice the time spent drawing the intersection of two
701 * circles dwarfs the time takes to check for intersection.
702 * Profiling on a 640x480 screen with 100 circles shows possible
703 * speed gains to be only a couple of percent.
705 * But hey, if you're bored, go have fun. Let me know how it
710 for(i = 0; i < f->num_circles; ++i)
712 Circle *c1 = f->circles + i;
714 if ( !f->draw_centers )
716 /* the default branch */
718 for(j = i + 1; j < f->num_circles; ++j)
720 double d, dsqr, dx, dy;
721 Circle *c2 = f->circles + j;
724 ++f->possible_intersections;
729 dsqr = dx * dx + dy * dy;
732 if ( (fabs(dx) < (c1->radius + c2->radius)) &&
733 (fabs(dy) < (c1->radius + c2->radius)) &&
734 ( d < (c1->radius + c2->radius) ) &&
735 ( d > fabs(c1->radius - c2->radius) ) )
737 double d1, d2, r1sqr;
744 /* woo-hoo. the circles are neither outside nor
745 * inside each other. they intersect.
747 * Now, compute the coordinates of the points of
752 ++f->intersection_count;
755 /* unit vector in direction of c1 to c2 */
759 r1sqr = c1->radius * c1->radius;
761 /* distance from c1's center midpoint of intersection
764 d1 = 0.5 * (r1sqr - c2->radius * c2->radius + dsqr) / d;
766 midpx = c1->x + d1 * bx;
767 midpy = c1->y + d1 * by;
769 /* distance from midpoint to points of intersection */
771 d2 = sqrt(r1sqr - d1 * d1);
773 int1x = midpx + d2 * by;
774 int1y = midpy - d2 * bx;
776 int2x = midpx - d2 * by;
777 int2y = midpy + d2 * bx;
779 for(s = 0; s < c1->num_painters; ++s)
781 paint(c1->painters + s, int1x, int1y, int2x, int2y,
782 dpy, window, fgc, f);
787 else /* f->draw_centers */
789 XDrawPoint(dpy, window, fgc, c1->x, c1->y);
798 unsigned int max_cycles;
802 XWindowAttributes xgwa;
809 interaggregate_init (Display *dpy, Window window)
811 struct state *st = (struct state *) calloc (1, sizeof(*st));
815 struct timeval tm1, tm2;
821 st->f = init_field();
822 st->growth_delay = (get_integer_resource(st->dpy, "growthDelay", "Integer"));
823 st->max_cycles = (get_integer_resource(st->dpy, "maxCycles", "Integer"));
824 st->f->num_circles = (get_integer_resource(st->dpy, "numCircles", "Integer"));
825 st->f->percent_orbits = (get_integer_resource(st->dpy, "percentOrbits", "Integer"));
826 st->f->base_orbits = (get_integer_resource(st->dpy, "baseOrbits", "Integer"));
827 st->f->base_on_center = (get_boolean_resource(st->dpy, "baseOnCenter", "Boolean"));
828 st->f->draw_centers = (get_boolean_resource(st->dpy, "drawCenters", "Boolean"));
830 if (st->f->num_circles <= 1)
832 fprintf(stderr, "%s: Minimum number of circles is 2\n",
837 if ( (st->f->percent_orbits < 0) || (st->f->percent_orbits > 100) )
839 fprintf(stderr, "%s: percent-oribts must be between 0 and 100\n",
844 if ( (st->f->base_orbits < 0) || (st->f->base_orbits > 100) )
846 fprintf(stderr, "%s: base-oribts must be between 0 and 100\n",
851 if ( st->f->percent_orbits == 100 )
852 st->f->base_on_center = True;
854 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
856 build_colors(st->f, st->dpy, &st->xgwa);
858 st->gcv.foreground = get_pixel_resource(st->dpy, st->xgwa.colormap,
859 "foreground", "Foreground");
860 st->gcv.background = get_pixel_resource(st->dpy, st->xgwa.colormap,
861 "background", "Background");
863 st->fgc = XCreateGC(st->dpy, st->window, GCForeground, &st->gcv);
865 st->f->height = st->xgwa.height;
866 st->f->width = st->xgwa.width;
867 st->f->visdepth = st->xgwa.depth;
868 st->f->fgcolor = st->gcv.foreground;
869 st->f->bgcolor = st->gcv.background;
871 /* Initialize stuff */
872 build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
875 gettimeofday(&tm1, NULL);
884 interaggregate_draw (Display *dpy, Window window, void *closure)
886 struct state *st = (struct state *) closure;
888 if ((st->f->cycles % 10) == 0)
890 /* Restart if the window size changes */
891 XGetWindowAttributes(st->dpy, st->window, &st->xgwa);
893 if (st->f->height != st->xgwa.height || st->f->width != st->xgwa.width)
895 st->f->height = st->xgwa.height;
896 st->f->width = st->xgwa.width;
897 st->f->visdepth = st->xgwa.depth;
899 build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
900 XSetForeground(st->dpy, st->fgc, st->gcv.background);
901 XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
902 XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
907 drawIntersections(st->dpy, st->window, st->fgc, st->f);
912 if (st->f->cycles >= st->max_cycles && st->max_cycles != 0)
914 build_field(st->dpy, st->window, st->xgwa, st->fgc, st->f);
915 XSetForeground(st->dpy, st->fgc, st->gcv.background);
916 XFillRectangle(st->dpy, st->window, st->fgc, 0, 0, st->xgwa.width, st->xgwa.height);
917 XSetForeground(st->dpy, st->fgc, st->gcv.foreground);
922 gettimeofday(&tm2, NULL);
924 tdiff = (tm2.tv_sec - tm1.tv_sec)
925 + (tm2.tv_usec - tm1.tv_usec) * 0.00001;
929 fprintf(stderr, "fps: %d %f %f\n",
930 frames, tdiff, frames / tdiff );
932 fprintf(stderr, "intersections: %d %d %f\n",
933 f->intersection_count, f->possible_intersections,
934 ((double)f->intersection_count) /
935 f->possible_intersections);
937 fprintf(stderr, "fpi: %f\n",
938 ((double)frames) / f->intersection_count );
941 tm1.tv_sec = tm2.tv_sec;
942 tm1.tv_usec = tm2.tv_usec;
944 f->intersection_count = f->possible_intersections = 0;
948 return st->growth_delay;
953 interaggregate_reshape (Display *dpy, Window window, void *closure,
954 unsigned int w, unsigned int h)
959 interaggregate_event (Display *dpy, Window window, void *closure, XEvent *event)
965 interaggregate_free (Display *dpy, Window window, void *closure)
967 struct state *st = (struct state *) closure;
972 XSCREENSAVER_MODULE ("Interaggregate", interaggregate)