1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* hop --- real plane fractals */
5 static const char sccsid[] = "@(#)hop.c 5.00 2000/11/01 xlockmore";
9 * Copyright (c) 1991 by Patrick J. Naughton.
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
24 * Changes in xlockmore distribution
25 * 01-Nov-2000: Allocation checks
26 * 24-Jun-1997: EJK and RR functions stolen from xmartin2.2
27 * Ed Kubaitis <ejk@ux2.cso.uiuc.edu> ejk functions and xmartin
28 * Renaldo Recuerdo rr function, generalized exponent version
29 * of the Barry Martin's square root function
30 * 10-May-1997: Compatible with xscreensaver
31 * 27-Jul-1995: 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-1995: changed name from hopalong to hop
35 * 09-Dec-1994: added Barry Martin's sine hop
36 * Changes in original xlock
37 * 29-Oct-1990: fix bad (int) cast.
38 * 29-Jul-1990: support for multiple screens.
39 * 08-Jul-1990: new timing and colors and new algorithm for fractals.
40 * 15-Dec-1989: Fix for proper skipping of {White,Black}Pixel() in colors.
41 * 08-Oct-1989: Fixed long standing typo bug in RandomInitHop();
42 * Fixed bug in memory allocation in init_hop();
43 * Moved seconds() to an extern.
44 * Got rid of the % mod since .mod is slow on a sparc.
46 * 31-Aug-1988: Forked from xlock.c for modularity.
47 * 23-Mar-1988: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
48 * Hopalong was attributed to Barry Martin of Aston University
49 * (Birmingham, England)
55 #define DEFAULTS "*delay: 10000 \n" \
59 "*fpsSolid: true \n" \
60 "*ignoreRotation: True \n" \
62 # define SMOOTH_COLORS
63 # define reshape_hop 0
64 # define hop_handle_event 0
65 # include "xlockmore.h" /* in xscreensaver distribution */
67 #else /* STANDALONE */
68 # include "xlock.h" /* in xlockmore distribution */
69 #endif /* STANDALONE */
73 #define DEF_MARTIN "False"
74 #define DEF_POPCORN "False"
75 #define DEF_EJK1 "False"
76 #define DEF_EJK2 "False"
77 #define DEF_EJK3 "False"
78 #define DEF_EJK4 "False"
79 #define DEF_EJK5 "False"
80 #define DEF_EJK6 "False"
81 #define DEF_RR "False"
82 #define DEF_JONG "False"
83 #define DEF_SINE "False"
97 static XrmOptionDescRec opts[] =
99 {"-martin", ".hop.martin", XrmoptionNoArg, "on"},
100 {"+martin", ".hop.martin", XrmoptionNoArg, "off"},
101 {"-popcorn", ".hop.popcorn", XrmoptionNoArg, "on"},
102 {"+popcorn", ".hop.popcorn", XrmoptionNoArg, "off"},
103 {"-ejk1", ".hop.ejk1", XrmoptionNoArg, "on"},
104 {"+ejk1", ".hop.ejk1", XrmoptionNoArg, "off"},
105 {"-ejk2", ".hop.ejk2", XrmoptionNoArg, "on"},
106 {"+ejk2", ".hop.ejk2", XrmoptionNoArg, "off"},
107 {"-ejk3", ".hop.ejk3", XrmoptionNoArg, "on"},
108 {"+ejk3", ".hop.ejk3", XrmoptionNoArg, "off"},
109 {"-ejk4", ".hop.ejk4", XrmoptionNoArg, "on"},
110 {"+ejk4", ".hop.ejk4", XrmoptionNoArg, "off"},
111 {"-ejk5", ".hop.ejk5", XrmoptionNoArg, "on"},
112 {"+ejk5", ".hop.ejk5", XrmoptionNoArg, "off"},
113 {"-ejk6", ".hop.ejk6", XrmoptionNoArg, "on"},
114 {"+ejk6", ".hop.ejk6", XrmoptionNoArg, "off"},
115 {"-rr", ".hop.rr", XrmoptionNoArg, "on"},
116 {"+rr", ".hop.rr", XrmoptionNoArg, "off"},
117 {"-jong", ".hop.jong", XrmoptionNoArg, "on"},
118 {"+jong", ".hop.jong", XrmoptionNoArg, "off"},
119 {"-sine", ".hop.sine", XrmoptionNoArg, "on"},
120 {"+sine", ".hop.sine", XrmoptionNoArg, "off"}
122 static argtype vars[] =
124 {&martin, "martin", "Martin", DEF_MARTIN, t_Bool},
125 {&popcorn, "popcorn", "Popcorn", DEF_POPCORN, t_Bool},
126 {&ejk1, "ejk1", "EJK1", DEF_EJK1, t_Bool},
127 {&ejk2, "ejk2", "EJK2", DEF_EJK2, t_Bool},
128 {&ejk3, "ejk3", "EJK3", DEF_EJK3, t_Bool},
129 {&ejk4, "ejk4", "EJK4", DEF_EJK4, t_Bool},
130 {&ejk5, "ejk5", "EJK5", DEF_EJK5, t_Bool},
131 {&ejk6, "ejk6", "EJK6", DEF_EJK6, t_Bool},
132 {&rr, "rr", "RR", DEF_RR, t_Bool},
133 {&jong, "jong", "Jong", DEF_JONG, t_Bool},
134 {&sine, "sine", "Sine", DEF_SINE, t_Bool}
136 static OptionStruct desc[] =
138 {"-/+martin", "turn on/off sqrt format"},
139 {"-/+popcorn", "turn on/off Clifford A. Pickover's popcorn format"},
140 {"-/+ejk1", "turn on/off ejk1 format"},
141 {"-/+ejk2", "turn on/off ejk2 format"},
142 {"-/+ejk3", "turn on/off ejk3 format"},
143 {"-/+ejk4", "turn on/off ejk4 format"},
144 {"-/+ejk5", "turn on/off ejk5 format"},
145 {"-/+ejk6", "turn on/off ejk6 format"},
146 {"-/+rr", "turn on/off rr format"},
147 {"-/+jong", "turn on/off jong format"},
148 {"-/+sine", "turn on/off sine format"}
151 ENTRYPOINT ModeSpecOpt hop_opts =
152 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
155 ModStruct hop_description =
156 {"hop", "init_hop", "draw_hop", "release_hop",
157 "refresh_hop", "init_hop", (char *) NULL, &hop_opts,
158 10000, 1000, 2500, 1, 64, 1.0, "",
159 "Shows real plane iterated fractals", 0, NULL};
175 #define OPS 8 /* 8, 9, 10 might be too close to a swastika for some... */
181 int centerx, centery; /* center of the screen */
183 double i, j; /* hopalong parameters */
189 XPoint *pointBuffer; /* pointer for XDrawPoints */
191 eraser_state *eraser;
195 static hopstruct *hops = (hopstruct *) NULL;
198 init_hop(ModeInfo * mi)
200 Display *display = MI_DISPLAY(mi);
206 if ((hops = (hopstruct *) calloc(MI_NUM_SCREENS(mi),
207 sizeof (hopstruct))) == NULL)
210 hp = &hops[MI_SCREEN(mi)];
212 hp->centerx = MI_WIDTH(mi) / 2;
213 hp->centery = MI_HEIGHT(mi) / 2;
214 /* Make the other operations less common since they are less interesting */
215 if (MI_IS_FULLRANDOM(mi)) {
244 range = sqrt((double) hp->centerx * hp->centerx +
245 (double) hp->centery * hp->centery) / (1.0 + LRAND() / MAXRAND);
247 hp->inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
252 hp->a = (LRAND() / MAXRAND) * 1500.0 + 40.0;
253 hp->b = (LRAND() / MAXRAND) * 17.0 + 3.0;
254 hp->c = (LRAND() / MAXRAND) * 3000.0 + 100.0;
256 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
257 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
259 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
263 if (MI_IS_VERBOSE(mi))
264 (void) fprintf(stdout, "sqrt a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
268 hp->a = (LRAND() / MAXRAND) * 500.0;
269 hp->c = (LRAND() / MAXRAND) * 100.0 + 10.0;
271 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
272 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 40.0;
274 hp->b = (LRAND() / MAXRAND) * 0.4;
275 if (MI_IS_VERBOSE(mi))
276 (void) fprintf(stdout, "ejk1 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
280 hp->a = (LRAND() / MAXRAND) * 500.0;
282 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
284 hp->b = pow(10.0, 6.0 + (LRAND() / MAXRAND) * 24.0);
287 hp->c = pow(10.0, (LRAND() / MAXRAND) * 9.0);
290 if (MI_IS_VERBOSE(mi))
291 (void) fprintf(stdout, "ejk2 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
295 hp->a = (LRAND() / MAXRAND) * 500.0;
296 hp->c = (LRAND() / MAXRAND) * 80.0 + 30.0;
298 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
299 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 70.0;
301 hp->b = (LRAND() / MAXRAND) * 0.35 + 0.5;
302 if (MI_IS_VERBOSE(mi))
303 (void) fprintf(stdout, "ejk3 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
307 hp->a = (LRAND() / MAXRAND) * 1000.0;
308 hp->c = (LRAND() / MAXRAND) * 40.0 + 30.0;
310 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 2.0;
311 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
313 hp->b = (LRAND() / MAXRAND) * 9.0 + 1.0;
314 if (MI_IS_VERBOSE(mi))
315 (void) fprintf(stdout, "ejk4 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
319 hp->a = (LRAND() / MAXRAND) * 600.0;
320 hp->c = (LRAND() / MAXRAND) * 90.0 + 20.0;
322 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 2.0;
323 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
325 hp->b = (LRAND() / MAXRAND) * 0.3 + 0.1;
326 if (MI_IS_VERBOSE(mi))
327 (void) fprintf(stdout, "ejk5 a=%g, b=%g, c=%g\n", hp->a, hp->b, hp->c);
331 hp->a = (LRAND() / MAXRAND) * 100.0 + 550.0;
333 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 30.0;
335 hp->b = (LRAND() / MAXRAND) + 0.5;
336 if (MI_IS_VERBOSE(mi))
337 (void) fprintf(stdout, "ejk6 a=%g, b=%g\n", hp->a, hp->b);
341 hp->a = (LRAND() / MAXRAND) * 100.0;
342 hp->b = (LRAND() / MAXRAND) * 20.0;
343 hp->c = (LRAND() / MAXRAND) * 200.0;
345 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 40.0;
346 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 200.0;
347 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
349 hp->d = (LRAND() / MAXRAND) * 0.9;
350 if (MI_IS_VERBOSE(mi))
351 (void) fprintf(stdout, "rr a=%g, b=%g, c=%g, d=%g\n",
352 hp->a, hp->b, hp->c, hp->d);
357 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.24 + 0.25;
359 if (MI_IS_VERBOSE(mi))
360 (void) fprintf(stdout, "popcorn a=%g, b=%g, c=%g, d=%g\n",
361 hp->a, hp->b, hp->c, hp->d);
364 hp->a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
365 hp->b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
366 hp->c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
367 hp->d = ((LRAND() / MAXRAND) * 2.0 - 1.0) * M_PI;
368 if (MI_IS_VERBOSE(mi))
369 (void) fprintf(stdout, "jong a=%g, b=%g, c=%g, d=%g\n",
370 hp->a, hp->b, hp->c, hp->d);
372 case SINE: /* MARTIN2 */
374 hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.07;
376 hp->a = M_PI + ((LRAND() / MAXRAND) * 2.0 - 1.0) * 0.7;
378 if (MI_IS_VERBOSE(mi))
379 (void) fprintf(stdout, "sine a=%g\n", hp->a);
382 if (MI_NPIXELS(mi) > 2)
383 hp->pix = NRAND(MI_NPIXELS(mi));
384 hp->bufsize = MI_COUNT(mi);
386 if (hp->pointBuffer == NULL) {
387 if ((hp->pointBuffer = (XPoint *) malloc(hp->bufsize *
388 sizeof (XPoint))) == NULL)
396 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
402 draw_hop(ModeInfo * mi)
411 hp = &hops[MI_SCREEN(mi)];
415 hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
421 if (hp->pointBuffer == NULL)
423 xp = hp->pointBuffer;
426 MI_IS_DRAWN(mi) = True;
428 if (MI_NPIXELS(mi) > 2) {
429 XSetForeground(MI_DISPLAY(mi), MI_GC(mi), MI_PIXEL(mi, hp->pix));
430 if (++hp->pix >= MI_NPIXELS(mi))
436 case MARTIN: /* SQRT, MARTIN1 */
437 oldi = hp->i + hp->inc;
438 hp->j = hp->a - hp->i;
439 hp->i = oldj + ((hp->i < 0)
440 ? sqrt(fabs(hp->b * oldi - hp->c))
441 : -sqrt(fabs(hp->b * oldi - hp->c)));
442 xp->x = hp->centerx + (int) (hp->i + hp->j);
443 xp->y = hp->centery - (int) (hp->i - hp->j);
446 oldi = hp->i + hp->inc;
447 hp->j = hp->a - hp->i;
448 hp->i = oldj - ((hp->i > 0) ? (hp->b * oldi - hp->c) :
449 -(hp->b * oldi - hp->c));
450 xp->x = hp->centerx + (int) (hp->i + hp->j);
451 xp->y = hp->centery - (int) (hp->i - hp->j);
454 oldi = hp->i + hp->inc;
455 hp->j = hp->a - hp->i;
456 hp->i = oldj - ((hp->i < 0) ? log(fabs(hp->b * oldi - hp->c)) :
457 -log(fabs(hp->b * oldi - hp->c)));
458 xp->x = hp->centerx + (int) (hp->i + hp->j);
459 xp->y = hp->centery - (int) (hp->i - hp->j);
462 oldi = hp->i + hp->inc;
463 hp->j = hp->a - hp->i;
464 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
465 -sin(hp->b * oldi) - hp->c);
466 xp->x = hp->centerx + (int) (hp->i + hp->j);
467 xp->y = hp->centery - (int) (hp->i - hp->j);
470 oldi = hp->i + hp->inc;
471 hp->j = hp->a - hp->i;
472 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
473 -sqrt(fabs(hp->b * oldi - hp->c)));
474 xp->x = hp->centerx + (int) (hp->i + hp->j);
475 xp->y = hp->centery - (int) (hp->i - hp->j);
478 oldi = hp->i + hp->inc;
479 hp->j = hp->a - hp->i;
480 hp->i = oldj - ((hp->i > 0) ? sin(hp->b * oldi) - hp->c :
481 -(hp->b * oldi - hp->c));
482 xp->x = hp->centerx + (int) (hp->i + hp->j);
483 xp->y = hp->centery - (int) (hp->i - hp->j);
486 oldi = hp->i + hp->inc;
487 hp->j = hp->a - hp->i;
488 hp->i = oldj - asin((hp->b * oldi) - (long) (hp->b * oldi));
489 xp->x = hp->centerx + (int) (hp->i + hp->j);
490 xp->y = hp->centery - (int) (hp->i - hp->j);
493 oldi = hp->i + hp->inc;
494 hp->j = hp->a - hp->i;
495 hp->i = oldj - ((hp->i < 0) ? -pow(fabs(hp->b * oldi - hp->c), hp->d) :
496 pow(fabs(hp->b * oldi - hp->c), hp->d));
497 xp->x = hp->centerx + (int) (hp->i + hp->j);
498 xp->y = hp->centery - (int) (hp->i - hp->j);
509 if (hp->a++ >= INCVAL) {
511 if (hp->b++ >= INCVAL)
514 hp->i = (-hp->c * INCVAL / 2 + hp->c * hp->a) * M_PI / 180.0;
515 hp->j = (-hp->c * INCVAL / 2 + hp->c * hp->b) * M_PI / 180.0;
517 tempi = hp->i - HVAL * sin(hp->j + tan(3.0 * hp->j));
518 tempj = hp->j - HVAL * sin(hp->i + tan(3.0 * hp->i));
519 xp->x = hp->centerx + (int) (MI_WIDTH(mi) / 40 * tempi);
520 xp->y = hp->centery + (int) (MI_HEIGHT(mi) / 40 * tempj);
527 oldi = hp->i + 4 * hp->inc / hp->centerx;
530 hp->j = sin(hp->c * hp->i) - cos(hp->d * hp->j);
531 hp->i = sin(hp->a * oldj) - cos(hp->b * oldi);
532 xp->x = hp->centerx + (int) (hp->centerx * (hp->i + hp->j) / 4.0);
533 xp->y = hp->centery - (int) (hp->centery * (hp->i - hp->j) / 4.0);
535 case SINE: /* MARTIN2 */
536 oldi = hp->i + hp->inc;
537 hp->j = hp->a - hp->i;
538 hp->i = oldj - sin(oldi);
539 xp->x = hp->centerx + (int) (hp->i + hp->j);
540 xp->y = hp->centery - (int) (hp->i - hp->j);
545 XDrawPoints(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
546 hp->pointBuffer, hp->bufsize, CoordModeOrigin);
547 if (++hp->count > MI_CYCLES(mi)) {
549 hp->eraser = erase_window (MI_DISPLAY(mi), MI_WINDOW(mi), hp->eraser);
550 #endif /* STANDALONE */
556 release_hop(ModeInfo * mi)
561 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
562 hopstruct *hp = &hops[screen];
564 if (hp->pointBuffer != NULL)
565 (void) free((void *) hp->pointBuffer);
567 (void) free((void *) hops);
568 hops = (hopstruct *) NULL;
573 refresh_hop(ModeInfo * mi)
578 XSCREENSAVER_MODULE_2 ("Hopalong", hopalong, hop)
580 #endif /* MODE_hop */