#include "Matrice.cpp"

#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef Matrice<double> Mat;
typedef Vecteur<double> Vect;

Mat L(3,3);

int max(int y, int x)
{
	return (x>y?x:y);
};

void Rossler(double a, double b, double c, Vect pt)
{
	
	double x=pt(1), y=pt(2), z=pt(3);
	if (fabs(x)>.1)
	{
		double temp[9]={0, -1, -1, 1, a, 0, b/x, 0, x-c};
		Mat tmp(3, 3, 0, temp);
		L=tmp;
	}
	else if (fabs(y)>.1)
	{
		double temp[9]={0, -1, -1, 1, a, 0, 0, b/y, x-c};
		Mat tmp(3, 3, 0, temp);
		L=tmp;
	}
	else
	{
		double temp[9]={0, -1, -1, 1, a, 0, 0, 0, x-c+b/z};
		Mat tmp(3, 3, 0, temp);
		L=tmp;
	};
};


void main()
{
	// Initialisations

	// Définition des paramètres:
	int precision;
	double a,b,c,delta_t,temps;
	float a_,b_,c_,delta_t_,temps_;
	char* nom_fichier=new char[255];
	double pt0[3]={-1.,0.1,0.4};
	float pt1=0, pt2=0, pt3=0;
	Vect temp(3), k1(3), pt(3,pt0);
	double coefficients0[4]={1/4.,1/3.,1/2.,1.};
	Vect coefficients(4,coefficients0);
    
	printf("Ce programme permet de generer des points de l'attracteur de Rossler.\n\n");
	printf("Entrez 0 pour prendre les valeurs par defaut.\n\n");
	printf("Coefficient a (0.398 par defaut): ");
	scanf("%f",&a_);
	printf("Coeficient b (2 par defaut): ");
	scanf("%f",&b_);
	printf("Coefficient c (4 par defaut): ");
	scanf("%f",&c_);
	printf("Condition initiale sous la forme X Y Z\n    (0 0 0 est remplace par -1 0.1 0.4): ");
	scanf("%f %f %f",&pt1, &pt2, &pt3);
	printf("Pas de temps (0.02 par defaut): ");
	scanf("%f",&delta_t_);
	printf("Duree de l'integration (400 par defaut): ");
	scanf("%f",&temps_);
	printf("\nPrecision des donnees (5 par defaut): ");
	scanf("%d",&precision);
	printf("Nom du fichier cible: ");
	scanf("%s",nom_fichier);

	a=a_;
	b=b_;
	c=c_;
	delta_t=delta_t_;
	temps=temps_;

	if (a==0) a=0.398;
	if (b==0) b=2.;
	if (c==0) c=4.;
	if (delta_t==0) delta_t=0.02;
	if (temps==0) temps=400;
	if (precision==0) precision=5;
	if ((pt1!=0) || (pt2!=0) || (pt3!=0))
	{
		pt(1)=pt1;
		pt(2)=pt2;
		pt(3)=pt3;
	};

	int nb=temps/delta_t;

	printf("\nGeneration des points...\n");

	k1=pt;
	
	// Génération des points

	FILE *f;
	f=fopen(nom_fichier,"wb");
	char* res=new char[precision+5];
	
	for (int i=0; i<nb; i++)
	{

		// Runge-Kutta
		Rossler(a,b,c,pt);
		for(int j=1; j<5; j++)
		{
			k1=pt+coefficients(j)*delta_t*L*k1;
		};
		pt=k1;

		_gcvt(pt(1),precision,res);
		fwrite(res,1,strlen(res),f);
		fwrite(" ",1,1,f);

		_gcvt(pt(2),precision,res);
		fwrite(res,1,strlen(res),f);
		fwrite(" ",1,1,f);

		_gcvt(pt(3),precision,res);
		fwrite(res,1,strlen(res),f);

		fwrite("\n",1,1,f);

		if (i==nb/2) printf("    Moitie des points generee...\n");
	};
}
