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" \
46 "*ignoreRotation: True \n" \
48 # define UNIFORM_COLORS
49 # define release_julia 0
50 # define reshape_julia 0
51 # include "xlockmore.h" /* in xscreensaver distribution */
52 #else /* !STANDALONE */
53 # include "xlock.h" /* in xlockmore distribution */
54 #endif /* !STANDALONE */
57 #define DEF_MOUSE "False"
59 ENTRYPOINT ModeSpecOpt julia_opts = { 0, };
62 #define numpoints ((0x2<<jp->depth)-1)
66 int centery; /* center of the screen */
68 double ci; /* julia params */
77 int redrawing, redrawpos;
83 XPoint **pointBuffer; /* pointer for XDrawPoints */
89 static juliastruct *julias = NULL;
91 /* How many segments to draw per cycle when redrawing */
95 apply(juliastruct * jp, register double xr, register double xi, int d)
99 jp->pointBuffer[jp->buffer][jp->itree].x =
100 (int) (0.5 * xr * jp->centerx + jp->centerx);
101 jp->pointBuffer[jp->buffer][jp->itree].y =
102 (int) (0.5 * xi * jp->centery + jp->centery);
109 /* Avoid atan2: DOMAIN error message */
110 if (xi == 0.0 && xr == 0.0)
113 theta = atan2(xi, xr) / 2.0;
115 /*r = pow(xi * xi + xr * xr, 0.25); */
116 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
122 apply(jp, xr, xi, d);
123 apply(jp, -xr, -xi, d);
128 incr(ModeInfo * mi, juliastruct * jp)
130 if (jp->button_down_p)
132 jp->cr = ((double) (jp->mouse_x + 2 - jp->centerx)) * 2 / jp->centerx;
133 jp->ci = ((double) (jp->mouse_y + 2 - jp->centery)) * 2 / jp->centery;
138 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 300.0)) *
139 sin(jp->inc * M_PI / 200.0));
140 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 300.0)) *
141 cos(jp->inc * M_PI / 200.0));
143 jp->cr += 0.5 * cos(M_PI * jp->inc / 400.0);
144 jp->ci += 0.5 * sin(M_PI * jp->inc / 400.0);
146 jp->cr = 1.5 * (sin(M_PI * (jp->inc / 290.0)) *
147 sin(jp->inc * M_PI / 210.0));
148 jp->ci = 1.5 * (cos(M_PI * (jp->inc / 310.0)) *
149 cos(jp->inc * M_PI / 190.0));
151 jp->cr += 0.5 * cos(M_PI * jp->inc / 395.0);
152 jp->ci += 0.5 * sin(M_PI * jp->inc / 410.0);
158 init_julia(ModeInfo * mi)
160 Display *display = MI_DISPLAY(mi);
161 Window window = MI_WINDOW(mi);
166 MI_INIT (mi, julias);
167 jp = &julias[MI_SCREEN(mi)];
169 jp->centerx = MI_WIN_WIDTH(mi) / 2;
170 jp->centery = MI_WIN_HEIGHT(mi) / 2;
172 jp->depth = MI_BATCHCOUNT(mi);
178 if (jp->button_down_p && !jp->cursor && !jp->cursor)
182 black.red = black.green = black.blue = 0;
183 black.flags = DoRed|DoGreen|DoBlue;
184 bit = XCreatePixmapFromBitmapData (display, window, "\000", 1, 1,
185 MI_WIN_BLACK_PIXEL(mi),
186 MI_WIN_BLACK_PIXEL(mi), 1);
187 jp->cursor = XCreatePixmapCursor (display, bit, bit, &black, &black,
189 XFreePixmap (display, bit);
191 #endif /* HAVE_JWXYZ */
193 if (jp->pixmap != None &&
194 jp->circsize != (MIN(jp->centerx, jp->centery) / 60) * 2 + 1) {
195 XFreePixmap(display, jp->pixmap);
198 if (jp->pixmap == None) {
199 GC fg_gc = None, bg_gc = None;
201 jp->circsize = MAX(8, (MIN(jp->centerx, jp->centery) / 96) * 2 + 1);
202 jp->pixmap = XCreatePixmap(display, window, jp->circsize, jp->circsize, 1);
204 fg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
206 bg_gc = XCreateGC(display, jp->pixmap, GCForeground, &gcv);
207 XFillRectangle(display, jp->pixmap, bg_gc,
208 0, 0, jp->circsize, jp->circsize);
209 if (jp->circsize < 2)
210 XDrawPoint(display, jp->pixmap, fg_gc, 0, 0);
212 XFillArc(display, jp->pixmap, fg_gc,
213 0, 0, jp->circsize, jp->circsize, 0, 23040);
215 XFreeGC(display, fg_gc);
217 XFreeGC(display, bg_gc);
221 if (MI_WIN_IS_INROOT(mi))
223 else if (jp->circsize > 0)
224 XDefineCursor (display, window, jp->cursor);
226 XUndefineCursor (display, window);
227 #endif /* HAVE_JWXYZ */
229 if (!jp->stippledGC) {
230 gcv.foreground = MI_WIN_BLACK_PIXEL(mi);
231 gcv.background = MI_WIN_BLACK_PIXEL(mi);
232 if ((jp->stippledGC = XCreateGC(display, window,
233 GCForeground | GCBackground, &gcv)) == None)
236 if (MI_NPIXELS(mi) > 2)
237 jp->pix = NRAND(MI_NPIXELS(mi));
238 jp->inc = ((LRAND() & 1) * 2 - 1) * NRAND(200);
239 jp->nbuffers = (MI_CYCLES(mi) + 1);
240 if (!jp->pointBuffer)
241 jp->pointBuffer = (XPoint **) calloc(jp->nbuffers, sizeof (XPoint *));
242 for (i = 0; i < jp->nbuffers; ++i)
243 if (jp->pointBuffer[i])
244 (void) memset((char *) jp->pointBuffer[i], 0,
245 numpoints * sizeof (XPoint));
247 jp->pointBuffer[i] = (XPoint *) calloc(numpoints, sizeof (XPoint));
251 XClearWindow(display, window);
256 julia_handle_event (ModeInfo *mi, XEvent *event)
258 juliastruct *jp = &julias[MI_SCREEN(mi)];
260 if (event->xany.type == ButtonPress &&
261 event->xbutton.button == Button1)
263 jp->button_down_p = True;
264 jp->mouse_x = event->xbutton.x;
265 jp->mouse_y = event->xbutton.y;
268 else if (event->xany.type == ButtonRelease &&
269 event->xbutton.button == Button1)
271 jp->button_down_p = False;
274 else if (event->xany.type == MotionNotify && jp->button_down_p)
276 jp->mouse_x = event->xmotion.x;
277 jp->mouse_y = event->xmotion.y;
286 /* hack: moved here by jwz. */
287 #define ERASE_IMAGE(d,w,g,x,y,xl,yl,xs,ys) \
289 (y<yl+ys)?XFillRectangle(d,w,g,xl,yl,xs,y-yl): \
290 XFillRectangle(d,w,g,xl,yl,xs,ys); \
292 (y>yl-ys)?XFillRectangle(d,w,g,xl,y+ys,xs,yl-y): \
293 XFillRectangle(d,w,g,xl,yl,xs,ys); \
295 (x<xl+xs)?XFillRectangle(d,w,g,xl,yl,x-xl,ys): \
296 XFillRectangle(d,w,g,xl,yl,xs,ys); \
298 (x>xl-xs)?XFillRectangle(d,w,g,x+xs,yl,xl-x,ys): \
299 XFillRectangle(d,w,g,xl,yl,xs,ys)
303 draw_julia (ModeInfo * mi)
305 Display *display = MI_DISPLAY(mi);
306 Window window = MI_WINDOW(mi);
308 juliastruct *jp = &julias[MI_SCREEN(mi)];
310 register double xr = 0.0, xi = 0.0;
311 int k = 64, rnd = 0, i, j;
312 XPoint *xp = jp->pointBuffer[jp->buffer], old_circle, new_circle;
314 old_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
315 old_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
317 new_circle.x = (int) (jp->centerx * jp->cr / 2) + jp->centerx - 2;
318 new_circle.y = (int) (jp->centery * jp->ci / 2) + jp->centery - 2;
319 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
320 XFillArc(display, window, gc,
321 old_circle.x-jp->circsize/2-2,
322 old_circle.y-jp->circsize/2-2,
323 jp->circsize+4, jp->circsize+4,
325 /* draw a circle at the c-parameter so you can see it's effect on the
326 structure of the julia set */
327 XSetForeground(display, jp->stippledGC, MI_WIN_WHITE_PIXEL(mi));
329 XSetTSOrigin(display, jp->stippledGC, new_circle.x, new_circle.y);
330 XSetStipple(display, jp->stippledGC, jp->pixmap);
331 XSetFillStyle(display, jp->stippledGC, FillOpaqueStippled);
332 #endif /* HAVE_JWXYZ */
333 XDrawArc(display, window, jp->stippledGC,
334 new_circle.x-jp->circsize/2,
335 new_circle.y-jp->circsize/2,
336 jp->circsize, jp->circsize,
339 if (jp->erase == 1) {
340 XDrawPoints(display, window, gc,
341 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
344 if (MI_NPIXELS(mi) > 2) {
345 XSetForeground(display, gc, MI_PIXEL(mi, jp->pix));
346 if (++jp->pix >= MI_NPIXELS(mi))
349 XSetForeground(display, gc, MI_WIN_WHITE_PIXEL(mi));
352 /* save calls to LRAND by using bit shifts over and over on the same
353 int for 32 iterations, then get a new random int */
357 /* complex sqrt: x^0.5 = radius^0.5*(cos(theta/2) + i*sin(theta/2)) */
362 /* Avoid atan2: DOMAIN error message */
363 if (xi == 0.0 && xr == 0.0)
366 theta = atan2(xi, xr) / 2.0;
368 /*r = pow(xi * xi + xr * xr, 0.25); */
369 r = sqrt(sqrt(xi * xi + xr * xr)); /* 3 times faster */
374 if ((rnd >> (k % 32)) & 0x1) {
378 xp->x = jp->centerx + (int) ((jp->centerx >> 1) * xr);
379 xp->y = jp->centery + (int) ((jp->centery >> 1) * xi);
384 apply(jp, xr, xi, jp->depth);
386 XDrawPoints(display, window, gc,
387 jp->pointBuffer[jp->buffer], numpoints, CoordModeOrigin);
390 if (jp->buffer > jp->nbuffers - 1) {
391 jp->buffer -= jp->nbuffers;
395 for (i = 0; i < REDRAWSTEP; i++) {
396 j = (jp->buffer - jp->redrawpos + jp->nbuffers) % jp->nbuffers;
397 XDrawPoints(display, window, gc,
398 jp->pointBuffer[j], numpoints, CoordModeOrigin);
400 if (++(jp->redrawpos) >= jp->nbuffers) {
409 free_julia (ModeInfo * mi)
411 Display *display = MI_DISPLAY(mi);
412 juliastruct *jp = &julias[MI_SCREEN(mi)];
415 if (jp->pointBuffer) {
416 for (buffer = 0; buffer < jp->nbuffers; buffer++)
417 if (jp->pointBuffer[buffer])
418 (void) free((void *) jp->pointBuffer[buffer]);
419 (void) free((void *) jp->pointBuffer);
421 if (jp->stippledGC != None)
422 XFreeGC(display, jp->stippledGC);
423 if (jp->pixmap != None)
424 XFreePixmap(display, jp->pixmap);
427 XFreeCursor (display, jp->cursor);
433 refresh_julia (ModeInfo * mi)
435 juliastruct *jp = &julias[MI_SCREEN(mi)];
442 XSCREENSAVER_MODULE ("Julia", julia)