1 /* -*- Mode: C; tab-width: 4 -*-
2 * hop --- real plane fractals.
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)hop.c 4.02 97/04/01 xlockmore";
8 /* Copyright (c) 1988-91 by Patrick J. Naughton.
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and that
13 * both that copyright notice and this permission notice appear in
14 * supporting documentation.
16 * This file is provided AS IS with no warranties of any kind. The author
17 * shall have no liability with respect to the infringement of copyrights,
18 * trade secrets or any patents by this file or any part thereof. In no
19 * event will the author be liable for any lost revenue or profits or
20 * other special, indirect and consequential damages.
23 * Changes of David Bagley <bagleyd@bigfoot.com>
24 * 24-Jun-97: EJK and RR functions stolen from xmartin2.2
25 * Ed Kubaitis <ejk@ux2.cso.uiuc.edu> ejk functions and xmartin
26 * Renaldo Recuerdo rr function, generalized exponent version
27 * of the Barry Martin's square root function
28 * 10-May-97: jwz@jwz.org: ported from xlockmore 4.03a10 to be a
29 * standalone program and thus usable with xscreensaver
30 * (I threw away my 1992 port and started over.)
31 * 27-Jul-95: added Peter de Jong's hop from Scientific American
32 * July 87 p. 111. Sometimes they are amazing but there are a
33 * few duds (I did not see a pattern in the parameters).
34 * 29-Mar-95: changed name from hopalong to hop
35 * 09-Dec-94: added Barry Martin's sine hop
37 * (12-Aug-92: jwz@lucid.com: made xlock version run standalone.)
39 * Changes of Patrick J. Naughton
40 * 29-Oct-90: fix bad (int) cast.
41 * 29-Jul-90: support for multiple screens.
42 * 08-Jul-90: new timing and colors and new algorithm for fractals.
43 * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors.
44 * 08-Oct-89: Fixed long standing typo bug in RandomInitHop();
45 * Fixed bug in memory allocation in init_hop();
46 * Moved seconds() to an extern.
47 * Got rid of the % mod since .mod is slow on a sparc.
49 * 31-Aug-88: Forked from xlock.c for modularity.
50 * 23-Mar-88: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
51 * Hopalong was attributed to Barry Martin of Aston University
52 * (Birmingham, England)
56 # define PROGCLASS "Hopalong"
57 # define HACK_INIT init_hop
58 # define HACK_DRAW draw_hop
59 # define HACK_FREE release_hop
60 # define hop_opts xlockmore_opts
61 # define DEFAULTS "*delay: 10000 \n" \
65 # define SMOOTH_COLORS
66 # include "xlockmore.h" /* from the xscreensaver distribution */
68 #else /* !STANDALONE */
69 # include "xlock.h" /* from the xlockmore distribution */
70 #endif /* !STANDALONE */
72 #define DEF_MARTIN "False"
73 #define DEF_POPCORN "False"
74 #define DEF_EJK1 "False"
75 #define DEF_EJK2 "False"
76 #define DEF_EJK3 "False"
77 #define DEF_EJK4 "False"
78 #define DEF_EJK5 "False"
79 #define DEF_EJK6 "False"
80 #define DEF_RR "False"
81 #define DEF_JONG "False"
82 #define DEF_SINE "False"
96 static XrmOptionDescRec opts[] =
98 {"-martin", ".hop.martin", XrmoptionNoArg, (caddr_t) "on"},
99 {"+martin", ".hop.martin", XrmoptionNoArg, (caddr_t) "off"},
100 {"-popcorn", ".hop.popcorn", XrmoptionNoArg, (caddr_t) "on"},
101 {"+popcorn", ".hop.popcorn", XrmoptionNoArg, (caddr_t) "off"},
102 {"-ejk1", ".hop.ejk1", XrmoptionNoArg, (caddr_t) "on"},
103 {"+ejk1", ".hop.ejk1", XrmoptionNoArg, (caddr_t) "off"},
104 {"-ejk2", ".hop.ejk2", XrmoptionNoArg, (caddr_t) "on"},
105 {"+ejk2", ".hop.ejk2", XrmoptionNoArg, (caddr_t) "off"},
106 {"-ejk3", ".hop.ejk3", XrmoptionNoArg, (caddr_t) "on"},
107 {"+ejk3", ".hop.ejk3", XrmoptionNoArg, (caddr_t) "off"},
108 {"-ejk4", ".hop.ejk4", XrmoptionNoArg, (caddr_t) "on"},
109 {"+ejk4", ".hop.ejk4", XrmoptionNoArg, (caddr_t) "off"},
110 {"-ejk5", ".hop.ejk5", XrmoptionNoArg, (caddr_t) "on"},
111 {"+ejk5", ".hop.ejk5", XrmoptionNoArg, (caddr_t) "off"},
112 {"-ejk6", ".hop.ejk6", XrmoptionNoArg, (caddr_t) "on"},
113 {"+ejk6", ".hop.ejk6", XrmoptionNoArg, (caddr_t) "off"},
114 {"-rr", ".hop.rr", XrmoptionNoArg, (caddr_t) "on"},
115 {"+rr", ".hop.rr", XrmoptionNoArg, (caddr_t) "off"},
116 {"-jong", ".hop.jong", XrmoptionNoArg, (caddr_t) "on"},
117 {"+jong", ".hop.jong", XrmoptionNoArg, (caddr_t) "off"},
118 {"-sine", ".hop.sine", XrmoptionNoArg, (caddr_t) "on"},
119 {"+sine", ".hop.sine", XrmoptionNoArg, (caddr_t) "off"}
121 static argtype vars[] =
123 {(caddr_t *) & martin, "martin", "Martin", DEF_MARTIN, t_Bool},
124 {(caddr_t *) & popcorn, "popcorn", "Popcorn", DEF_POPCORN, t_Bool},
125 {(caddr_t *) & ejk1, "ejk1", "EJK1", DEF_EJK1, t_Bool},
126 {(caddr_t *) & ejk2, "ejk2", "EJK2", DEF_EJK2, t_Bool},
127 {(caddr_t *) & ejk3, "ejk3", "EJK3", DEF_EJK3, t_Bool},
128 {(caddr_t *) & ejk4, "ejk4", "EJK4", DEF_EJK4, t_Bool},
129 {(caddr_t *) & ejk5, "ejk5", "EJK5", DEF_EJK5, t_Bool},
130 {(caddr_t *) & ejk6, "ejk6", "EJK6", DEF_EJK6, t_Bool},
131 {(caddr_t *) & rr, "rr", "RR", DEF_RR, t_Bool},
132 {(caddr_t *) & jong, "jong", "Jong", DEF_JONG, t_Bool},
133 {(caddr_t *) & sine, "sine", "Sine", DEF_SINE, t_Bool}
135 static OptionStruct desc[] =
137 {"-/+martin", "turn on/off sqrt format"},
138 {"-/+popcorn", "turn on/off Clifford A. Pickover's popcorn format"},
139 {"-/+ejk1", "turn on/off ejk1 format"},
140 {"-/+ejk2", "turn on/off ejk2 format"},
141 {"-/+ejk3", "turn on/off ejk3 format"},
142 {"-/+ejk4", "turn on/off ejk4 format"},
143 {"-/+ejk5", "turn on/off ejk5 format"},
144 {"-/+ejk6", "turn on/off ejk6 format"},
145 {"-/+rr", "turn on/off rr format"},
146 {"-/+jong", "turn on/off jong format"},
147 {"-/+sine", "turn on/off sine format"}
150 ModeSpecOpt hop_opts =
151 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
154 ModStruct hop_description =
155 {"hop", "init_hop", "draw_hop", "release_hop",
156 "refresh_hop", "init_hop", NULL, &hop_opts,
157 10000, 1000, 2500, 1, 64, 1.0, "",
158 "Shows real plane iterated fractals", 0, NULL};
174 #define OPS 8 /* 8, 9, 10 might be too close to a swastika for some... */
180 int centerx, centery; /* center of the screen */
182 double i, j; /* hopalong parameters */
188 XPoint *pointBuffer; /* pointer for XDrawPoints */
191 static hopstruct *hops = NULL;
194 init_hop(ModeInfo * mi)
196 Display *display = MI_DISPLAY(mi);
202 if ((hops = (hopstruct *) calloc(MI_NUM_SCREENS(mi),
203 sizeof (hopstruct))) == NULL)
206 hp = &hops[MI_SCREEN(mi)];
208 hp->centerx = MI_WIDTH(mi) / 2;
209 hp->centery = MI_HEIGHT(mi) / 2;
210 /* Make the other operations less common since they are less interesting */
211 if (MI_IS_FULLRANDOM(mi)) {
240 range = sqrt((double) hp->centerx * hp->centerx +
241 (double) hp->centery * hp->centery) / (1.0 + LRAND() / MAXRAND);
243 hp->inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
248 hp->a = (LRAND() / MAXRAND) * 1500.0 + 40.0;
249 hp->b = (LRAND() / MAXRAND) * 17.0 + 3.0;
250 hp->c = (LRAND() / MAXRAND) * 3000.0 + 100.0;
252 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
253 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
255 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
259 if (MI_IS_VERBOSE(mi))
260 (void) fprintf(stdout, "sqrt a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
264 hp->a = (LRAND() / MAXRAND) * 500.0;
265 hp->c = (LRAND() / MAXRAND) * 100.0 + 10.0;
267 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
268 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 40.0;
270 hp->b = (LRAND() / MAXRAND) * 0.4;
271 if (MI_IS_VERBOSE(mi))
272 (void) fprintf(stdout, "ejk1 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
276 hp->a = (LRAND() / MAXRAND) * 500.0;
278 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
280 hp->b = pow(10.0, 6.0 + (LRAND() / MAXRAND) * 24.0);
283 hp->c = pow(10.0, (LRAND() / MAXRAND) * 9.0);
286 if (MI_IS_VERBOSE(mi))
287 (void) fprintf(stdout, "ejk2 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
291 hp->a = (LRAND() / MAXRAND) * 500.0;
292 hp->c = (LRAND() / MAXRAND) * 80.0 + 30.0;
294 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
295 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 70.0;
297 hp->b = (LRAND() / MAXRAND) * 0.35 + 0.5;
298 if (MI_IS_VERBOSE(mi))
299 (void) fprintf(stdout, "ejk3 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
303 hp->a = (LRAND() / MAXRAND) * 1000.0;
304 hp->c = (LRAND() / MAXRAND) * 40.0 + 30.0;
306 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 2.0;
307 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
309 hp->b = (LRAND() / MAXRAND) * 9.0 + 1.0;
310 if (MI_IS_VERBOSE(mi))
311 (void) fprintf(stdout, "ejk4 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
315 hp->a = (LRAND() / MAXRAND) * 600.0;
316 hp->c = (LRAND() / MAXRAND) * 90.0 + 20.0;
318 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 2.0;
319 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
321 hp->b = (LRAND() / MAXRAND) * 0.3 + 0.1;
322 if (MI_IS_VERBOSE(mi))
323 (void) fprintf(stdout, "ejk5 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
327 hp->a = (LRAND() / MAXRAND) * 100.0 + 550.0;
329 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
331 hp->b = (LRAND() / MAXRAND) + 0.5;
332 if (MI_IS_VERBOSE(mi))
333 (void) fprintf(stdout, "ejk6 a=%g, b=%g\n", hp->a, hp->b);
337 hp->a = (LRAND() / MAXRAND) * 100.0;
338 hp->b = (LRAND() / MAXRAND) * 20.0;
339 hp->c = (LRAND() / MAXRAND) * 200.0;
341 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 40.0;
342 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
343 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
345 hp->d = (LRAND() / MAXRAND) * 0.9;
346 if (MI_IS_VERBOSE(mi))
347 (void) fprintf(stdout, "rr a=%g, b=%g, c=%g, d=%g\n",
348 hp->a, hp->b, hp->c, hp->d);
353 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.24 + 0.25;
355 if (MI_IS_VERBOSE(mi))
356 (void) fprintf(stdout, "popcorn a=%g, b=%g, c=%g, d=%g\n",
357 hp->a, hp->b, hp->c, hp->d);
360 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
361 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
362 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
363 hp->d = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
364 if (MI_IS_VERBOSE(mi))
365 (void) fprintf(stdout, "jong a=%g, b=%g, c=%g, d=%g\n",
366 hp->a, hp->b, hp->c, hp->d);
368 case SINE: /* MARTIN2 */
370 hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.07;
372 hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.7;
374 if (MI_IS_VERBOSE(mi))
375 (void) fprintf(stdout, "sine a=%g\n", hp->a);
378 if (MI_NPIXELS(mi) > 2)
379 hp->pix = NRAND(MI_NPIXELS(mi));
380 hp->bufsize = MI_COUNT(mi);
382 if (!hp->pointBuffer)
383 hp->pointBuffer = (XPoint *) malloc(hp->bufsize * sizeof (XPoint));
387 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
393 draw_hop(ModeInfo * mi)
395 hopstruct *hp = &hops[MI_SCREEN(mi)];
397 XPoint *xp = hp->pointBuffer;
400 MI_IS_DRAWN(mi) = True;
403 if (MI_NPIXELS(mi) > 2) {
404 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_PIXEL(mi, hp->pix));
405 if (++hp->pix >= MI_NPIXELS(mi))
411 case MARTIN: /* SQRT, MARTIN1 */
412 oldi = hp->i + hp->inc;
413 hp->j = hp->a - hp->i;
414 hp->i = oldj + ((hp->i < 0)
415 ? sqrt(fabs(hp->b * oldi - hp->c))
416 : -sqrt(fabs(hp->b * oldi - hp->c)));
417 xp->x = hp->centerx + (int) (hp->i + hp->j);
418 xp->y = hp->centery - (int) (hp->i - hp->j);
421 oldi = hp->i + hp->inc;
422 hp->j = hp->a - hp->i;
423 hp->i = oldj - ((hp->i > 0) ? (hp->b * oldi - hp->c) :
424 -(hp->b * oldi - hp->c));
425 xp->x = hp->centerx + (int) (hp->i + hp->j);
426 xp->y = hp->centery - (int) (hp->i - hp->j);
429 oldi = hp->i + hp->inc;
430 hp->j = hp->a - hp->i;
431 hp->i = oldj - ((hp->i < 0) ? log(fabs(hp->b * oldi - hp->c)) :
432 -log(fabs(hp->b * oldi - hp->c)));
433 xp->x = hp->centerx + (int) (hp->i + hp->j);
434 xp->y = hp->centery - (int) (hp->i - hp->j);
437 oldi = hp->i + hp->inc;
438 hp->j = hp->a - hp->i;
439 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
440 -sin(hp->b * oldi) - hp->c);
441 xp->x = hp->centerx + (int) (hp->i + hp->j);
442 xp->y = hp->centery - (int) (hp->i - hp->j);
445 oldi = hp->i + hp->inc;
446 hp->j = hp->a - hp->i;
447 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
448 -sqrt(fabs(hp->b * oldi - hp->c)));
449 xp->x = hp->centerx + (int) (hp->i + hp->j);
450 xp->y = hp->centery - (int) (hp->i - hp->j);
453 oldi = hp->i + hp->inc;
454 hp->j = hp->a - hp->i;
455 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
456 -(hp->b * oldi - hp->c));
457 xp->x = hp->centerx + (int) (hp->i + hp->j);
458 xp->y = hp->centery - (int) (hp->i - hp->j);
461 oldi = hp->i + hp->inc;
462 hp->j = hp->a - hp->i;
463 hp->i = oldj - asin((hp->b * oldi) - (long) (hp->b * oldi));
464 xp->x = hp->centerx + (int) (hp->i + hp->j);
465 xp->y = hp->centery - (int) (hp->i - hp->j);
468 oldi = hp->i + hp->inc;
469 hp->j = hp->a - hp->i;
470 hp->i = oldj - ((hp->i < 0) ? -pow(fabs(hp->b * oldi - hp->c), hp->d) :
471 pow(fabs(hp->b * oldi - hp->c), hp->d));
472 xp->x = hp->centerx + (int) (hp->i + hp->j);
473 xp->y = hp->centery - (int) (hp->i - hp->j);
484 if (hp->a++ >= INCVAL) {
486 if (hp->b++ >= INCVAL)
489 hp->i = (-hp->c * INCVAL / 2 + hp->c * hp->a) * M_PI / 180.0;
490 hp->j = (-hp->c * INCVAL / 2 + hp->c * hp->b) * M_PI / 180.0;
492 tempi = hp->i - HVAL * sin(hp->j + tan(3.0 * hp->j));
493 tempj = hp->j - HVAL * sin(hp->i + tan(3.0 * hp->i));
494 xp->x = hp->centerx + (int) (MI_WIDTH(mi) / 40 * tempi);
495 xp->y = hp->centery + (int) (MI_HEIGHT(mi) / 40 * tempj);
502 oldi = hp->i + 4 * hp->inc / hp->centerx;
505 hp->j = sin(hp->c * hp->i) - cos(hp->d * hp->j);
506 hp->i = sin(hp->a * oldj) - cos(hp->b * oldi);
507 xp->x = hp->centerx + (int) (hp->centerx * (hp->i + hp->j) / 4.0);
508 xp->y = hp->centery - (int) (hp->centery * (hp->i - hp->j) / 4.0);
510 case SINE: /* MARTIN2 */
511 oldi = hp->i + hp->inc;
512 hp->j = hp->a - hp->i;
513 hp->i = oldj - sin(oldi);
514 xp->x = hp->centerx + (int) (hp->i + hp->j);
515 xp->y = hp->centery - (int) (hp->i - hp->j);
520 XDrawPoints(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
521 hp->pointBuffer, hp->bufsize, CoordModeOrigin);
522 if (++hp->count > MI_CYCLES(mi)) {
524 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
525 #endif /* STANDALONE */
531 release_hop(ModeInfo * mi)
536 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
537 hopstruct *hp = &hops[screen];
540 (void) free((void *) hp->pointBuffer);
543 (void) free((void *) hops);
549 refresh_hop(ModeInfo * mi)