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 "*fpsSolid: true \n" \
47 # define UNIFORM_COLORS
48 # define reshape_julia 0
49 # define julia_handle_event 0
50 # include "xlockmore.h" /* in xscreensaver distribution */
51 #else /* !STANDALONE */
52 # include "xlock.h" /* in xlockmore distribution */
53 #endif /* !STANDALONE */
58 #define DEF_MOUSE "False"
60 static XrmOptionDescRec opts[] =
62 {"-mouse", ".julia.mouse", XrmoptionNoArg, "on"},
63 {"+mouse", ".julia.mouse", XrmoptionNoArg, "off"},
65 static argtype vars[] =
67 {&track_p, "mouse", "Mouse", DEF_MOUSE, t_Bool},
69 static OptionStruct desc[] =
71 {"-/+mouse", "turn on/off mouse tracking"},
74 ENTRYPOINT ModeSpecOpt julia_opts = { 2, opts, 1, vars, desc };
77 #define numpoints ((0x2<<jp->depth)-1)
81 int centery; /* center of the screen */
83 double ci; /* julia params */
92 int redrawing, redrawpos;
98 XPoint **pointBuffer; /* pointer for XDrawPoints */
102 static juliastruct *julias = NULL;
104 /* How many segments to draw per cycle when redrawing */
108 apply(juliastruct * jp, register double xr, register double xi, int d)
112 jp->pointBuffer[jp->buffer][jp->itree].x =
113 (int) (0.5 * xr * jp->centerx + jp->centerx);
114 jp->pointBuffer[jp->buffer][jp->itree].y =
115 (int) (0.5 * xi * jp->centery + jp->centery);
122 /* Avoid atan2: DOMAIN error message */
123 if (xi == 0.0 && xr == 0.0)
126 theta = atan2(xi, xr) / 2.0;
128 /*r = pow(xi * xi + xr * xr, 0.25); */
129 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
135 apply(jp, xr, xi, d);
136 apply(jp, -xr, -xi, d);
141 incr(ModeInfo * mi, juliastruct * jp)
150 XQueryPointer(MI_DISPLAY(mi), MI_WINDOW(mi),
151 &r, &c, &rx, &ry, &cx, &cy, &m);
152 if (cx <= 0 || cy <= 0 ||
153 cx >= MI_WIN_WIDTH(mi) || cy >= MI_WIN_HEIGHT(mi))
159 jp->cr = ((double) (cx + 2 - jp->centerx)) * 2 / jp->centerx;
160 jp->ci = ((double) (cy + 2 - jp->centery)) * 2 / jp->centery;
166 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 300.0)) *
167 sin(jp->inc * M_PI / 200.0));
168 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 300.0)) *
169 cos(jp->inc * M_PI / 200.0));
171 jp->cr += 0.5 * cos(M_PI * jp->inc / 400.0);
172 jp->ci += 0.5 * sin(M_PI * jp->inc / 400.0);
174 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 290.0)) *
175 sin(jp->inc * M_PI / 210.0));
176 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 310.0)) *
177 cos(jp->inc * M_PI / 190.0));
179 jp->cr += 0.5 * cos(M_PI * jp->inc / 395.0);
180 jp->ci += 0.5 * sin(M_PI * jp->inc / 410.0);
186 init_julia(ModeInfo * mi)
188 Display *display = MI_DISPLAY(mi);
189 Window window = MI_WINDOW(mi);
194 if (julias == NULL) {
195 if ((julias = (juliastruct *) calloc(MI_NUM_SCREENS(mi),
196 sizeof (juliastruct))) == NULL)
199 jp = &julias[MI_SCREEN(mi)];
201 jp->centerx = MI_WIN_WIDTH(mi) / 2;
202 jp->centery = MI_WIN_HEIGHT(mi) / 2;
204 jp->depth = MI_BATCHCOUNT(mi);
210 if (track_p && !jp->cursor)
214 black.red = black.green = black.blue = 0;
215 black.flags = DoRed|DoGreen|DoBlue;
216 bit = XCreatePixmapFromBitmapData (display, window, "\000", 1, 1,
217 MI_WIN_BLACK_PIXEL(mi),
218 MI_WIN_BLACK_PIXEL(mi), 1);
219 jp->cursor = XCreatePixmapCursor (display, bit, bit, &black, &black,
221 XFreePixmap (display, bit);
223 #endif /* HAVE_COCOA */
225 if (jp->pixmap != None &&
226 jp->circsize != (MIN(jp->centerx, jp->centery) / 60) * 2 + 1) {
227 XFreePixmap(display, jp->pixmap);
230 if (jp->pixmap == None) {
231 GC fg_gc = None, bg_gc = None;
233 jp->circsize = (MIN(jp->centerx, jp->centery) / 96) * 2 + 1;
234 jp->pixmap = XCreatePixmap(display, window, jp->circsize, jp->circsize, 1);
236 fg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
238 bg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
239 XFillRectangle(display, jp->pixmap, bg_gc,
240 0, 0, jp->circsize, jp->circsize);
241 if (jp->circsize < 2)
242 XDrawPoint(display, jp->pixmap, fg_gc, 0, 0);
244 XFillArc(display, jp->pixmap, fg_gc,
245 0, 0, jp->circsize, jp->circsize, 0, 23040);
247 XFreeGC(display, fg_gc);
249 XFreeGC(display, bg_gc);
253 if (MI_WIN_IS_INROOT(mi))
255 else if (jp->circsize > 0)
256 XDefineCursor (display, window, jp->cursor);
258 XUndefineCursor (display, window);
259 #endif /* HAVE_COCOA */
261 if (!jp->stippledGC) {
262 gcv.foreground = MI_WIN_BLACK_PIXEL(mi);
263 gcv.background = MI_WIN_BLACK_PIXEL(mi);
264 if ((jp->stippledGC = XCreateGC(display, window,
265 GCForeground | GCBackground, &gcv)) == None)
268 if (MI_NPIXELS(mi) > 2)
269 jp->pix = NRAND(MI_NPIXELS(mi));
270 jp->inc = ((LRAND() & 1) * 2 - 1) * NRAND(200);
271 jp->nbuffers = (MI_CYCLES(mi) + 1);
272 if (!jp->pointBuffer)
273 jp->pointBuffer = (XPoint **) calloc(jp->nbuffers, sizeof (XPoint *));
274 for (i = 0; i < jp->nbuffers; ++i)
275 if (jp->pointBuffer[i])
276 (void) memset((char *) jp->pointBuffer[i], 0,
277 numpoints * sizeof (XPoint));
279 jp->pointBuffer[i] = (XPoint *) calloc(numpoints, sizeof (XPoint));
283 XClearWindow(display, window);
287 /* hack: moved here by jwz. */
288 #define ERASE_IMAGE(d,w,g,x,y,xl,yl,xs,ys) \
290 (y<yl+ys)?XFillRectangle(d,w,g,xl,yl,xs,y-yl): \
291 XFillRectangle(d,w,g,xl,yl,xs,ys); \
293 (y>yl-ys)?XFillRectangle(d,w,g,xl,y+ys,xs,yl-y): \
294 XFillRectangle(d,w,g,xl,yl,xs,ys); \
296 (x<xl+xs)?XFillRectangle(d,w,g,xl,yl,x-xl,ys): \
297 XFillRectangle(d,w,g,xl,yl,xs,ys); \
299 (x>xl-xs)?XFillRectangle(d,w,g,x+xs,yl,xl-x,ys): \
300 XFillRectangle(d,w,g,xl,yl,xs,ys)
304 draw_julia (ModeInfo * mi)
306 Display *display = MI_DISPLAY(mi);
307 Window window = MI_WINDOW(mi);
309 juliastruct *jp = &julias[MI_SCREEN(mi)];
311 register double xr = 0.0, xi = 0.0;
312 int k = 64, rnd = 0, i, j;
313 XPoint *xp = jp->pointBuffer[jp->buffer], old_circle, new_circle;
315 old_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
316 old_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
318 new_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
319 new_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
320 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
321 ERASE_IMAGE(display, window, gc, new_circle.x, new_circle.y,
322 old_circle.x, old_circle.y, jp->circsize, jp->circsize);
323 /* draw a circle at the c-parameter so you can see it's effect on the
324 structure of the julia set */
325 XSetForeground(display, jp->stippledGC, MI_WIN_WHITE_PIXEL(mi));
327 XSetTSOrigin(display, jp->stippledGC, new_circle.x, new_circle.y);
328 XSetStipple(display, jp->stippledGC, jp->pixmap);
329 XSetFillStyle(display, jp->stippledGC, FillOpaqueStippled);
330 #endif /* HAVE_COCOA */
331 XFillRectangle(display, window, jp->stippledGC, new_circle.x, new_circle.y,
332 jp->circsize, jp->circsize);
333 if (jp->erase == 1) {
334 XDrawPoints(display, window, gc,
335 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
338 if (MI_NPIXELS(mi) > 2) {
339 XSetForeground(display, gc, MI_PIXEL(mi, jp->pix));
340 if (++jp->pix >= MI_NPIXELS(mi))
343 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
346 /* save calls to LRAND by using bit shifts over and over on the same
347 int for 32 iterations, then get a new random int */
351 /* complex sqrt: x^0.5 = radius^0.5*(cos(theta/2) + i*sin(theta/2)) */
356 /* Avoid atan2: DOMAIN error message */
357 if (xi == 0.0 && xr == 0.0)
360 theta = atan2(xi, xr) / 2.0;
362 /*r = pow(xi * xi + xr * xr, 0.25); */
363 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
368 if ((rnd >> (k % 32)) & 0x1) {
372 xp->x = jp->centerx + (int) ((jp->centerx >> 1) * xr);
373 xp->y = jp->centery + (int) ((jp->centery >> 1) * xi);
378 apply(jp, xr, xi, jp->depth);
380 XDrawPoints(display, window, gc,
381 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
384 if (jp->buffer > jp->nbuffers - 1) {
385 jp->buffer -= jp->nbuffers;
389 for (i = 0; i < REDRAWSTEP; i++) {
390 j = (jp->buffer - jp->redrawpos + jp->nbuffers) % jp->nbuffers;
391 XDrawPoints(display, window, gc,
392 jp->pointBuffer[j], numpoints, CoordModeOrigin);
394 if (++(jp->redrawpos) >= jp->nbuffers) {
403 release_julia (ModeInfo * mi)
405 if (julias != NULL) {
408 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
409 Display *display = MI_DISPLAY(mi);
410 juliastruct *jp = &julias[screen];
413 if (jp->pointBuffer) {
414 for (buffer = 0; buffer < jp->nbuffers; buffer++)
415 if (jp->pointBuffer[buffer])
416 (void) free((void *) jp->pointBuffer[buffer]);
417 (void) free((void *) jp->pointBuffer);
419 if (jp->stippledGC != None)
420 XFreeGC(display, jp->stippledGC);
421 if (jp->pixmap != None)
422 XFreePixmap(display, jp->pixmap);
425 XFreeCursor (display, jp->cursor);
428 (void) free((void *) julias);
434 refresh_julia (ModeInfo * mi)
436 juliastruct *jp = &julias[MI_SCREEN(mi)];
442 XSCREENSAVER_MODULE ("Julia", julia)