/* firefly1.c		u̔ */

#include	<stdio.h>
#include	"wingxa.h"

#define		rep 100	/* Repeat number */

const int
	L = 300,	/* Screen size */
	d = 5; 		/* Diameter of lamp */
	nm = 3		/* Lamp number */
const float
	r = 3.8,	/* Supression constant */
	K = 1;  	/* Enhandement constant */


/* boundary conditions */
const float
	x[nm] ;			// intensity value
	for(i=1;i<=nm;i++){
		x[i]=rnd(1);	// set initial value
	}

/* array of light intensity record */
float	a[nm][rep];


/* valiables */
struct Light{		// Light describing data
	int	px;	// lamp position x 0-L
	int	py;	// lamp position y 0-L
	int	g;	// temporal intensity 0-255
	int	gp;	// object intensity 0-255
	};

Light	Lt[nm];		// light data set array

int	i,		// loop cycle
	k,		// lamp number
	sign;		// up down sign
float	p,
	xx[nm];		// trangient intensity


void	lamp(int k)	// light the green lamp on screen

/********************************************************/
/* gradually imcrease/decrease intensity of light to gp */
/********************************************************/

{
	for(k=1;k<=3;k++)
	{
	if(g[k]<gp[k]){		// object value is larger
		sign=-1;	// set decrement
	} else {		// object value is simaller
		sign=1;		// set increment
	}
	//printf("g,gp = %d,%d",g,gp);
	while(j!=g[k])		// until g reaches the object value
	{
		gcolor(0,j,0);	// set color
		gcirc(50,50,d);	// draw circle
		sleep(5);	// wait 5msec
		j=j+sign;	// increment of g
	}

}


float	logistic1(int i,int k)	// logistic function

/****************************************/
/* calculate discrete logistic equation */
/****************************************/

{
	x=ax[k][i-1];
	xx=r*x*(K-x);		// calculate next value

	//printf("i,x,dx,g = %d,%5.3f,%5.3f\n",i,xx,dx);	
	return xx;
}


void	main(void)
{
	/* graphic initialization */
	gwinpos(0,0);	// Screen position
	gwinsize(L,L);	// Screen size
	ginit(3);	// Screen initialize
	gsetorg(0,0);	// Set x,y origin left low
	xx=x0;yy=y0,zz=z0;	// set initial value

	while(1)	// repeat light draw loop
	{
		if(keypress())	break;	// stop by any key press
		for(k=1;k<=nm;k++)
			{
			if(g[k]==gp[k])
			{
				ax[k][i]=logistic1(i,k);// record old values to array
				g=ax[k]*255;		// intensity of green
				gp=xp*255;		// previous intensity
			}
		i+=1;
		lamp(k);		// light lamp

	}

	//for(i=1;i<=rep;i++){
	//	printf(">%5.3f",ax[i]);
	//}
	gcls();

}
