#include <psychlops.h>
using namespace Psychlops;	// Initially developed with Psychlops Win32 1.0.2 / 20080414
/*
void drawGaussianFilter(Image &img, double sigma, double factor)
{
	img.release();
	img.set(sigma*8, sigma*8, Image::RGBA);
	const int width = img.getWidth(), height = img.getHeight();
	double xp, yp, r, r2;
	for(int y=0; y<height; y++) {
		yp = y-height/2.0;
		for(int x=0; x<width; x++) {
			xp = x-width/2.0;
			r = sqrt(xp*xp+yp*yp);
			r2 = -(r*r) / (2.0*sigma*sigma);
			img.alpha(x,y,factor*(exp(r2)));
		}
	}
}

void psychlops_main() {
	Canvas cnvs(1024,768,Canvas::window);

	Group gr;

	// Attach filter to Group
	Image filter;
	drawGaussianFilter( filter, 100, 1 );
	filter.centering(0,0);
	gr.clip(&filter);

	// Append 'filtered' Figures
	Psychlops::Rectangle dot[100];
	for(int i=0; i<100; i++) {
		dot[i].fill = Color::white;
		dot[i].set(5,5).centering(random(-300,300),random(-300,300)).join(gr);
	}

	cnvs.clear();
	gr.centering().draw();
	cnvs.flip();

	while(!Keyboard::esc.pushed()) {}

}
*/

const double E = 2.718281828459045;
const double LOG2E = 1.44269504088896340736;
double sigmoid(double x, double d, double a) { return 1 / ( 1 + pow(E, -(a*(x-d))) ); }
double log2NormalDistibution(double log_x, double octave_mu, double octave_sigma) { return Math::normalDistibution(log(log_x)*LOG2E, octave_mu, octave_sigma); }
double cumulativeLog2NormalDistibution(double log_x, double octave_mu, double octave_sigma) { return Math::cumulativeNormalDistibution(log(log_x)*LOG2E, octave_mu, octave_sigma); }


double bind(double x) {
	return cumulativeLog2NormalDistibution(x, 2, 1.15/2);
}
double bind2(double x) {
	return 1-cumulativeLog2NormalDistibution(x, 1, 1.15/2);
}

void psychlops_main() {
	Canvas cnvs(1024,768,Canvas::window);

	Figures::FunctionalPlot plot(256,256);
	//plot.append(&bind);
	//plot.append(&bind2);
	//plot.x_max=20; plot.x_min=0.0000001; plot.y_max=1.1; plot.y_min=-0.1;
	plot.append(&sin);
	plot.x_max=10; plot.x_min=-10; plot.y_max=1.1; plot.y_min=-1.1;
	plot.draw();
	cnvs.flip();

	while(!Keyboard::esc.pushed()) {}
}


/*
void psychlops_main() {
	Canvas sampleA(Canvas::fullscreen);

	Image img(200,200);
	img.clear(Color::blue);
	img.rect(Psychlops::Rectangle(50,0,150,100),Color::green); // Draw rect to upper side
	img.centering().draw();


	Psychlops::Rectangle rect(500,500);
	Image target;

	sampleA.to(target, rect.centering());
	rect.draw(Color(1,0,0,0.2));	// show source area of Canvas::to


	sampleA.flip();	// If Canvas::flip has been done bofore Canvas::to, Canvas::to won't work as you intended.
	while(!Input::get(Keyboard::esc));

	// writing outline of the image
	target.line(0, 0, 0, target.getHeight(), Color::red);
	target.line(0, 0, target.getWidth(), 0, Color::red);
	target.line(0, 0, target.getWidth(), target.getHeight(), Color::red);

	sampleA.clear();
	target.draw();
	sampleA.flip();
	while(!Input::get(Keyboard::esc));
}
*/

/*
Psychlops::Rectangle rect1(1279,1023); Psychlops::Rectangle rect2;
Psychlops::Image Stimulus1(rect1); Psychlops::Image Stimulus2(rect1);
Psychlops::Color col(0.8);

void psychlops_main() {
	Canvas sampleA(Canvas::fullscreen);
	for(int y=0; y<10; y++) {
		rect2.set(0, y*100+25, 1279, y*100+20+25);
		Stimulus1.rect(rect2, col);
	}
	Stimulus1.draw();
	sampleA.flip();
	while(!Input::get(Keyboard::esc));

	sampleA.flip();

	rect2.set(0,0,500,500);//x座標が指定できていない
	//rect2.set(0,0,1680,500);//上下反転？
	//rect2.set(0,500,1680,1000);//OK

	sampleA.to(Stimulus2, rect2);
	Stimulus2.draw();
	sampleA.flip();
	while(!Input::get(Keyboard::esc));
}

*/
