#include "AABB.h"

bool AABBi::AABBvsCircleCollisionTest(const AABBi &box, const Vector2i &circlePos, const int &circleRadius, Vector2i &closestPoint)
{
	closestPoint = circlePos;
	if(circlePos.x < box.Left)
		closestPoint.x = box.Left;
	else if(circlePos.x > box.Right)
		closestPoint.x = box.Right;

	if(circlePos.y < box.Bottom)
		closestPoint.y = box.Bottom;
	else if(closestPoint.y > box.Top)
		closestPoint.y = box.Top;

	Vector2i diffVec = closestPoint - circlePos;
	if(diffVec.getLengthSqrd() > circleRadius * circleRadius)
		return false;
	else
		return true;
}

bool AABBi::AABBvsAABBCollisionTest(const AABBi &boxA, const AABBi &boxB)
{
	if(boxA.Top < boxB.Bottom)
		return false;
	if(boxA.Bottom > boxB.Top)
		return false;
	if(boxA.Left > boxB.Right)
		return false;
	if(boxA.Right < boxB.Left)
		return false;

	return true;
}

eAABBSideQueryResult AABBi::GetSideFromClosestPoint(const AABBi &box, const Vector2i &point, Vector2i* pSidePointA, Vector2i* pSidePointB)
{
	eAABBSideQueryResult qResult = eInsideAABB;

	if(box.Left == point.x)
	{
		if(pSidePointA)
			*pSidePointA = Vector2i(box.Left, box.Top);
		if(pSidePointB)
			*pSidePointB = Vector2i(box.Left, box.Bottom);
		qResult = eLeftSide;
	}
	else if(box.Right == point.x)
	{
		if(pSidePointA)
			*pSidePointA = Vector2i(box.Right, box.Top);
		if(pSidePointB)
			*pSidePointB = Vector2i(box.Right, box.Bottom);
		qResult = eRightSide;
	}
	else if(box.Top == point.y)
	{
		if(pSidePointA)
			*pSidePointA = Vector2i(box.Left, box.Top);
		if(pSidePointB)
			*pSidePointB = Vector2i(box.Right, box.Top);
		qResult = eTopSide;
	}
	else if(box.Bottom == point.y)
	{
		if(pSidePointA)
			*pSidePointA = Vector2i(box.Left, box.Bottom);
		if(pSidePointB)
			*pSidePointB = Vector2i(box.Right, box.Bottom);
		qResult = eBottomSide;
	}

	return qResult;
}
