#include "cardGemoetry.h"



//-----------------------------
void cardGemoetry::identifyShortestSide(card &theCard){
	int dist = 999999;
	int pta	 = 0;
	int ptb  = 0;
	
	for(int i = 0; i < 4; i++){
		for(int j = 0; j < 4; j++){
			if(i == j)continue;
		
			float d = sqrt( pow(theCard.c[i].x - theCard.c[j].x , 2) + pow(theCard.c[i].y - theCard.c[j].y , 2) );
			
			if(d < dist){
				pta = i;
				ptb = j;
				dist = d;
			}
		 }
	}
		
	theCard.pta = pta;
	theCard.ptb = ptb;
	
}

//-----------------------------
void cardGemoetry::calculatePointOrder(card &theCard){
	
	// a new approach
	//
	// 1. find the shortest side
	// 2. find the next shortest side
	//    we should have the two ends now
	// 3. find the highest end
	// 4. from the highest end look for the point where 
	//    the point to its left (anti clockwise) is from 
	//	  the other end - this is now point A
	// 5. next go to the other point on the highest end
	//    this is now point B 
	// 6. now go clockwise from there 
	
	int pta, ptb, ptc, ptd = 0;
		
	// 1. find the shortest side
	identifyShortestSide(theCard);
	int shortest1 = theCard.pta;
	int shortest2 = theCard.ptb;

	//2. find the second shortest side
	int shortest3, shortest4 = -1;
	
	for(int i = 0; i < 4; i++){
		if( i == shortest1 || i == shortest2) continue;
		else shortest3 = i;
		break;
	}
	for(int i = 0; i < 4; i++){
		if( i == shortest1 || i == shortest2 || i == shortest3) continue;
		else shortest4 = i;
		break;
	}
	
	//3. find the highest end
	bool isFirstPair = false;
	if( ( MIN(theCard.c[shortest1].y, theCard.c[shortest2].y) < MIN(theCard.c[shortest3].y, theCard.c[shortest4].y) ) )isFirstPair = true;
	
	//we need to swap the points over if the second pair are the highest points
	if(!isFirstPair){
		int s1, s2;
		
		s1		  = shortest1;
		s2		  = shortest2;
		shortest1 = shortest3;
		shortest2 = shortest4;
		shortest3 = s1;
		shortest4 = s2;
	}
	
	//4. find the point whose previous point is from the other end
	//our contour goes counter clockwise we actaully check the next point 	
	int prevPt1 = shortest1 + 1;
	if(prevPt1 == 4)prevPt1 = 0;

	//if the point before shortest1 is one of the points from the other end then shortest1 is our pta
	if( theCard.c[prevPt1] == theCard.c[shortest3] || theCard.c[prevPt1] == theCard.c[shortest4]){
		pta = shortest1;
		ptb = shortest2;
		ptc = shortest4;
		ptd = shortest3;
	}else{
		pta = shortest2;
		ptb = shortest1;
		ptc = shortest4;
		ptd = shortest3;
	}
	
	theCard.pta = pta;
	theCard.ptb = ptb;
	theCard.ptc = ptc;
	theCard.ptd = ptd;	
	
	//finally lets sort the pts 
	// c[0] to be pta  
	// c[1] to be ptb  etc...
	//
	ofxVec2f tmpArray[4];
	tmpArray[0] = theCard.c[pta];
	tmpArray[1] = theCard.c[ptb];
	tmpArray[2] = theCard.c[ptc];
	tmpArray[3] = theCard.c[ptd];
	
	memcpy(theCard.c, tmpArray, sizeof(ofxVec2f)*4);
	
	theCard.pta = 0;
	theCard.ptb = 1;
	theCard.ptc = 2;
	theCard.ptd = 3;
	
	theCard.computeSqPoints();
}

//-----------------------------
void cardGemoetry::calculateCenter(card &theCard){
	float avgX = 0;
	float avgY = 0;
	
	for(int i = 0; i < 4; i ++){
		avgX += theCard.c[i].x;
		avgY += theCard.c[i].y;				
	}
	
	avgX /= 4;
	avgY /= 4;
	
	theCard.center.x = avgX;
	theCard.center.y = avgY;
}

//-----------------------------
void cardGemoetry::calculateAngle(card &theCard){
	float midPointX = 0.5 * theCard.c[ theCard.pta ].x + 0.5 * theCard.c[ theCard.ptb ].x; 
	float midPointY = 0.5 * theCard.c[ theCard.pta ].y + 0.5 * theCard.c[ theCard.ptb ].y; 
		
	float dx = midPointX - theCard.center.x;
	float dy = midPointY - theCard.center.y;
		
	float angle = atan2(dy, dx);
	ofxVec2f angleVec(dx, dy);
	angleVec.normalize();
	
	theCard.angle		= angle;
	theCard.angleVec	= angleVec; 
}	

//-----------------------------
void cardGemoetry::calculateGeometry(card &theCard){
	calculatePointOrder( theCard );
	calculateCenter(theCard);
	calculateAngle(theCard);
}



								
				