float CL_LineMath::closest_point_relative( const CL_Pointf &P, const CL_Pointf &A, const CL_Pointf &B)
{
	if( A == B )
		return 1.0;

	float u = ((P.x - A.x)*(B.x - A.x) + (P.y - A.y)*(B.y - A.y)) / (cl_pow2(B.x-A.x) + cl_pow2(B.y-A.y));

	if( u < 0.0 ) return 0.0;
	if( u > 1.0 ) return 1.0;

	return u;
}
int CL_LineSegment2x<int>::point_distance(const CL_Vec2i &point)
{
	int L = cl_pow2(q.x-p.x) + cl_pow2(q.y-p.y);
	int r = ((point.x-p.x)*(q.x-p.x)+(point.y-p.y)*(q.y-p.y)) / L;
	
	if( r <= 0 || r >= 1 )
	{
		return cl_min( point.distance(p), point.distance(q) );
	}
	
	int s = ((p.y-point.y)*(q.x-p.x)-(p.x-point.x)*(q.y-p.y)) / L;

	s *= (int) (sqrt((float) L) + 0.5f);

	if (s < 0)
		s = -s;

	return s;
}
Type CL_LineSegment2x<Type>::point_distance(const CL_Vec2<Type> &point)
{
	Type L = cl_pow2(q.x-p.x) + cl_pow2(q.y-p.y);
	Type r = ((point.x-p.x)*(q.x-p.x)+(point.y-p.y)*(q.y-p.y)) / L;
	
	if( r <= 0 || r >= 1 )
	{
		return cl_min( point.distance(p), point.distance(q) );
	}
	
	Type s = ((p.y-point.y)*(q.x-p.x)-(p.x-point.x)*(q.y-p.y)) / L;

	s *= sqrt(L);

	if (s < ((Type)0))
		s = -s;

	return s;
}