1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* hop --- real plane fractals */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)hop.c 5.00 2000/11/01 xlockmore";
10 * Copyright (c) 1991 by Patrick J. Naughton.
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
25 * Changes in xlockmore distribution
26 * 01-Nov-2000: Allocation checks
27 * 24-Jun-1997: EJK and RR functions stolen from xmartin2.2
28 * Ed Kubaitis <ejk@ux2.cso.uiuc.edu> ejk functions and xmartin
29 * Renaldo Recuerdo rr function, generalized exponent version
30 * of the Barry Martin's square root function
31 * 10-May-1997: Compatible with xscreensaver
32 * 27-Jul-1995: added Peter de Jong's hop from Scientific American
33 * July 87 p. 111. Sometimes they are amazing but there are a
34 * few duds (I did not see a pattern in the parameters).
35 * 29-Mar-1995: changed name from hopalong to hop
36 * 09-Dec-1994: added Barry Martin's sine hop
37 * Changes in original xlock
38 * 29-Oct-1990: fix bad (int) cast.
39 * 29-Jul-1990: support for multiple screens.
40 * 08-Jul-1990: new timing and colors and new algorithm for fractals.
41 * 15-Dec-1989: Fix for proper skipping of {White,Black}Pixel() in colors.
42 * 08-Oct-1989: Fixed long standing typo bug in RandomInitHop();
43 * Fixed bug in memory allocation in init_hop();
44 * Moved seconds() to an extern.
45 * Got rid of the % mod since .mod is slow on a sparc.
47 * 31-Aug-1988: Forked from xlock.c for modularity.
48 * 23-Mar-1988: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
49 * Hopalong was attributed to Barry Martin of Aston University
50 * (Birmingham, England)
56 #define PROGCLASS "Hop"
57 #define HACK_INIT init_hop
58 #define HACK_DRAW draw_hop
59 #define hop_opts xlockmore_opts
60 #define DEFAULTS "*delay: 10000 \n" \
65 #include "xlockmore.h" /* in xscreensaver distribution */
67 #else /* STANDALONE */
68 #include "xlock.h" /* in xlockmore distribution */
70 #endif /* STANDALONE */
74 #define DEF_MARTIN "False"
75 #define DEF_POPCORN "False"
76 #define DEF_EJK1 "False"
77 #define DEF_EJK2 "False"
78 #define DEF_EJK3 "False"
79 #define DEF_EJK4 "False"
80 #define DEF_EJK5 "False"
81 #define DEF_EJK6 "False"
82 #define DEF_RR "False"
83 #define DEF_JONG "False"
84 #define DEF_SINE "False"
98 static XrmOptionDescRec opts[] =
100 {(char *) "-martin", (char *) ".hop.martin", XrmoptionNoArg, (caddr_t) "on"},
101 {(char *) "+martin", (char *) ".hop.martin", XrmoptionNoArg, (caddr_t) "off"},
102 {(char *) "-popcorn", (char *) ".hop.popcorn", XrmoptionNoArg, (caddr_t) "on"},
103 {(char *) "+popcorn", (char *) ".hop.popcorn", XrmoptionNoArg, (caddr_t) "off"},
104 {(char *) "-ejk1", (char *) ".hop.ejk1", XrmoptionNoArg, (caddr_t) "on"},
105 {(char *) "+ejk1", (char *) ".hop.ejk1", XrmoptionNoArg, (caddr_t) "off"},
106 {(char *) "-ejk2", (char *) ".hop.ejk2", XrmoptionNoArg, (caddr_t) "on"},
107 {(char *) "+ejk2", (char *) ".hop.ejk2", XrmoptionNoArg, (caddr_t) "off"},
108 {(char *) "-ejk3", (char *) ".hop.ejk3", XrmoptionNoArg, (caddr_t) "on"},
109 {(char *) "+ejk3", (char *) ".hop.ejk3", XrmoptionNoArg, (caddr_t) "off"},
110 {(char *) "-ejk4", (char *) ".hop.ejk4", XrmoptionNoArg, (caddr_t) "on"},
111 {(char *) "+ejk4", (char *) ".hop.ejk4", XrmoptionNoArg, (caddr_t) "off"},
112 {(char *) "-ejk5", (char *) ".hop.ejk5", XrmoptionNoArg, (caddr_t) "on"},
113 {(char *) "+ejk5", (char *) ".hop.ejk5", XrmoptionNoArg, (caddr_t) "off"},
114 {(char *) "-ejk6", (char *) ".hop.ejk6", XrmoptionNoArg, (caddr_t) "on"},
115 {(char *) "+ejk6", (char *) ".hop.ejk6", XrmoptionNoArg, (caddr_t) "off"},
116 {(char *) "-rr", (char *) ".hop.rr", XrmoptionNoArg, (caddr_t) "on"},
117 {(char *) "+rr", (char *) ".hop.rr", XrmoptionNoArg, (caddr_t) "off"},
118 {(char *) "-jong", (char *) ".hop.jong", XrmoptionNoArg, (caddr_t) "on"},
119 {(char *) "+jong", (char *) ".hop.jong", XrmoptionNoArg, (caddr_t) "off"},
120 {(char *) "-sine", (char *) ".hop.sine", XrmoptionNoArg, (caddr_t) "on"},
121 {(char *) "+sine", (char *) ".hop.sine", XrmoptionNoArg, (caddr_t) "off"}
123 static argtype vars[] =
125 {(caddr_t *) & martin, (char *) "martin", (char *) "Martin", (char *) DEF_MARTIN, t_Bool},
126 {(caddr_t *) & popcorn, (char *) "popcorn", (char *) "Popcorn", (char *) DEF_POPCORN, t_Bool},
127 {(caddr_t *) & ejk1, (char *) "ejk1", (char *) "EJK1", (char *) DEF_EJK1, t_Bool},
128 {(caddr_t *) & ejk2, (char *) "ejk2", (char *) "EJK2", (char *) DEF_EJK2, t_Bool},
129 {(caddr_t *) & ejk3, (char *) "ejk3", (char *) "EJK3", (char *) DEF_EJK3, t_Bool},
130 {(caddr_t *) & ejk4, (char *) "ejk4", (char *) "EJK4", (char *) DEF_EJK4, t_Bool},
131 {(caddr_t *) & ejk5, (char *) "ejk5", (char *) "EJK5", (char *) DEF_EJK5, t_Bool},
132 {(caddr_t *) & ejk6, (char *) "ejk6", (char *) "EJK6", (char *) DEF_EJK6, t_Bool},
133 {(caddr_t *) & rr, (char *) "rr", (char *) "RR", (char *) DEF_RR, t_Bool},
134 {(caddr_t *) & jong, (char *) "jong", (char *) "Jong", (char *) DEF_JONG, t_Bool},
135 {(caddr_t *) & sine, (char *) "sine", (char *) "Sine", (char *) DEF_SINE, t_Bool}
137 static OptionStruct desc[] =
139 {(char *) "-/+martin", (char *) "turn on/off sqrt format"},
140 {(char *) "-/+popcorn", (char *) "turn on/off Clifford A. Pickover's popcorn format"},
141 {(char *) "-/+ejk1", (char *) "turn on/off ejk1 format"},
142 {(char *) "-/+ejk2", (char *) "turn on/off ejk2 format"},
143 {(char *) "-/+ejk3", (char *) "turn on/off ejk3 format"},
144 {(char *) "-/+ejk4", (char *) "turn on/off ejk4 format"},
145 {(char *) "-/+ejk5", (char *) "turn on/off ejk5 format"},
146 {(char *) "-/+ejk6", (char *) "turn on/off ejk6 format"},
147 {(char *) "-/+rr", (char *) "turn on/off rr format"},
148 {(char *) "-/+jong", (char *) "turn on/off jong format"},
149 {(char *) "-/+sine", (char *) "turn on/off sine format"}
152 ModeSpecOpt hop_opts =
153 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
156 ModStruct hop_description =
157 {"hop", "init_hop", "draw_hop", "release_hop",
158 "refresh_hop", "init_hop", (char *) NULL, &hop_opts,
159 10000, 1000, 2500, 1, 64, 1.0, "",
160 "Shows real plane iterated fractals", 0, NULL};
176 #define OPS 8 /* 8, 9, 10 might be too close to a swastika for some... */
182 int centerx, centery; /* center of the screen */
184 double i, j; /* hopalong parameters */
190 XPoint *pointBuffer; /* pointer for XDrawPoints */
193 static hopstruct *hops = (hopstruct *) NULL;
196 init_hop(ModeInfo * mi)
198 Display *display = MI_DISPLAY(mi);
204 if ((hops = (hopstruct *) calloc(MI_NUM_SCREENS(mi),
205 sizeof (hopstruct))) == NULL)
208 hp = &hops[MI_SCREEN(mi)];
210 hp->centerx = MI_WIDTH(mi) / 2;
211 hp->centery = MI_HEIGHT(mi) / 2;
212 /* Make the other operations less common since they are less interesting */
213 if (MI_IS_FULLRANDOM(mi)) {
242 range = sqrt((double) hp->centerx * hp->centerx +
243 (double) hp->centery * hp->centery) / (1.0 + LRAND() / MAXRAND);
245 hp->inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
250 hp->a = (LRAND() / MAXRAND) * 1500.0 + 40.0;
251 hp->b = (LRAND() / MAXRAND) * 17.0 + 3.0;
252 hp->c = (LRAND() / MAXRAND) * 3000.0 + 100.0;
254 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
255 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
257 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
261 if (MI_IS_VERBOSE(mi))
262 (void) fprintf(stdout, "sqrt a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
266 hp->a = (LRAND() / MAXRAND) * 500.0;
267 hp->c = (LRAND() / MAXRAND) * 100.0 + 10.0;
269 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
270 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 40.0;
272 hp->b = (LRAND() / MAXRAND) * 0.4;
273 if (MI_IS_VERBOSE(mi))
274 (void) fprintf(stdout, "ejk1 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
278 hp->a = (LRAND() / MAXRAND) * 500.0;
280 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
282 hp->b = pow(10.0, 6.0 + (LRAND() / MAXRAND) * 24.0);
285 hp->c = pow(10.0, (LRAND() / MAXRAND) * 9.0);
288 if (MI_IS_VERBOSE(mi))
289 (void) fprintf(stdout, "ejk2 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
293 hp->a = (LRAND() / MAXRAND) * 500.0;
294 hp->c = (LRAND() / MAXRAND) * 80.0 + 30.0;
296 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
297 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 70.0;
299 hp->b = (LRAND() / MAXRAND) * 0.35 + 0.5;
300 if (MI_IS_VERBOSE(mi))
301 (void) fprintf(stdout, "ejk3 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
305 hp->a = (LRAND() / MAXRAND) * 1000.0;
306 hp->c = (LRAND() / MAXRAND) * 40.0 + 30.0;
308 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 2.0;
309 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
311 hp->b = (LRAND() / MAXRAND) * 9.0 + 1.0;
312 if (MI_IS_VERBOSE(mi))
313 (void) fprintf(stdout, "ejk4 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
317 hp->a = (LRAND() / MAXRAND) * 600.0;
318 hp->c = (LRAND() / MAXRAND) * 90.0 + 20.0;
320 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 2.0;
321 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
323 hp->b = (LRAND() / MAXRAND) * 0.3 + 0.1;
324 if (MI_IS_VERBOSE(mi))
325 (void) fprintf(stdout, "ejk5 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
329 hp->a = (LRAND() / MAXRAND) * 100.0 + 550.0;
331 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
333 hp->b = (LRAND() / MAXRAND) + 0.5;
334 if (MI_IS_VERBOSE(mi))
335 (void) fprintf(stdout, "ejk6 a=%g, b=%g\n", hp->a, hp->b);
339 hp->a = (LRAND() / MAXRAND) * 100.0;
340 hp->b = (LRAND() / MAXRAND) * 20.0;
341 hp->c = (LRAND() / MAXRAND) * 200.0;
343 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 40.0;
344 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
345 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
347 hp->d = (LRAND() / MAXRAND) * 0.9;
348 if (MI_IS_VERBOSE(mi))
349 (void) fprintf(stdout, "rr a=%g, b=%g, c=%g, d=%g\n",
350 hp->a, hp->b, hp->c, hp->d);
355 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.24 + 0.25;
357 if (MI_IS_VERBOSE(mi))
358 (void) fprintf(stdout, "popcorn a=%g, b=%g, c=%g, d=%g\n",
359 hp->a, hp->b, hp->c, hp->d);
362 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
363 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
364 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
365 hp->d = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
366 if (MI_IS_VERBOSE(mi))
367 (void) fprintf(stdout, "jong a=%g, b=%g, c=%g, d=%g\n",
368 hp->a, hp->b, hp->c, hp->d);
370 case SINE: /* MARTIN2 */
372 hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.07;
374 hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.7;
376 if (MI_IS_VERBOSE(mi))
377 (void) fprintf(stdout, "sine a=%g\n", hp->a);
380 if (MI_NPIXELS(mi) > 2)
381 hp->pix = NRAND(MI_NPIXELS(mi));
382 hp->bufsize = MI_COUNT(mi);
384 if (hp->pointBuffer == NULL) {
385 if ((hp->pointBuffer = (XPoint *) malloc(hp->bufsize *
386 sizeof (XPoint))) == NULL)
392 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
398 draw_hop(ModeInfo * mi)
407 hp = &hops[MI_SCREEN(mi)];
408 if (hp->pointBuffer == NULL)
410 xp = hp->pointBuffer;
413 MI_IS_DRAWN(mi) = True;
415 if (MI_NPIXELS(mi) > 2) {
416 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_PIXEL(mi, hp->pix));
417 if (++hp->pix >= MI_NPIXELS(mi))
423 case MARTIN: /* SQRT, MARTIN1 */
424 oldi = hp->i + hp->inc;
425 hp->j = hp->a - hp->i;
426 hp->i = oldj + ((hp->i < 0)
427 ? sqrt(fabs(hp->b * oldi - hp->c))
428 : -sqrt(fabs(hp->b * oldi - hp->c)));
429 xp->x = hp->centerx + (int) (hp->i + hp->j);
430 xp->y = hp->centery - (int) (hp->i - hp->j);
433 oldi = hp->i + hp->inc;
434 hp->j = hp->a - hp->i;
435 hp->i = oldj - ((hp->i > 0) ? (hp->b * oldi - hp->c) :
436 -(hp->b * oldi - hp->c));
437 xp->x = hp->centerx + (int) (hp->i + hp->j);
438 xp->y = hp->centery - (int) (hp->i - hp->j);
441 oldi = hp->i + hp->inc;
442 hp->j = hp->a - hp->i;
443 hp->i = oldj - ((hp->i < 0) ? log(fabs(hp->b * oldi - hp->c)) :
444 -log(fabs(hp->b * oldi - hp->c)));
445 xp->x = hp->centerx + (int) (hp->i + hp->j);
446 xp->y = hp->centery - (int) (hp->i - hp->j);
449 oldi = hp->i + hp->inc;
450 hp->j = hp->a - hp->i;
451 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
452 -sin(hp->b * oldi) - hp->c);
453 xp->x = hp->centerx + (int) (hp->i + hp->j);
454 xp->y = hp->centery - (int) (hp->i - hp->j);
457 oldi = hp->i + hp->inc;
458 hp->j = hp->a - hp->i;
459 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
460 -sqrt(fabs(hp->b * oldi - hp->c)));
461 xp->x = hp->centerx + (int) (hp->i + hp->j);
462 xp->y = hp->centery - (int) (hp->i - hp->j);
465 oldi = hp->i + hp->inc;
466 hp->j = hp->a - hp->i;
467 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
468 -(hp->b * oldi - hp->c));
469 xp->x = hp->centerx + (int) (hp->i + hp->j);
470 xp->y = hp->centery - (int) (hp->i - hp->j);
473 oldi = hp->i + hp->inc;
474 hp->j = hp->a - hp->i;
475 hp->i = oldj - asin((hp->b * oldi) - (long) (hp->b * oldi));
476 xp->x = hp->centerx + (int) (hp->i + hp->j);
477 xp->y = hp->centery - (int) (hp->i - hp->j);
480 oldi = hp->i + hp->inc;
481 hp->j = hp->a - hp->i;
482 hp->i = oldj - ((hp->i < 0) ? -pow(fabs(hp->b * oldi - hp->c), hp->d) :
483 pow(fabs(hp->b * oldi - hp->c), hp->d));
484 xp->x = hp->centerx + (int) (hp->i + hp->j);
485 xp->y = hp->centery - (int) (hp->i - hp->j);
496 if (hp->a++ >= INCVAL) {
498 if (hp->b++ >= INCVAL)
501 hp->i = (-hp->c * INCVAL / 2 + hp->c * hp->a) * M_PI / 180.0;
502 hp->j = (-hp->c * INCVAL / 2 + hp->c * hp->b) * M_PI / 180.0;
504 tempi = hp->i - HVAL * sin(hp->j + tan(3.0 * hp->j));
505 tempj = hp->j - HVAL * sin(hp->i + tan(3.0 * hp->i));
506 xp->x = hp->centerx + (int) (MI_WIDTH(mi) / 40 * tempi);
507 xp->y = hp->centery + (int) (MI_HEIGHT(mi) / 40 * tempj);
514 oldi = hp->i + 4 * hp->inc / hp->centerx;
517 hp->j = sin(hp->c * hp->i) - cos(hp->d * hp->j);
518 hp->i = sin(hp->a * oldj) - cos(hp->b * oldi);
519 xp->x = hp->centerx + (int) (hp->centerx * (hp->i + hp->j) / 4.0);
520 xp->y = hp->centery - (int) (hp->centery * (hp->i - hp->j) / 4.0);
522 case SINE: /* MARTIN2 */
523 oldi = hp->i + hp->inc;
524 hp->j = hp->a - hp->i;
525 hp->i = oldj - sin(oldi);
526 xp->x = hp->centerx + (int) (hp->i + hp->j);
527 xp->y = hp->centery - (int) (hp->i - hp->j);
532 XDrawPoints(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
533 hp->pointBuffer, hp->bufsize, CoordModeOrigin);
534 if (++hp->count > MI_CYCLES(mi)) {
536 erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
537 #endif /* STANDALONE */
543 release_hop(ModeInfo * mi)
548 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
549 hopstruct *hp = &hops[screen];
551 if (hp->pointBuffer != NULL)
552 (void) free((void *) hp->pointBuffer);
554 (void) free((void *) hops);
555 hops = (hopstruct *) NULL;
560 refresh_hop(ModeInfo * mi)
565 #endif /* MODE_hop */