Ejemplo n.º 1
0
// Helper function to compute the minimal circle that contains the given three points.
// To avoid extra Sqrt() operations in hot inner loops, this function returns a Circle2D structure that has its radius squared (caller needs to compute circle.r = Sqrt(circle.r) to use)
// This is essentially a fast version of Circle2D::OptimalEnclosingCircle(a, b, c)
static Circle2D MakeCircleSq(float AB, float AC, const float2 &ab, const float2 &ac)
{
	const float AB_AC = Dot(ab,ac);
	float denom = AB*AC - AB_AC*AB_AC;
	if (Abs(denom) < 1e-5f) // Each of a, b and c lie on a straight line?
	{
		if (AB_AC > 0.f) return AB > AC ? Circle2D(ab*0.5f, AB*0.25f) : Circle2D(ac*0.5f, AC*0.25f);
		else return Circle2D((ab+ac)*0.5f, (AB + AC - 2.f*AB_AC)*0.25f);
	}
	denom = 0.5f / denom;
	float s = (AC * AB - AB_AC * AC) * denom;
	if (s < 0.f) return Circle2D(ac * 0.5f, AC * 0.25f);
	else
	{
		float t = (AC * AB - AB_AC * AB) * denom;
		if (t < 0.f) return Circle2D(ab * 0.5f, AB * 0.25f);
		else if (s + t > 1.f) return Circle2D((ab + ac) * 0.5f, (AB + AC - 2.f*AB_AC)*0.25f);
		else
		{
			const float2 center = s * ab + t * ac;
			return Circle2D(center, center.LengthSq());
		}
	}
}
Ejemplo n.º 2
0
float2 float2::ProjectTo(const float2 &direction) const
{
	assume(!direction.IsZero());
	return direction * this->Dot(direction) / direction.LengthSq();
}
Ejemplo n.º 3
0
float float2::AngleBetween(const float2 &other) const
{
	return acos(Dot(other)) / Sqrt(LengthSq() * other.LengthSq());
}
Ejemplo n.º 4
0
bool float2::IsPerpendicular(const float2 &other, float epsilonSq) const
{
	float dot = Dot(other);
	return dot*dot <= epsilonSq * LengthSq() * other.LengthSq();
}