/* 3circle.cpp
*
*   Example of image generation for three overlapping circles
*   each circle a different primary color. 
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "image.h"

char text[256];
void three_circles(IMAGE *out);

int main(int argc, char *argv[])
{
	char *filename;
	IMAGE *f;
	int size;
	if (argc>1) filename = argv[1];
	else {
		printf("Enter filename: ");
		gets(text);
		if (*text==0 || *text=='\n') exit(-1);
		filename = text;
	}
	printf("filename: %s\n",filename);
	size = 360;
	if (argc>2) sscanf(argv[2],"%d",&size);
	f = make_image(filename,size,(int) (size*sqrt(3.0)/2.0+9),PIX_RGB);
	printf("image size: %d x %d\n",f->hlen,f->vlen);
	three_circles(f);
	return 0;
}

void three_circles(IMAGE *out)
{
	int i, j;
	double x, y,dx, dy;
	double d1,d2,d3 ,r,g,b;
	pixel *buf, *bp ,z;
	double xc, yc ,xc1, yc1, xc2,yc2 ,xc3 ,yc3;
	xc = out->hlen/2;
	yc = out->vlen/2;
	xc1 = -0.3;
	yc1 = -0.27;
	xc2 =  0.3;
	yc2 = -0.27;
	xc3 = 0;
	yc3 = 0.3*sqrt(3.0)-0.27;	
		
	dx = 1.0/xc;
	dy = 1.0/xc;
	buf = make_buffer(out);
	double radius = 0.6 ;
		for (j=0; j<out->vlen; j++) {
		bp = buf;
		y = dy*(yc-j);
		for (i=0; i<out->hlen; i++) {
			x = dx*(i-xc);
			d1 = hypot((xc1-x),(yc1-y)); 
			d2 = hypot((xc2-x),(yc2-y)); 
			d3 = hypot((xc3-x),(yc3-y)); 
			/*printf("\n x = %f y=%f  ",x,y); */
 			if(d1>radius) r=0;
			  else r=255;
			if(d2>radius) b=0;
			  else b=255;  
			if(d3>radius) g=0;
			  else g=255;			 			
			z=RGB(r,g,b); 
			*bp++ = z;
		}
		put_line(out,j,buf,PIX_RGB);
	}
	free_buffer(buf);
}

	