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 * 28-May-97: jwz@jwz.org: added interactive frobbing with the mouse.
24 * 10-May-97: jwz@jwz.org: turned into a standalone program.
25 * 02-Dec-95: snagged boilerplate from hop.c
26 * used ifs {w0 = sqrt(x-c), w1 = -sqrt(x-c)} with random iteration
27 * to plot the julia set, and sinusoidially varied parameter for set
28 * and plotted parameter with a circle.
32 * One thing to note is that batchcount is the *depth* of the search tree,
33 * so the number of points computed is 2^batchcount - 1. I use 8 or 9
34 * on a dx266 and it looks okay. The sinusoidal variation of the parameter
35 * might not be as interesting as it could, but it still gives an idea of
36 * the effect of the parameter.
40 # define DEFAULTS "*count: 1000 \n" \
44 # define UNIFORM_COLORS
45 # define reshape_julia 0
46 # define julia_handle_event 0
47 # include "xlockmore.h" /* in xscreensaver distribution */
48 #else /* !STANDALONE */
49 # include "xlock.h" /* in xlockmore distribution */
50 #endif /* !STANDALONE */
55 #define DEF_MOUSE "False"
57 static XrmOptionDescRec opts[] =
59 {"-mouse", ".julia.mouse", XrmoptionNoArg, "on"},
60 {"+mouse", ".julia.mouse", XrmoptionNoArg, "off"},
62 static argtype vars[] =
64 {&track_p, "mouse", "Mouse", DEF_MOUSE, t_Bool},
66 static OptionStruct desc[] =
68 {"-/+mouse", "turn on/off mouse tracking"},
71 ENTRYPOINT ModeSpecOpt julia_opts = { 2, opts, 1, vars, desc };
74 #define numpoints ((0x2<<jp->depth)-1)
78 int centery; /* center of the screen */
80 double ci; /* julia params */
89 int redrawing, redrawpos;
95 XPoint **pointBuffer; /* pointer for XDrawPoints */
99 static juliastruct *julias = NULL;
101 /* How many segments to draw per cycle when redrawing */
105 apply(juliastruct * jp, register double xr, register double xi, int d)
109 jp->pointBuffer[jp->buffer][jp->itree].x =
110 (int) (0.5 * xr * jp->centerx + jp->centerx);
111 jp->pointBuffer[jp->buffer][jp->itree].y =
112 (int) (0.5 * xi * jp->centery + jp->centery);
119 /* Avoid atan2: DOMAIN error message */
120 if (xi == 0.0 && xr == 0.0)
123 theta = atan2(xi, xr) / 2.0;
125 /*r = pow(xi * xi + xr * xr, 0.25); */
126 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
132 apply(jp, xr, xi, d);
133 apply(jp, -xr, -xi, d);
138 incr(ModeInfo * mi, juliastruct * jp)
147 XQueryPointer(MI_DISPLAY(mi), MI_WINDOW(mi),
148 &r, &c, &rx, &ry, &cx, &cy, &m);
149 if (cx <= 0 || cy <= 0 ||
150 cx >= MI_WIN_WIDTH(mi) || cy >= MI_WIN_HEIGHT(mi))
156 jp->cr = ((double) (cx + 2 - jp->centerx)) * 2 / jp->centerx;
157 jp->ci = ((double) (cy + 2 - jp->centery)) * 2 / jp->centery;
162 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 300.0)) *
163 sin(jp->inc * M_PI / 200.0));
164 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 300.0)) *
165 cos(jp->inc * M_PI / 200.0));
167 jp->cr += 0.5 * cos(M_PI * jp->inc / 400.0);
168 jp->ci += 0.5 * sin(M_PI * jp->inc / 400.0);
173 init_julia(ModeInfo * mi)
175 Display *display = MI_DISPLAY(mi);
176 Window window = MI_WINDOW(mi);
181 if (julias == NULL) {
182 if ((julias = (juliastruct *) calloc(MI_NUM_SCREENS(mi),
183 sizeof (juliastruct))) == NULL)
186 jp = &julias[MI_SCREEN(mi)];
188 jp->centerx = MI_WIN_WIDTH(mi) / 2;
189 jp->centery = MI_WIN_HEIGHT(mi) / 2;
191 jp->depth = MI_BATCHCOUNT(mi);
197 if (track_p && !jp->cursor)
201 black.red = black.green = black.blue = 0;
202 black.flags = DoRed|DoGreen|DoBlue;
203 bit = XCreatePixmapFromBitmapData (display, window, "\000", 1, 1,
204 MI_WIN_BLACK_PIXEL(mi),
205 MI_WIN_BLACK_PIXEL(mi), 1);
206 jp->cursor = XCreatePixmapCursor (display, bit, bit, &black, &black,
208 XFreePixmap (display, bit);
210 #endif /* HAVE_COCOA */
212 if (jp->pixmap != None &&
213 jp->circsize != (MIN(jp->centerx, jp->centery) / 60) * 2 + 1) {
214 XFreePixmap(display, jp->pixmap);
217 if (jp->pixmap == None) {
218 GC fg_gc = None, bg_gc = None;
220 jp->circsize = (MIN(jp->centerx, jp->centery) / 96) * 2 + 1;
221 jp->pixmap = XCreatePixmap(display, window, jp->circsize, jp->circsize, 1);
223 fg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
225 bg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
226 XFillRectangle(display, jp->pixmap, bg_gc,
227 0, 0, jp->circsize, jp->circsize);
228 if (jp->circsize < 2)
229 XDrawPoint(display, jp->pixmap, fg_gc, 0, 0);
231 XFillArc(display, jp->pixmap, fg_gc,
232 0, 0, jp->circsize, jp->circsize, 0, 23040);
234 XFreeGC(display, fg_gc);
236 XFreeGC(display, bg_gc);
240 if (MI_WIN_IS_INROOT(mi))
242 else if (jp->circsize > 0)
243 XDefineCursor (display, window, jp->cursor);
245 XUndefineCursor (display, window);
246 #endif /* HAVE_COCOA */
248 if (!jp->stippledGC) {
249 gcv.foreground = MI_WIN_BLACK_PIXEL(mi);
250 gcv.background = MI_WIN_BLACK_PIXEL(mi);
251 if ((jp->stippledGC = XCreateGC(display, window,
252 GCForeground | GCBackground, &gcv)) == None)
255 if (MI_NPIXELS(mi) > 2)
256 jp->pix = NRAND(MI_NPIXELS(mi));
257 jp->inc = ((LRAND() & 1) * 2 - 1) * NRAND(200);
258 jp->nbuffers = (MI_CYCLES(mi) + 1);
259 if (!jp->pointBuffer)
260 jp->pointBuffer = (XPoint **) calloc(jp->nbuffers, sizeof (XPoint *));
261 for (i = 0; i < jp->nbuffers; ++i)
262 if (jp->pointBuffer[i])
263 (void) memset((char *) jp->pointBuffer[i], 0,
264 numpoints * sizeof (XPoint));
266 jp->pointBuffer[i] = (XPoint *) calloc(numpoints, sizeof (XPoint));
270 XClearWindow(display, window);
274 /* hack: moved here by jwz. */
275 #define ERASE_IMAGE(d,w,g,x,y,xl,yl,xs,ys) \
277 (y<yl+ys)?XFillRectangle(d,w,g,xl,yl,xs,y-yl): \
278 XFillRectangle(d,w,g,xl,yl,xs,ys); \
280 (y>yl-ys)?XFillRectangle(d,w,g,xl,y+ys,xs,yl-y): \
281 XFillRectangle(d,w,g,xl,yl,xs,ys); \
283 (x<xl+xs)?XFillRectangle(d,w,g,xl,yl,x-xl,ys): \
284 XFillRectangle(d,w,g,xl,yl,xs,ys); \
286 (x>xl-xs)?XFillRectangle(d,w,g,x+xs,yl,xl-x,ys): \
287 XFillRectangle(d,w,g,xl,yl,xs,ys)
291 draw_julia (ModeInfo * mi)
293 Display *display = MI_DISPLAY(mi);
294 Window window = MI_WINDOW(mi);
296 juliastruct *jp = &julias[MI_SCREEN(mi)];
298 register double xr = 0.0, xi = 0.0;
299 int k = 64, rnd = 0, i, j;
300 XPoint *xp = jp->pointBuffer[jp->buffer], old_circle, new_circle;
302 old_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
303 old_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
305 new_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
306 new_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
307 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
308 ERASE_IMAGE(display, window, gc, new_circle.x, new_circle.y,
309 old_circle.x, old_circle.y, jp->circsize, jp->circsize);
310 /* draw a circle at the c-parameter so you can see it's effect on the
311 structure of the julia set */
312 XSetForeground(display, jp->stippledGC, MI_WIN_WHITE_PIXEL(mi));
314 XSetTSOrigin(display, jp->stippledGC, new_circle.x, new_circle.y);
315 XSetStipple(display, jp->stippledGC, jp->pixmap);
316 XSetFillStyle(display, jp->stippledGC, FillOpaqueStippled);
317 #endif /* HAVE_COCOA */
318 XFillRectangle(display, window, jp->stippledGC, new_circle.x, new_circle.y,
319 jp->circsize, jp->circsize);
320 if (jp->erase == 1) {
321 XDrawPoints(display, window, gc,
322 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
325 if (MI_NPIXELS(mi) > 2) {
326 XSetForeground(display, gc, MI_PIXEL(mi, jp->pix));
327 if (++jp->pix >= MI_NPIXELS(mi))
330 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
333 /* save calls to LRAND by using bit shifts over and over on the same
334 int for 32 iterations, then get a new random int */
338 /* complex sqrt: x^0.5 = radius^0.5*(cos(theta/2) + i*sin(theta/2)) */
343 /* Avoid atan2: DOMAIN error message */
344 if (xi == 0.0 && xr == 0.0)
347 theta = atan2(xi, xr) / 2.0;
349 /*r = pow(xi * xi + xr * xr, 0.25); */
350 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
355 if ((rnd >> (k % 32)) & 0x1) {
359 xp->x = jp->centerx + (int) ((jp->centerx >> 1) * xr);
360 xp->y = jp->centery + (int) ((jp->centery >> 1) * xi);
365 apply(jp, xr, xi, jp->depth);
367 XDrawPoints(display, window, gc,
368 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
371 if (jp->buffer > jp->nbuffers - 1) {
372 jp->buffer -= jp->nbuffers;
376 for (i = 0; i < REDRAWSTEP; i++) {
377 j = (jp->buffer - jp->redrawpos + jp->nbuffers) % jp->nbuffers;
378 XDrawPoints(display, window, gc,
379 jp->pointBuffer[j], numpoints, CoordModeOrigin);
381 if (++(jp->redrawpos) >= jp->nbuffers) {
390 release_julia (ModeInfo * mi)
392 if (julias != NULL) {
395 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
396 Display *display = MI_DISPLAY(mi);
397 juliastruct *jp = &julias[screen];
400 if (jp->pointBuffer) {
401 for (buffer = 0; buffer < jp->nbuffers; buffer++)
402 if (jp->pointBuffer[buffer])
403 (void) free((void *) jp->pointBuffer[buffer]);
404 (void) free((void *) jp->pointBuffer);
406 if (jp->stippledGC != None)
407 XFreeGC(display, jp->stippledGC);
408 if (jp->pixmap != None)
409 XFreePixmap(display, jp->pixmap);
412 XFreeCursor (display, jp->cursor);
415 (void) free((void *) julias);
421 refresh_julia (ModeInfo * mi)
423 juliastruct *jp = &julias[MI_SCREEN(mi)];
429 XSCREENSAVER_MODULE ("Julia", julia)