#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
	w = 800;
	h = 400;
	
	
	texGray.allocate(w,h,GL_LUMINANCE);
	
	
	counter = 0;
	ofBackground(255,0,102);
	bSmooth = true;
	ofSetWindowTitle("1D diffusion with explicit finite difference scheme");
	ofSetFrameRate(60);
	
	Diffuse = 1;
	spaceStep = .1;
	timeStep = .1;
	
	latticeSize = 500;
	stepSize = 1;
	steps = 1000;
	
	alpha = Diffuse * timeStep / spaceStep * spaceStep;
	
	for (i = 0; i < 1000; i++){
        u[i].x = i;
		u[i].y = 100.0;
	}
	u[499].y += 500.0;
	
	for (i = 0; i < 1000; i++){
        v[i].x = i;
		v[i].y = ofRandom(200.0, 700.0);
	}
	
}

//--------------------------------------------------------------
void testApp::update(){
	
	for (i = 1; i < 999; i++){
		unew[i] = u[i] + alpha*(u[i-1]+u[i-1]-2*u[i]);
	}
	for (i = 1; i < 999; i++){
		u[i] = unew[i];
	}
	
	for (i = 1; i < 999; i++){
		vnew[i] = v[i] + alpha*(v[i-1]+v[i-1]-2*v[i]);
	}
	for (i = 1; i < 999; i++){
		v[i] = vnew[i];
	}
	
}

//--------------------------------------------------------------
void testApp::draw(){
	
	
	ofSetColor(0xFFFFFF);
	ofNoFill();
	ofBeginShape();
	for (int i = 0; i < 1000; i++){
		ofVertex(u[i].x, u[i].y);
	}
	ofEndShape();
	
	ofSetColor(0xFFFFFF);
	ofNoFill();
	ofBeginShape();
	for (int i = 0; i < 1000; i++){
		ofVertex(v[i].x, v[i].y);
	}
	ofEndShape();
	
	string info = "diffusion constant: " + ofToString(Diffuse)+ "\n" + 
	"delta x: " + ofToString(spaceStep) + "\n" + 
	"delta t: " + ofToString(timeStep) + "\n"
	;
	ofFill();
	ofSetColor(0xFFFFFF);
	ofRect(10,10,300,70);
	ofSetColor(0x000000);
	ofDrawBitmapString(info,30,30);
	
	ofSaveFrame();
	
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
	
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
	
}

