#include "stdafx.h"
#include "point.h"
#include <math.h>
#include <tuple>
NAMESPACE_POINT_BEGIN (NAMESPACE_POINT)

point::point() 
	:x(0) ,y(0)
{
}
point point::min_(const point&a,const point&b)
{
	return b<a?b:a;
}

point point::max_(const point&a,const point&b)
{
	return a<b?b:a;
}
void point::set(const point&p)
{
	x=p.x;
	y=p.y;
}
void point::set(double x_,double y_) 
{
	x=x_;
	y=y_;
}

void point::swap(point&p)
{
	point tmp=*this;
	*this=p;
	p=tmp;
}

//比较两个点的大小，先比较x，如果x相等，比较y
bool point::operator<(const point&p)const
{
	double c=x-p.x;
	if(c!=0) 
		return c<0;

	return y-p.y<0;
}

bool point::eq(double l,double r,double deta)
{
	return fabs(l-r)<=deta;
}

//如果无解
bool point::empty()const
{
	return x==0 && y==0;
}
bool point::invalid() const
{
	return (eq(x,0,1e-10) && eq(y,0,1e-10)) || (eq(x,-1000,1e-10) && eq(y,-1000,1e-10));
}

bool point::operator==(const point&r)const
{
	return eq(x,r.x,1e-10) && eq(y,r.y,1e-10);
}

double point::dist_direct(const point&o)const
{
	double d=dist(o);
	return o<*this?-d:d;
}

double point::dist_direct(double x,double y)const
{
	return dist_direct(point(x,y));

}

//计算两个点之间的距离
double point::dist(const point&o)const
{
	return dist(o.x,o.y);
}

//计算此点距离其他点的距离
double point::dist(double x,double y)const
{
	double dx=this->x-x;
	double dy=this->y-y;
	return sqrt(dx*dx+dy*dy);
}

double point::cos_k(const point&o)const
{
	double dx=o.x-x;
	double dy=o.y-y;
	return dx/sqrt(dx*dx+dy*dy);
}

double point::sin_k(const point&o)const
{
	double dx=o.x-x;
	double dy=o.y-y;
	return dy/sqrt(dx*dx+dy*dy);
}

point point::middle(const point&o)const
{
	return point((x+o.x)/2,(y+o.y)/2);
}

std::tuple<double,double,double> point::get_abc(const point&o)const
{
	double cos=fabs(cos_k(o));
	if(cos==0)
		return std::make_tuple(1,0,-x);

	if(cos==1)
		return std::make_tuple(0,1,-y);

	double dx=o.x-x;
	double dy=o.y-y;
	double k=dy/dx;
	double deno=sqrt(k*k+1);
	return std::make_tuple(k/deno,-1/deno,(y-k*x)/deno);
	//return std::make_tuple(k,-1,y-k*x);
}

NAMESPACE_POINT_END(NAMESPACE_POINT)
