/* Ansi-compliant version of raster.c */
#define UNIX 0         /*  change to  0  if not a unix system  */

	/* The Unix features will compile -- with warnings -- on
	unix using cc or gcc. They will not compile on unix using
	c++. Don't know why. */

#define TITLE "Put title here"   
			/* for none, use "".  NO UNBALANCED PARENS  */

#include <math.h>
#include <stdio.h>

			/* Recommended size is .8, 1.6, 2.4 (small
			   multiples of .8 inches.) Can go up to 6.4
			   for final copy. Recommended coarseness is
			   2, 4 or 8. Can go down to 1 for final copy. */

#define MESH 300                  /*  dots per inch ; don't change  */
#define SIZE 4.0                  /*  width = height of picture, in inches  */
#define DOTCOUNT (MESH*SIZE)      /*  width = height of picture
                                                        measured in dots   */
#define POINTS_PER_INCH 72	  /* don't change this */

#define COARSENESS 1      /* should be 1, 2, 4, 8, 16 or 32 */

/* prototypes */
void begin_picture(void);
void showpage(void);
void begin_bitmap(int X, int Y);
void begin_small_bitmap(int X, int Y);
void end_bitmap(void);
int f(double r, double s);
double g(double x, double y);

void main(void)
{
			/* The picture will be scaled so that
			   X,Y,j,k,m will all enumerate pixels */

int X,Y;  int  m, j, k; int CODE;
char c; char temp[300];
int  ff, i, ii, total;
double x, y;

begin_picture();
for (Y=0;Y < DOTCOUNT;Y+=32) for (X=0;X < DOTCOUNT;X+=32)
{
	/*
	Bitmap begins here; reserved for later delivery, however.
	*/
	i=0; total=0;
	for (m=0;m <32;m+=COARSENESS)
	{
		/* Each  j-value corresponds to one row of the bitmap */
		for  (j=0;j<32;j+=(8*COARSENESS))
		{	
			/* And each k-value corresponds to an 8-bit character
			   describing 8 bits of the row. If the row length is
			   less than eight, then padding is added below. */
			CODE=0;
			for  (k=0;((k<(8*COARSENESS))&&(k<32));k+=COARSENESS)
			{
				x  =  (X + j + k)/DOTCOUNT;
				y  =  (Y + m)/DOTCOUNT;
				ff  =   f(x,y);
				CODE = (2*CODE) + ff;
				total += ff;
			}
				/* Padding for each row of the bitmap, as
				   specified in footnote 6 on page 74 of
				   the PostScript manual: */
			   if(COARSENESS== 8) CODE*= 16;
			   if(COARSENESS==16) CODE*= 64;
			   if(COARSENESS==32) CODE*=128;
			/* Here the 8-bit character CODE is delivered as
			   two hexadecimal digits: */
			c = CODE/16;
			if (c<10) temp[i]=c+'0'; else temp[i]=c-10+'A';
				i++;
			c = CODE%16;
			if (c<10) temp[i]=c+'0'; else temp[i]=c-10+'A';
				i++;
		}
	}
/*
printf("\nTotal = %d\n",total);
*/
	if (total==0)
	{
		begin_small_bitmap(X,Y); 
		printf("00");
		end_bitmap();
	}
	else if(total==((32/COARSENESS)*(32/COARSENESS)))
	{
		begin_small_bitmap(X,Y); 
		printf("80");
		end_bitmap();
	}
	else
	{
		begin_bitmap(X,Y); 
		for (ii=0;ii<i;ii++)
		{
				/* Enough newlines so that one can easily
				   examine output when necessary: */
			if ((ii%64==0)&&(ii>0)) putchar('\n');
			putchar(temp[ii]);
		}
		end_bitmap();
	}
}
showpage();
}

char months[12][20] = {"January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October",
		"November", "December"};
char days[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday",
			"Thursday", "Friday", "Saturday"};

void begin_picture(void)
{
#if(UNIX)
char *getlogin();
#include <sys/types.h>
#include <sys/time.h>
char month[20];
char day[20];
struct tm *localtime(), *tp;
struct timeval tv;
struct timezone tz;

#endif
	double scaling_factor = (((double)POINTS_PER_INCH)/MESH);
	int size = 32/COARSENESS;
	printf("%%!PS\n");   /* PS added 11/10/99 wt  */

printf("\t(\\tRaster picture");
#if(UNIX)
printf(" by %s",getlogin());
#endif
printf(",\\n) print\n");
printf("\t(\\tprepared with software by W. Taylor. See Section\\n) print\n");
printf("\t(\\t2.7 of his \"Geometry of Computer Graphics,\"\\n)      print\n");
printf("\t(\\tpublished by Wadsworth-Brooks-Cole, 1991.\\n)        print\n");
printf("\t(\\t                 * * * *                 \\n)        print\n");
printf("\t(\\tSize: "); printf("%2.1f",SIZE);
		printf(" inches.\\n)                                print\n");
printf("\t(\\tPixel resolution: "); printf("%d",COARSENESS);
		printf("\\n)                              print\n");
printf("\t(\\tName of picture: "); printf(TITLE);
		printf("\\n)    print\n");

printf("save 214.2 88.2 moveto                       ");
printf("%%location of the label\n");
printf("/Times-Roman findfont 14 scalefont setfont   %%font for the label\n");
printf("("); printf(TITLE); printf(") show    %%the label itself\n");

#if(UNIX)
printf("restore save 400.2 48.2 moveto               ");
printf("%%location of the username\n");
printf("/Times-Roman findfont  8 scalefont");
printf(" setfont   %%font for the username\n");
printf("(Login name: %s) show\n",getlogin());
	gettimeofday(&tv, &tz);
	tp = localtime(&tv.tv_sec);
printf("400.2 33.2 moveto               ");
printf("%%location of the date\n");
printf("(%s, %s %d, %d) show restore\n",
	days[tp->tm_wday], months[tp->tm_mon], tp->tm_mday, 1900+tp->tm_year);
/*
if (tp->tm_hour==0)
printf("             %d:%d  AM\n",   12      ,tp->tm_min);
if (tp->tm_hour<12)
printf("             %d:%d  AM\n",tp->tm_hour,tp->tm_min);
if (tp->tm_hour==12)
printf("             %d:%d  PM\n",    0      ,tp->tm_min);
if (tp->tm_hour>12)
printf("             %d:%d  PM\n",tp->tm_hour -12,tp->tm_min);
*/
#endif

printf("%%%%\n%%%%\n");

printf("000.0 150.00 translate\n");

	printf("%3.2f %3.2f scale ", scaling_factor, scaling_factor);
	printf("%% This makes one unit = one pixel, at 300 DPI\n");
	printf("/x {image restore save} def\n");
	printf("/y {translate %d %d 1",size,size);
	printf(" [%4.5f 0 0 %4.5f 0 0]",(float)size/32,
						     (float)size/32);
	printf("} def\n");
	printf("/z {translate 1 1 1 [0.03125 0 0");
					printf(" 0.03125 0 0]} def\n");
	printf("save\n");
}

void begin_small_bitmap(int X,int Y)
{
	int size = 32/COARSENESS;
	printf("%d %d ",X+200,Y+200);
	printf("z\n");
	printf("{<\n");                  /*} to balance parens */
}

void begin_bitmap(int X,int Y)
{
	int size = 32/COARSENESS;
	printf("%d %d ",X+200,Y+200);
	printf("y\n");
	printf("{<\n");
}

void end_bitmap()
{
	printf(">}\n");
	printf("x ");
}

void showpage(void )
{
	printf("\nshowpage\n");
}


/* Here, for the sake of interest, is the very cubic curve drawn on
page 166 of the text. The function  g  computes the cubic;  the function
f  (which is the one we plot) decides whether  f  is positive or
negative.
*/

int f(double r, double s)
{
double z;
int k;

z  =  g(r,s);

if  (z>0.0)  return (0);
return (1);
}


double g(double x, double y)
{
double u, v;

u  =  4*x - 2.0;
v  =  4*y - 2.0;

return (u*u*u - u -v*v);
}