1 /* -*- Mode: C; tab-width: 4 -*-
2 * julia --- continuously varying Julia set.
5 static const char sccsid[] = "@(#)julia.c 4.03 97/04/10 xlockmore";
8 /* Copyright (c) 1995 Sean McCullough <bankshot@mailhost.nmt.edu>.
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 * 10-Jun-06: j.grahl@ucl.ac.uk: tweaked functions for parameter of Julia set
24 * 28-May-97: jwz@jwz.org: added interactive frobbing with the mouse.
25 * 10-May-97: jwz@jwz.org: turned into a standalone program.
26 * 02-Dec-95: snagged boilerplate from hop.c
27 * used ifs {w0 = sqrt(x-c), w1 = -sqrt(x-c)} with random iteration
28 * to plot the julia set, and sinusoidially varied parameter for set
29 * and plotted parameter with a circle.
33 * One thing to note is that batchcount is the *depth* of the search tree,
34 * so the number of points computed is 2^batchcount - 1. I use 8 or 9
35 * on a dx266 and it looks okay. The sinusoidal variation of the parameter
36 * might not be as interesting as it could, but it still gives an idea of
37 * the effect of the parameter.
41 # define DEFAULTS "*count: 1000 \n" \
45 # define UNIFORM_COLORS
46 # define reshape_julia 0
47 # define julia_handle_event 0
48 # include "xlockmore.h" /* in xscreensaver distribution */
49 #else /* !STANDALONE */
50 # include "xlock.h" /* in xlockmore distribution */
51 #endif /* !STANDALONE */
56 #define DEF_MOUSE "False"
58 static XrmOptionDescRec opts[] =
60 {"-mouse", ".julia.mouse", XrmoptionNoArg, "on"},
61 {"+mouse", ".julia.mouse", XrmoptionNoArg, "off"},
63 static argtype vars[] =
65 {&track_p, "mouse", "Mouse", DEF_MOUSE, t_Bool},
67 static OptionStruct desc[] =
69 {"-/+mouse", "turn on/off mouse tracking"},
72 ENTRYPOINT ModeSpecOpt julia_opts = { 2, opts, 1, vars, desc };
75 #define numpoints ((0x2<<jp->depth)-1)
79 int centery; /* center of the screen */
81 double ci; /* julia params */
90 int redrawing, redrawpos;
96 XPoint **pointBuffer; /* pointer for XDrawPoints */
100 static juliastruct *julias = NULL;
102 /* How many segments to draw per cycle when redrawing */
106 apply(juliastruct * jp, register double xr, register double xi, int d)
110 jp->pointBuffer[jp->buffer][jp->itree].x =
111 (int) (0.5 * xr * jp->centerx + jp->centerx);
112 jp->pointBuffer[jp->buffer][jp->itree].y =
113 (int) (0.5 * xi * jp->centery + jp->centery);
120 /* Avoid atan2: DOMAIN error message */
121 if (xi == 0.0 && xr == 0.0)
124 theta = atan2(xi, xr) / 2.0;
126 /*r = pow(xi * xi + xr * xr, 0.25); */
127 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
133 apply(jp, xr, xi, d);
134 apply(jp, -xr, -xi, d);
139 incr(ModeInfo * mi, juliastruct * jp)
148 XQueryPointer(MI_DISPLAY(mi), MI_WINDOW(mi),
149 &r, &c, &rx, &ry, &cx, &cy, &m);
150 if (cx <= 0 || cy <= 0 ||
151 cx >= MI_WIN_WIDTH(mi) || cy >= MI_WIN_HEIGHT(mi))
157 jp->cr = ((double) (cx + 2 - jp->centerx)) * 2 / jp->centerx;
158 jp->ci = ((double) (cy + 2 - jp->centery)) * 2 / jp->centery;
164 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 300.0)) *
165 sin(jp->inc * M_PI / 200.0));
166 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 300.0)) *
167 cos(jp->inc * M_PI / 200.0));
169 jp->cr += 0.5 * cos(M_PI * jp->inc / 400.0);
170 jp->ci += 0.5 * sin(M_PI * jp->inc / 400.0);
172 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 290.0)) *
173 sin(jp->inc * M_PI / 210.0));
174 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 310.0)) *
175 cos(jp->inc * M_PI / 190.0));
177 jp->cr += 0.5 * cos(M_PI * jp->inc / 395.0);
178 jp->ci += 0.5 * sin(M_PI * jp->inc / 410.0);
184 init_julia(ModeInfo * mi)
186 Display *display = MI_DISPLAY(mi);
187 Window window = MI_WINDOW(mi);
192 if (julias == NULL) {
193 if ((julias = (juliastruct *) calloc(MI_NUM_SCREENS(mi),
194 sizeof (juliastruct))) == NULL)
197 jp = &julias[MI_SCREEN(mi)];
199 jp->centerx = MI_WIN_WIDTH(mi) / 2;
200 jp->centery = MI_WIN_HEIGHT(mi) / 2;
202 jp->depth = MI_BATCHCOUNT(mi);
208 if (track_p && !jp->cursor)
212 black.red = black.green = black.blue = 0;
213 black.flags = DoRed|DoGreen|DoBlue;
214 bit = XCreatePixmapFromBitmapData (display, window, "\000", 1, 1,
215 MI_WIN_BLACK_PIXEL(mi),
216 MI_WIN_BLACK_PIXEL(mi), 1);
217 jp->cursor = XCreatePixmapCursor (display, bit, bit, &black, &black,
219 XFreePixmap (display, bit);
221 #endif /* HAVE_COCOA */
223 if (jp->pixmap != None &&
224 jp->circsize != (MIN(jp->centerx, jp->centery) / 60) * 2 + 1) {
225 XFreePixmap(display, jp->pixmap);
228 if (jp->pixmap == None) {
229 GC fg_gc = None, bg_gc = None;
231 jp->circsize = (MIN(jp->centerx, jp->centery) / 96) * 2 + 1;
232 jp->pixmap = XCreatePixmap(display, window, jp->circsize, jp->circsize, 1);
234 fg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
236 bg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
237 XFillRectangle(display, jp->pixmap, bg_gc,
238 0, 0, jp->circsize, jp->circsize);
239 if (jp->circsize < 2)
240 XDrawPoint(display, jp->pixmap, fg_gc, 0, 0);
242 XFillArc(display, jp->pixmap, fg_gc,
243 0, 0, jp->circsize, jp->circsize, 0, 23040);
245 XFreeGC(display, fg_gc);
247 XFreeGC(display, bg_gc);
251 if (MI_WIN_IS_INROOT(mi))
253 else if (jp->circsize > 0)
254 XDefineCursor (display, window, jp->cursor);
256 XUndefineCursor (display, window);
257 #endif /* HAVE_COCOA */
259 if (!jp->stippledGC) {
260 gcv.foreground = MI_WIN_BLACK_PIXEL(mi);
261 gcv.background = MI_WIN_BLACK_PIXEL(mi);
262 if ((jp->stippledGC = XCreateGC(display, window,
263 GCForeground | GCBackground, &gcv)) == None)
266 if (MI_NPIXELS(mi) > 2)
267 jp->pix = NRAND(MI_NPIXELS(mi));
268 jp->inc = ((LRAND() & 1) * 2 - 1) * NRAND(200);
269 jp->nbuffers = (MI_CYCLES(mi) + 1);
270 if (!jp->pointBuffer)
271 jp->pointBuffer = (XPoint **) calloc(jp->nbuffers, sizeof (XPoint *));
272 for (i = 0; i < jp->nbuffers; ++i)
273 if (jp->pointBuffer[i])
274 (void) memset((char *) jp->pointBuffer[i], 0,
275 numpoints * sizeof (XPoint));
277 jp->pointBuffer[i] = (XPoint *) calloc(numpoints, sizeof (XPoint));
281 XClearWindow(display, window);
285 /* hack: moved here by jwz. */
286 #define ERASE_IMAGE(d,w,g,x,y,xl,yl,xs,ys) \
288 (y<yl+ys)?XFillRectangle(d,w,g,xl,yl,xs,y-yl): \
289 XFillRectangle(d,w,g,xl,yl,xs,ys); \
291 (y>yl-ys)?XFillRectangle(d,w,g,xl,y+ys,xs,yl-y): \
292 XFillRectangle(d,w,g,xl,yl,xs,ys); \
294 (x<xl+xs)?XFillRectangle(d,w,g,xl,yl,x-xl,ys): \
295 XFillRectangle(d,w,g,xl,yl,xs,ys); \
297 (x>xl-xs)?XFillRectangle(d,w,g,x+xs,yl,xl-x,ys): \
298 XFillRectangle(d,w,g,xl,yl,xs,ys)
302 draw_julia (ModeInfo * mi)
304 Display *display = MI_DISPLAY(mi);
305 Window window = MI_WINDOW(mi);
307 juliastruct *jp = &julias[MI_SCREEN(mi)];
309 register double xr = 0.0, xi = 0.0;
310 int k = 64, rnd = 0, i, j;
311 XPoint *xp = jp->pointBuffer[jp->buffer], old_circle, new_circle;
313 old_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
314 old_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
316 new_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
317 new_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
318 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
319 ERASE_IMAGE(display, window, gc, new_circle.x, new_circle.y,
320 old_circle.x, old_circle.y, jp->circsize, jp->circsize);
321 /* draw a circle at the c-parameter so you can see it's effect on the
322 structure of the julia set */
323 XSetForeground(display, jp->stippledGC, MI_WIN_WHITE_PIXEL(mi));
325 XSetTSOrigin(display, jp->stippledGC, new_circle.x, new_circle.y);
326 XSetStipple(display, jp->stippledGC, jp->pixmap);
327 XSetFillStyle(display, jp->stippledGC, FillOpaqueStippled);
328 #endif /* HAVE_COCOA */
329 XFillRectangle(display, window, jp->stippledGC, new_circle.x, new_circle.y,
330 jp->circsize, jp->circsize);
331 if (jp->erase == 1) {
332 XDrawPoints(display, window, gc,
333 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
336 if (MI_NPIXELS(mi) > 2) {
337 XSetForeground(display, gc, MI_PIXEL(mi, jp->pix));
338 if (++jp->pix >= MI_NPIXELS(mi))
341 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
344 /* save calls to LRAND by using bit shifts over and over on the same
345 int for 32 iterations, then get a new random int */
349 /* complex sqrt: x^0.5 = radius^0.5*(cos(theta/2) + i*sin(theta/2)) */
354 /* Avoid atan2: DOMAIN error message */
355 if (xi == 0.0 && xr == 0.0)
358 theta = atan2(xi, xr) / 2.0;
360 /*r = pow(xi * xi + xr * xr, 0.25); */
361 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
366 if ((rnd >> (k % 32)) & 0x1) {
370 xp->x = jp->centerx + (int) ((jp->centerx >> 1) * xr);
371 xp->y = jp->centery + (int) ((jp->centery >> 1) * xi);
376 apply(jp, xr, xi, jp->depth);
378 XDrawPoints(display, window, gc,
379 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
382 if (jp->buffer > jp->nbuffers - 1) {
383 jp->buffer -= jp->nbuffers;
387 for (i = 0; i < REDRAWSTEP; i++) {
388 j = (jp->buffer - jp->redrawpos + jp->nbuffers) % jp->nbuffers;
389 XDrawPoints(display, window, gc,
390 jp->pointBuffer[j], numpoints, CoordModeOrigin);
392 if (++(jp->redrawpos) >= jp->nbuffers) {
401 release_julia (ModeInfo * mi)
403 if (julias != NULL) {
406 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
407 Display *display = MI_DISPLAY(mi);
408 juliastruct *jp = &julias[screen];
411 if (jp->pointBuffer) {
412 for (buffer = 0; buffer < jp->nbuffers; buffer++)
413 if (jp->pointBuffer[buffer])
414 (void) free((void *) jp->pointBuffer[buffer]);
415 (void) free((void *) jp->pointBuffer);
417 if (jp->stippledGC != None)
418 XFreeGC(display, jp->stippledGC);
419 if (jp->pixmap != None)
420 XFreePixmap(display, jp->pixmap);
423 XFreeCursor (display, jp->cursor);
426 (void) free((void *) julias);
432 refresh_julia (ModeInfo * mi)
434 juliastruct *jp = &julias[MI_SCREEN(mi)];
440 XSCREENSAVER_MODULE ("Julia", julia)