/*   浜様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様�
     �                                                               �
     �                                                               �
     �     image = PROGRAM TO GENERATE ITERATED FUNCTION SYSTEMS     �
     �                                                               �
     藩様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様�
*/

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

/* GLOBALS */
int LINEWIDTH,OPERATOR,XCENTER,YCENTER,ANGLE;
unsigned long int PATTERN;

void image_draw(int color, int iter);
void plots(int x, int y, int color);

int adapt,mode;
int j, k, xscale,yscale,xoffset,yoffset,pr,p[4],pk[4];
long unsigned int i;
float a[4],b[4],c[4],d[4],e[4],f[4],x,y,newx;

main()
{
	int iter,cnum;

	_setvideomode(16);

	printf("Enter the color (1-15) you wish to use: ");
	scanf("%d",&cnum);
	printf("Enter the number of iterations to use: ");
	scanf("%d",&iter);
	_clearscreen(_GCLEARSCREEN);

	a[0] =  0 ;  a[1] = .1;    a[2] =  .42; a[3] =  .42;
	b[0] =  0 ;  b[1] =  0;    b[2] = -.42; b[3] =  .42;
	c[0] =  0 ;  c[1] =  0;    c[2] =  .42; c[3] = -.42;
	d[0] = .5 ;  d[1] = .1;    d[2] =  .42; d[3] =  .42;
	e[0] =   0;  e[1] = 0;     e[2] = 0;    e[3] =    0;
	f[0] =   0;  f[1] = .2;    f[2] = .2;   f[3] =   .2;
	p[0] =1638;  p[1] = 6553;  p[2] =19660; p[3] = 32767;
	xscale = 750;
	yscale = 750;
	xoffset = 0;
	yoffset = -160;
	_clearscreen(_GCLEARSCREEN);
	_setcolor(9);
	image_draw(cnum,iter);
	getch();
}

void image_draw(int color, int iter)
{
	int px,py;

	x = 0;
	y = 0;
	_setcolor(color);
	for (i=1; i<=iter; i++)
	{
		j = rand();
		k = (j < p[0]) ? 0 : ((j < p[1]) ? 1 : ((j < p[2]) ? 2 : 3));
		newx = (a[k]* x + b[k] * y + e[k]);
		y = (c[k] * x + d[k] * y + f[k]);
		x = newx;
                px = x*xscale + xoffset;
                py = (y*yscale + yoffset);
		if ((px>=-320) && (px<320) && (py>=-240) && (py<240))
			plots (px,py,color);
	}
}

void plots(int x, int y, int color)
{
	#define convert(x,y)   {x = (x + 319);  y = (175 - ((93*y) >> 7));}
	convert(x,y);
	_setpixel(x,y);
}
