/**
 * \file Abscisse.cpp
 * \date 24.04.2004
 * \author Pavol Misik
 * \brief Implementation of the Abscisse class.
 * 
 */
#include "Abscisse.h"

/** \brief Konstruktor usecky z dvoch bodov
 * \param p1 Bod1
 * \param p2 Bod2
 */
Abscisse::Abscisse(const Point &p1, const Point &p2): Line(p1, p2)
{
	ptBegin = p1;
	ptEnd = p2;
	SetRange(ptBegin, ptEnd);
}

/** \brief Destruktor */
Abscisse::~Abscisse()
{
}

/** \brief Zisti, ci bod p lezi na usecke
 *  \param p Testovany bod.
 *  \return \li true Ak bod lezi na usecke
 *			\li fakse Ak bod nelezi na usecke
 */
bool Abscisse::IsOnAbscisse(const Point &p)
{
	if (this->fB == 0.0)		// otocim suradnicovy system
	{
		Point tP = Point(p.GetY(), p.GetX());
		Abscisse a(Point(ptBegin.GetY(), ptBegin.GetX()), Point(ptEnd.GetY(), ptEnd.GetX()));
		
		Point ptOnLine;
		ptOnLine.SetX(tP.GetX());
		ptOnLine.SetY(GetY(ptOnLine.GetX()));
	
	
		if (tP.GetY() != ptOnLine.GetY())
			return false;
		if (a.left <= tP.GetX()   && tP.GetX()<=a.right &&
			a.bottom <= tP.GetY() && tP.GetY() <= a.top)
		{
			return true;
		}
		return false;
	}
	
	Point ptOnLine;
	ptOnLine.SetX(p.GetX());
	ptOnLine.SetY(GetY(ptOnLine.GetX()));
	
	
	if (p.GetY() != ptOnLine.GetY())
		return false;
	if (left <= p.GetX()   && p.GetX()<=right &&
		bottom <= p.GetY() && p.GetY() <= top)
	{
		return true;
	}
	return false;
}

/** \brief Nastavi interne rozssahy pre usecku (lavu, pravu dolnu a hornu max hodnotu
 *	\param p1 Krajny bod usecky.
 *  \param p2 Krajny bod usecky.
 */
void Abscisse::SetRange(const Point& p1, const Point& p2)
{
	if (p1.GetX()<= p2.GetX())
	{
		left  = p1.GetX();
		right = p2.GetX();
	}
	else
	{
		right  = p1.GetX();
		left = p2.GetX();
	}
	
	if (p1.GetY()<=p2.GetY())
	{
		bottom = p1.GetY();
		top = p2.GetY();
	}
	else
	{
		top = p1.GetY();
		bottom = p2.GetY();
	}
}

/** \brief Vrati rovnobeznu usecku s touto useckou, ktora prechadza bodom p
 *  \param p Bod, ktorim ma prechadzat usecka
 *  \return Rovnobezna usecka s touto useckou, ktora prechadza bodom p
 */
Abscisse Abscisse::ParallelPerp(const Point &p)const
{
	Point p1, p2;
	Line ln1 = Perpendicular(ptBegin);
	Line ln2 = Perpendicular(ptEnd);
	Line paralel = Parallel(p);


	paralel.Intercept(ln1, p1);
	paralel.Intercept(ln2, p2);
	return Abscisse(p1, p2);
}

/*
#include <stdio.h>
void main()
{

	Point p1(0.0f, 0.0f);
	Point p2(4.0f, 4.0f);
	
	Abscisse a1(p1, p2);

	Point p3(5.0f, 5.0f);
	Point p4(2.0f, 2.0f);
	Point p5(2.0f, 7.0f);
	Point p6(7.0f, 2.0f);
	
	if (a1.IsOnAbscisse(p3))
		printf("\nBod p3 lezi na usecke");
	else 
		printf("\nBod p3 nelezi na usecke");

	if (a1.IsOnAbscisse(p4))
		printf("\nBod p4 lezi na usecke");
	else 
		printf("\nBod p4 nelezi na usecke");
	
	if (a1.IsOnAbscisse(p5))
		printf("\nBod p5 lezi na usecke");
	else 
		printf("\nBod p5 nelezi na usecke");
	
	if (a1.IsOnAbscisse(p6))
		printf("\nBod p6 lezi na usecke");
	else 
		printf("\nBod p6 nelezi na usecke");

	Point p7(1.0f, 0.0f);
	Abscisse a2 = a1.ParallelPerp(p7);

	Point ball(0.0f, 0.0f);
	Point frnd(0.0f, 12.37f);
	Point enemyPos(0.0f, 10.54f);
	Point enemy2Pos(-1.26f, 9.06f);

	Line l3(ball, frnd);
	Abscisse a3(ball, frnd);
	
	Abscisse p = a3.ParallelPerp(enemyPos);
	
	float maxDist = 2.0f;
	float minDist = a3.DistanceToPoint(enemyPos);
	
	bool onLine = p.IsOnAbscisse(enemyPos);
	
	if (minDist<maxDist && onLine==true)
		{
			printf("\nJe");
		}
		else
		{
			printf("\nNieJe");
		}

	bool onLine2 = p.IsOnAbscisse(enemy2Pos);
	
	if (minDist<maxDist && onLine2==true)
		{
			printf("\nJe");
		}
		else
		{
			printf("\nNieJe");
		}

}

*/
