#include "lineRecorder.h"


//---------------------------------------------------------
lineRecorder::lineRecorder(){
	nPts = 0;
}

//---------------------------------------------------------
void lineRecorder::addPoint(ofVec3f newPt){
	pts[nPts%MAX_NUM_LINE_RECORDER_PTS].set(newPt);
	nPts++;
}

//---------------------------------------------------------
void lineRecorder::draw(float offsetx, float offsety){
	ofSetColor(0xffffff);
	
	int start, end;
	if (nPts < MAX_NUM_LINE_RECORDER_PTS) { start = 0; end = nPts;}
	else {
		start = nPts;
		end = nPts +  MAX_NUM_LINE_RECORDER_PTS;
	}
	
	glBegin(GL_LINE_STRIP);
	for (int i = start; i < end; i++){
		glVertex2f(pts[i%MAX_NUM_LINE_RECORDER_PTS].x - offsetx, pts[i%MAX_NUM_LINE_RECORDER_PTS].y - offsety);
	}
	glEnd();
}	

//---------------------------------------------------------
float lineRecorder::getCurrentAngle(){	
	if (nPts > 3){
		int currentPt = (nPts - 1) % MAX_NUM_LINE_RECORDER_PTS;
		int prevPt = (nPts - 2) % MAX_NUM_LINE_RECORDER_PTS;
		ofVec3f diff = pts[currentPt] - pts[prevPt];
		return atan2(diff.y, diff.x);
	} else {
		return 0;
	}
}

//---------------------------------------------------------
ofVec3f lineRecorder::getCurrentPoint(){
	if (nPts > 0){
		int currentPt = (nPts - 1) % MAX_NUM_LINE_RECORDER_PTS;
		return ofVec3f(pts[currentPt]);
	} else {
		return ofVec3f (0,0,0);
	}
}