#include "../nltrans.h"
#include <math.h>
#include <graphics.h>
#include <mylib.h>
#include <std.h>



main(int argc, char **argv)
{
 C nltransform a(tensor222(.01,-.01,-.01,0,0,0,0,.001),
 				   matrix22(.8, .11, -.21, .9), vector2(1, -3), vector2(0,0));

 C nltransform b = inverse(a);
 cout << "the inverse of\n" << a << "\n is\n" << b << "\n";
 C nltransform c = b*a;
 cout << "Result of a*inverse(a):\n" << c << "\n";
 getkey();

 GrSetMode(GR_default_graphics);

  vector2 grc(0.5*GrMaxX(), 0.5*GrMaxY());
 C ltransform l(grc, grc+vector2(10,-20), matrix22(1.1, 0.2, -0.1, 0.9));

 C nltransform nl(argc>1 ? tensor222()
 				          : tensor222(1e-3,1e-4,1e-4,-3e-4, 0,-1e-4,-1e-4,3e-4),
 						l,
 						grc);
 C nltransform nls = inverse(nl);

 GrSetColor(1, 200, 100, 0);
 GrSetColor(2, 0, 100, 200);
 GrSetColor(3, 100, 100, 100);
 GrSetColor(4, 200, 0, 200);
 GrSetColor(0, 0, 0, 0);
 GrPlot(round(grc(0)), round(grc(1)), 4);
 GrPlot(round(grc(0))+1, round(grc(1)), 4);
 GrPlot(round(grc(0)), round(grc(1))+1, 4);
 GrPlot(round(grc(0))-1, round(grc(1)), 4);
 GrPlot(round(grc(0)), round(grc(1))-1, 4);

 int x;
 for(x=0;x<GrMaxX();x++)	{
	int y;
	for(y=0;y<GrMaxY();y++)
		if(x%20==0 || y%20==0)	{

			C vector2 tmp(x, y);

			C vector2 nlstmp = nls(nl(tmp));
			C vector2 nltmp = tmp;

			{
			C int nlx = round(nltmp(0));
			C int nly = round(nltmp(1));
			C int c = GrPixel(nlx, nly);
			if(c != 2)
				GrPlot(nlx, nly, c==0 ? 2 : 3);
			}
			{
			C int nlsx = round(nlstmp(0));
			C int nlsy = round(nlstmp(1));
			C int c = GrPixel(nlsx, nlsy);
			if(c != 1)
				GrPlot(nlsx, nlsy, c==0 ? 1 : 3);
			}
			}
 	}
 getkey();

 GrSetMode(GR_default_text);
 cout << "nl= " << nl << "\n";
 cout << "inverse(nl)= " << nls << "\n";
}
