#include <GL/glut.h>
#include <vector>

#include "parametres.h"
#include "CPointChaud.h"
#include "globale.h"

void DeriveXX(float Avant[DIV_X][DIV_Y], float Apres[DIV_X][DIV_Y])
{
	int i, j;

	for (i = 1; i < DIV_X - 1; i++)
		for (j = 0; j < DIV_Y; j++)
			Apres[i][j] = (Avant[i+1][j] + Avant[i-1][j] - 2*Avant[i][j]) / 2;

	for (j = 0; j < DIV_Y; j++)
	{
		Apres[0][j] = Avant[1][j] - Avant[0][j];
		Apres[DIV_X - 1][j] = Avant[DIV_X - 2][j] - Avant[DIV_X - 1][j];
	}
}

void DeriveYY(float Avant[DIV_X][DIV_Y], float Apres[DIV_X][DIV_Y])
{
	int i, j;

	for (i = 0; i < DIV_X; i++)
		for (j = 1; j < DIV_Y - 1; j++)
			Apres[i][j] = (Avant[i][j+1] + Avant[i][j-1] - 2*Avant[i][j]) / 2;

	for (i = 0; i < DIV_X; i++)
	{
		Apres[i][0] = Avant[i][1] - Avant[i][0];
		Apres[i][DIV_Y - 1] = Avant[i][DIV_Y - 2] - Avant[i][DIV_Y - 1];
	}
}


void CalculTemp(void)
{
	float Uxx[DIV_X][DIV_Y];
	float Uyy[DIV_X][DIV_Y];

	DeriveXX(Temperatures, Uxx);
	DeriveYY(Temperatures, Uyy);

	int i, j;

	for (i = 0; i < DIV_X; i++)
		for (j = 0; j < DIV_Y; j++)
			Temperatures[i][j] += ALPHA2*(Uxx[i][j] + Uyy[i][j])*DT;

	for (i = 0; i < (signed) LesPoints.size(); i++)
		Temperatures[LesPoints[i].x][LesPoints[i].y] = LesPoints[i].LaTemperature;
}



void SetCoul(float Temperature)
{
	float Temp;

	if (COULEUR)
	{
		if (Temperature <= TEMPMIN)
			glColor3f(0,0,1);
		else if ((Temperature >= TEMPMIN) && (Temperature <= MILIEU))
		{
			Temp = 2 * float (Temperature - TEMPMIN) / DIFF;
			glColor3f(0, Temp, 1 - Temp);
		}
		else if ((Temperature >= MILIEU) && (Temperature <= TEMPMAX))
		{
			Temp = 2 * float (TEMPMAX - Temperature) / DIFF;
			glColor3f(1 - Temp, Temp, 0);
		}
		else
			glColor3f(1,0,0);
	}
	else
	{
		if (Temperature <= TEMPMIN)
			glColor3f(0,0,0);
		else if (Temperature >= TEMPMAX)
			glColor3f(1,1,1);
		else
		{
			Temp = (float (Temperature - TEMPMIN)) / (float (TEMPMAX - TEMPMIN));
			glColor3f(Temp,Temp,Temp);
		}
	}
}
