#include "linerBox.h"


linerBox::linerBox(float width, float height){
	pts[0].set(0,0,0);
	pts[1].set(width,0,0);
	pts[2].set(width, height,0);
	pts[3].set(0,height,0);
	position.set(0,0,0);
	rotation = 0;
	updatePts();
}

//---------------------------------------------------------
void linerBox::draw(float offsetx, float offsety){
	ofEnableAlphaBlending();
	ofSetColor(255,0,0,80);
	glBegin(GL_LINE_LOOP);
		for (int i = 0; i < 4; i++){
			glVertex2f(ptsTranslated[i].x-offsetx,ptsTranslated[i].y-offsety);
		}
	glEnd();
	ofDisableAlphaBlending();
	
	//ofRect(startPoint.x-offsetx,startPoint.y-offsety,10,10);	
	//ofSetColor(255,0,0);
	//ofRect(startPointTranslated.x-offsetx,startPointTranslated.y-offsety,10,10);
}


//---------------------------------------------------------
void linerBox::updatePts(){
	ofVec3f mid = midpt();
	ofVec3f diffInPosition = position - mid;
	for (int i = 0; i < 4; i++){
		ptsTranslated[i] = pts[i] - mid;
		ptsTranslated[i].rotate(rotation);
		ptsTranslated[i] += diffInPosition + mid;
	}
	
	startPointTranslated = startPoint;
	startPointTranslated -= mid;
	startPointTranslated.rotate(rotation);
	startPointTranslated += diffInPosition + mid;
	
}



//---------------------------------------------------------
void linerBox::matchMyStartToPointAngle(ofVec3f point, float angle){
	
	float diffInAngle 	= angle - startAngle;
	rotation 			= diffInAngle;
	updatePts();
	
	// based on my rotation, recalculate my start point
	float pctx = startPoint.x / 640.0f;
	float pcty = 1.0f - (startPoint.y / 480.0f);
	ofVec3f pt = interpolatePt(ptsTranslated, pctx, pcty);
	

	ofVec3f diff 		= point - pt;
	position 			+= diff;
	
	
}
		

//---------------------------------------------------------
void linerBox::setStartPointAngle(ofVec3f start, float angle){
	startPoint = start;
	startAngle = angle;
}
		
//---------------------------------------------------------
ofVec3f 	linerBox::midpt(){
	ofVec3f midpt;
	midpt.set(0,0,0);
	for (int i = 0; i < 4; i++){
		midpt += pts[i];
	}
	midpt /= 4.0f;
	return midpt;
}


