From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <8a9696ab24a9877f91bde19f6785268c@acl.lanl.gov> To: 9fans@cse.psu.edu From: andrey mirtchovski MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="upas-eriyzzoylhgicalfxwxcblpdvj" Subject: [9fans] thornbird (another xscreensaver hack) Date: Fri, 21 Jun 2002 16:21:26 -0600 Topicbox-Message-UUID: b5a3ed5e-eaca-11e9-9e20-41e7f4b1d025 This is a multi-part message in MIME format. --upas-eriyzzoylhgicalfxwxcblpdvj Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit this one compiles cleanly... i'm doing just the simple ones that involve XPoints for now, the complex ones will come later, as i become more familiar with X... andrey --upas-eriyzzoylhgicalfxwxcblpdvj Content-Disposition: attachment; filename=thornbird.c Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit /* thornbird --- continuously varying Thornbird set */ /*- * Copyright (c) 1996 by Tim Auckland * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, * provided that the above copyright notice appear in all copies and that * both that copyright notice and this permission notice appear in * supporting documentation. * * This file is provided AS IS with no warranties of any kind. The author * shall have no liability with respect to the infringement of copyrights, * trade secrets or any patents by this file or any part thereof. In no * event will the author be liable for any lost revenue or profits or * other special, indirect and consequential damages. * * "thornbird" shows a view of the "Bird in a Thornbush" fractal, * continuously varying the three free parameters. * * Revision History: * 01-Nov-2000: Allocation checks * 04-Jun-1999: 3D tumble added by Tim Auckland * 31-Jul-1997: Adapted from discrete.c Copyright (c) 1996 by Tim Auckland */ /* plan9-related stuff */ #include #include #include #include #define NULL nil #define XPoint Point #define SINF sin #define COSF cos #define NRAND nrand #define LRAND lrand #define MAXRAND ((2<<31)-1) #define FLOATRAND frand() #define M_PI 3.141592653589793238462643383 /* should be enough, no? */ #define BATCHCOUNT 3000 #define CYCLES 1 /* end of plan9-related stuff */ #define balance_rand(v) ((FLOATRAND*(v))-((v)/2)) /* random around 0 */ typedef struct thornbirdstruct thornbirdstruct; struct thornbirdstruct { int maxx; int maxy; /* max of the screen */ double a; double b; double c; double d; double e; double i; double j; /* thornbird parameters */ struct { double f1; double f2; } liss; struct { double theta; double dtheta; double phi; double dphi; } tumble; int inc; int pix; int count; int nbuffers; XPoint **pointBuffer; /* pointer for XDrawPoints */ }; static thornbirdstruct *thornbirds = (thornbirdstruct *) NULL; static void free_thornbird(thornbirdstruct *hp) { if (hp->pointBuffer != NULL) { int buffer; for (buffer = 0; buffer < hp->nbuffers; buffer++) if (hp->pointBuffer[buffer] != NULL) free((void *) hp->pointBuffer[buffer]); free((void *) hp->pointBuffer); hp->pointBuffer = (XPoint **) NULL; } } void init_thornbird(void) { thornbirdstruct *hp; if (thornbirds == NULL) { if ((thornbirds = (thornbirdstruct *)calloc(1, sizeof (thornbirdstruct))) == NULL) return; } hp = þbirds[0]; hp->maxx = Dx(screen->r); hp->maxy = Dy(screen->r); hp->b = 0.1; hp->i = hp->j = 0.1; hp->pix = 0; hp->inc = 0; hp->nbuffers = CYCLES; if (hp->pointBuffer == NULL) if ((hp->pointBuffer = (XPoint **) calloc(CYCLES, sizeof (XPoint *))) == NULL) { free_thornbird(hp); return; } if (hp->pointBuffer[0] == NULL) if ((hp->pointBuffer[0] = (XPoint *) malloc(BATCHCOUNT * sizeof (XPoint))) == NULL) { free_thornbird(hp); return; } /* select frequencies for parameter variation */ hp->liss.f1 = LRAND() % 5000; hp->liss.f2 = LRAND() % 2000; /* choose random 3D tumbling */ hp->tumble.theta = 0; hp->tumble.phi = 0; hp->tumble.dtheta = balance_rand(0.001); hp->tumble.dphi = balance_rand(0.005); hp->count = 0; } void draw_thornbird(void) { double oldj, oldi; int batchcount = BATCHCOUNT; int k; XPoint *xp; int current; int i; double sint, cost, sinp, cosp; thornbirdstruct *hp; if (thornbirds == NULL) return; hp = þbirds[0]; if (hp->pointBuffer == NULL) return; current = hp->inc % CYCLES; k = batchcount; xp = hp->pointBuffer[current]; /* vary papameters */ hp->a = 1.99 + (0.4 * sin(hp->inc / hp->liss.f1) + 0.05 * cos(hp->inc / hp->liss.f2)); hp->c = 0.80 + (0.15 * cos(hp->inc / hp->liss.f1) + 0.05 * sin(hp->inc / hp->liss.f2)); /* vary view */ hp->tumble.theta += hp->tumble.dtheta; hp->tumble.phi += hp->tumble.dphi; sint = sin(hp->tumble.theta); cost = cos(hp->tumble.theta); sinp = sin(hp->tumble.phi); cosp = cos(hp->tumble.phi); while (k--) { oldj = hp->j; oldi = hp->i; hp->j = oldi; hp->i = (1 - hp->c) * cos(M_PI * hp->a * oldj) + hp->c * hp->b; hp->b = oldj; xp->x = screen->r.min.x + (hp->maxx / 2 * (1 + sint*hp->j + cost*cosp*hp->i - cost*sinp*hp->b)); xp->y = screen->r.min.y + (hp->maxy / 2 * (1 - cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b)); xp++; } draw(screen, screen->r, display->black, nil, ZP); for(i = 0; i < batchcount; i++) { Rectangle r; Point p; p = hp->pointBuffer[current][i]; r = Rpt(p, addpt(p, Pt(1, 1))); draw(screen, r, display->white, nil, ZP); } hp->inc++; } void release_thornbird(void) { if (thornbirds != NULL) { free_thornbird(þbirds[0]); free((void *) thornbirds); thornbirds = (thornbirdstruct *) NULL; } } void refresh_thornbird() { draw(screen, screen->r, display->black, nil, ZP); } void eresized(int new) { if(new && getwindow(display, Refnone) < 0) { sysfatal("can't reattach to window"); } draw(screen, screen->r, display->black, nil, ZP); if(thornbirds != nil) release_thornbird(); init_thornbird(); } char *buttons[] = { "exit", 0 }; Menu menu = { buttons }; void main(int argc, char **argv) { Mouse m; vlong tbegin, tend; ulong frames = 0; USED(argc, argv); srand(time(0)); if(initdraw(nil, nil, "bez") < 0) sysfatal("initdraw failed: %r"); einit(Emouse); eresized(0); tbegin = nsec(); for(;;) { if(ecanmouse()) { m = emouse(); if(m.buttons&4) if(emenuhit(3, &m, &menu) == 0) { tend = nsec(); print("fps: %uf\n", frames/((tend - tbegin)/1000000000.0)); exits(0); } } draw_thornbird(); frames++; } } --upas-eriyzzoylhgicalfxwxcblpdvj--