/*   浜様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様�
     �                                                               �
     �                                                               �
     �     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 cnum,iter;
	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);


	_setvideomode(16);
	a[0] =  0 ;  a[1] = .2;    a[2] = -.15; a[3] = .85;
	b[0] =  0 ;  b[1] = -.26;  b[2] = .28;  b[3] = .04;
	c[0] =  0 ;  c[1] = .23;   c[2] =.26;   c[3] = -.04;
	d[0] = .16;  d[1] = .22;   d[2] = .24;  d[3] = .85;
	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] = 328;  p[1] = 2621;  p[2] = 4915; p[3] = 32767;
	xscale = 400;
	yscale = 400;
	xoffset = -50;
	yoffset = -275;
	_clearscreen(_GCLEARSCREEN);
	_setcolor(cnum);
	image_draw(cnum,iter);
	getch();
}

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

	x = 0;
	y = 0;
	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);
}
