BENO Documentation
------------------

By Detlef Mueller

About Fractal images v1.0

Hi all.

Every time I start using a new computer or learn a new (computer) language,
I write a program which draws the Mandelbrot set Z(n+1) = Z(n)^2 + C.
Here are various versions I wrote for the HP48.

My first attempt (BEN.1 - user-RPL) uses the build-in features of the HP48 for
plotting truth diagrams, it works - but is very sloooow.
In the second try (BEN.2 - also user-RPL) I build a shell around the inner
loop, hoping this would speed up the program. Again slooow as molasses.
While starting with sys-RPL I wrote the next version (BEN.3), simply
transfering BEN.2 into sys-RPL. Much faster, but who wants to wait hours for
a picture ?
My last attemp was BENO.4, an optimized version of BEN.3. Again not very
fast.

Drawing Mandelbrot sets isn't a problem on the HP48, if you have enougth time
and batteries ;-). Maybe somebody write a version using integer arithmetics
to speed things up a lot ?

Personally I prefer using the BENOIT program for realy good pictures (the one
in the fader demo was generated with this program).

Have fun exermining the programs.

This is the (mostly ANSI-) C source of the program to generate Mandebrot-set
images. Its output to stdout is directly dowloadable into the HP48.

BEGIN_SRC beno_dm.c
/** BENOIT.C
 ** Detlef Mueller, 17.10.1987
 ** Draws the Mandelbrot-Set for Zn+1 = Zn^2+C
 ** 09.03.1991, Want to see it on a HP-48SX
 ** Usage: BENOIT >file
 **/

#include	<stdio.h>
#include	<stdlib.h>

#include	<conio.h>			/* MesS-DOS */

#define	XRESO		131
#define	YRESO		64
#define	SPACE		63
#define	DEPTH		96

#define	sqr( x )	((x)*(x))

typedef	struct
	{
	    double
		right, left,
		up, down ;
	    int
		xreso, yreso,
		depth, space ;
	    void
		(*plot)( int, int, int ) ;
	}
	BENOIT ;


/** key = Frac( data ) ;
 ** BENOIT *data	Pointer to datablock
 ** char key		'\0' if picture complete else keystroce
 ** Calculate & draw the M-set Zn+1 = Zn^2+C
 ** 29.10.1988 DM
 **/

char  Frac ( register BENOIT *data )
{
    double
	cr, zr, zrq, zi, ziq,
	ci,
	dr, di ;
    int
	x, y,
	iterator ;

    dr = (data->right - data->left) / data->xreso ;
    di = (data->up - data->down)    / data->yreso ;

    ci = data->down ;

    for ( y = 0 ; y < data->yreso ; ++y )
    {
	if ( kbhit() )				/* MesS-DOS */
	    return ( getch() ) ;

	fprintf( stderr, "\r y:%3d x:   ", y ) ;

	cr = data->left ;

	for ( x = 0 ; x < data->xreso ; ++x )
	{
	    fprintf( stderr, "\b\b\b%3d", x ) ;

	    zr =
	    zi =
	    zrq =
	    ziq = 0.0 ;
	    iterator = 0 ;

	    do
	    {
		zi *= zr ;
		zi += zi + ci ;
		zr = zrq - ziq + cr ;

		zrq = sqr( zr ) ;
		ziq = sqr( zi ) ;
	    }
	    while ( zrq + ziq <= 100.0 && ++iterator < data->depth ) ;

	    if ( iterator <= data->space || iterator == data->depth )
		(*data->plot)( x, y, iterator ) ;

	    cr += dr ;
	}

	ci += di ;
    }

    return ( '\0' ) ;
}

/** Plot( x, y, intens ) ;
 ** int x, y		Point to plot
 ** int intens		Intensity of point
 ** Set bit in scr addressed by x, y
 ** 09.03.1991 DM
 **/

unsigned char
    scr[((XRESO + 7) >> 3) * YRESO] = { 0 } ;

void  Plot ( int x, int y, int i )
{
    static char
	cnv[] =
	{
	    0x10, 0x20, 0x40, 0x80,
	    0x01, 0x02, 0x04, 0x08
	} ;

    if ( i & 1 || i == DEPTH )
	scr[(x >> 3) + y * ((XRESO + 7) >> 3)] |= cnv[x & 7] ;
}

/** main
 ** 09.03.1991 DM
 **/

void  main ( void )
{
    static BENOIT
	data =
	{
	    -7.6962882280349731e-01, -7.7681696414947510e-01,
	    1.2724681198596954e-01, 1.2206903845071793e-01,
	    XRESO, YRESO,
	    DEPTH, SPACE,
	    Plot
	} ;

    if ( Frac( &data ) == 0x1B )
	exit( -1 ) ;

    printf( "%%%%HP: T(3); \\<< GROB 131 64 " ) ;

    for ( int i = 0 ; i < sizeof( scr ) ; ++i )
	printf( "%02X", (unsigned int)scr[i] ) ;

    printf( " PICT STO { } PVIEW TEXT PICT PURGE \\>>" ) ;

    exit( 0 ) ;
}
