#include "Wykres.h"

void Wykres(double *x, double *y, int n, char *Xlabel, char *Ylabel)
{
	// Funkcja sluzy do rysowania wykresu w oknie graficznym z danych wprowadzonych przez tablice x i y
	int H = 500;
	int W = 800;
	int np = 10; // liczba podzialow

	double x0, y0;
	double *xp, *yp;
	xp = new double[n];
	yp = new double[n];

	//graphics(W, H);

	// Wyznacz xmin, xmax, ymin, ymax
	double xmin, xmax, ymin, ymax;

	xmin = x[0];
	xmax = x[0];
	ymin = y[0];
	ymax = y[0];

	for(int i = 1; i<n; ++i)
	{
		if(x[i] > xmax)
			xmax = x[i];
		if(x[i] < xmin)
			xmin = x[i];
		if(y[i] > ymax)
			ymax = y[i];
		if(y[i] < ymin)
			ymin = y[i];
	}

	// Okresl polozenie osi x
	if((xmin <= 0) && (xmax >= 0))
	{
		x0 = -xmin/(xmax-xmin)*0.8*W+0.1*W;
	}
	else
	{
		x0 = 0.1*W;
	}

	// Okresl polozenie osi y
	if((ymin <= 0) && (ymax >= 0))
	{
		y0 = 0.1*H+ymax/(ymax - ymin)*0.8*H;
	}
	else
	{
		y0 = 0.9*H;
	}

	// Rysuj osie
	line(0.1*W, y0, 0.9*W, y0);
	line(x0, 0.9*H, x0, 0.1*H);

	// Przeskaluj tablice na polozenia w pikselach
	for(int i = 0; i<n; ++i)
	{
		xp[i] = (x[i]-xmin)/(xmax-xmin)*0.8*W+0.1*W;
		yp[i] = (ymax-y[i])/(ymax-ymin)*0.8*H+0.1*H;
	}

	// Rysuj linie
	for(int i = 0; i<n-1; ++i)
	{
		line(xp[i], yp[i], xp[i+1], yp[i+1]);
	}

	// Wypisz etykiety osi
	if(Xlabel != NULL)
	outtextxy(0.85*W, y0+0.05*H, Xlabel);

	if(Ylabel != NULL)
	outtextxy(x0, 0.03*H, Ylabel);

	// Wydrukuj minima i maksima
	printf("min(x) = %lf\tmax(x) = %lf\nmin(y) = %lf\tmax(y) = %lf\n", xmin, xmax, ymin, ymax);

	// Stworz podzialke osi x
	double Lx, dx, dxn, wykl;
	Lx = xmax - xmin;
	dx = Lx/np;
	wykl = floor(log(dx)/log(10.));
	dxn = dx/(pow(10.,wykl));
	if(dxn <= 1.)
	{
		dxn = 1;
	}
	else if(dxn <= 2.)
	{
		dxn = 2;
	}
	else if(dxn <= 2.5)
	{
		dxn = 2.5;
	}
	else if(dxn < 4.0)
	{
		dxn = 4.0;
	}
	else if(dxn < 5.0)
	{
		dxn = 5.0;
	}
	else
	{
		dxn = 10.0;
	}
	dxn = dxn*pow(10.,wykl);

	// Stworz podzialke osi y
	double Ly, dy, dyn;
	Ly = ymax - ymin;
	dy = Ly/np;
	wykl = floor(log(dy)/log(10.));
	dyn = dy/(pow(10.,wykl));
	if(dyn <= 1.)
	{
		dyn = 1;
	}
	else if(dyn <= 2.)
	{
		dyn = 2;
	}
	else if(dyn <= 2.5)
	{
		dyn = 2.5;
	}
	else if(dyn < 4.0)
	{
		dyn = 4.0;
	}
	else if(dyn < 5.0)
	{
		dyn = 5.0;
	}
	else
	{
		dyn = 10.0;
	}
	dyn = dyn*pow(10.,wykl);

	dx = dxn;
	dy = dyn;

	// Rysuj linie podzialki
	int cont = 1;
	double a, x1, y1;
	a = ceil(xmin/dxn);
	x1 = a*dx;
	char buffer[32];

	while(cont)
	{
		line((x1-xmin)/(xmax-xmin)*0.8*W+0.1*W, y0-0.01*H, (x1-xmin)/(xmax-xmin)*0.8*W+0.1*W, y0+0.01*H);

		if(fabs(x1)<0.001*(xmax-xmin))
			x1 = 0;

		sprintf(buffer, "%g", x1);
		outtextxy((x1-xmin)/(xmax-xmin)*0.8*W+0.1*W, y0+0.01*H, buffer);
		x1 += dx;
		if(x1 > xmax)
		{
			cont = 0;
		}
	}

	cont = 1;
	a = ceil(ymin/dyn);
	y1 = a*dy;

	while(cont)
	{
		line(x0-0.01*W, 0.1*H+(ymax-y1)/(ymax-ymin)*0.8*H, x0+0.01*W, 0.1*H+(ymax-y1)/(ymax-ymin)*0.8*H);
		
		if(fabs(y1)<0.001*(ymax-ymin))
			y1 = 0;

		sprintf(buffer, "%g", y1);
		outtextxy(x0-0.09*W, 0.1*H+(ymax-y1)/(ymax-ymin)*0.8*H, buffer);
		y1 += dy;
		if(y1 > ymax)
		{
			cont = 0;
		}
	}	

	delete [] xp;
	delete [] yp;

	wait();
}