Math/Sphere: Fix (Squared)Distance

Former-commit-id: d91c0d6e8a193e47c3ed45babaf3b83bf1cbe7ee
This commit is contained in:
Lynix 2015-06-17 23:35:06 +02:00
parent 80208b0dc5
commit 35066a3451
3 changed files with 25 additions and 6 deletions

View File

@ -68,14 +68,13 @@ bool NzSphere<T>::Contains(const NzVector3<T>& point) const
template<typename T>
T NzSphere<T>::Distance(T X, T Y, T Z) const
{
NzVector3<T> distance(X-x, Y-y, Z-z);
return distance.GetLength();
return Distance({X, Y, Z});
}
template<typename T>
T NzSphere<T>::Distance(const NzVector3<T>& point) const
{
return Distance(point.x, point.y, point.z);
return NzVector3f::Distance(point, GetPosition()) - radius;
}
template<typename T>
@ -249,14 +248,13 @@ NzSphere<T>& NzSphere<T>::Set(const NzSphere<U>& sphere)
template<typename T>
T NzSphere<T>::SquaredDistance(T X, T Y, T Z) const
{
NzVector3<T> distance(X-x, Y-y, Z-z);
return distance.GetSquaredLength();
return SquaredDistance({X, Y, Z});
}
template<typename T>
T NzSphere<T>::SquaredDistance(const NzVector3<T>& point) const
{
return SquaredDistance(point.x, point.y, point.z);
return NzVector3f::Distance(point, GetPosition()) - radius * radius;
}
template<typename T>

View File

@ -103,12 +103,15 @@ class NzVector3
static NzVector3 Backward();
static NzVector3 CrossProduct(const NzVector3& vec1, const NzVector3& vec2);
static T DotProduct(const NzVector3& vec1, const NzVector3& vec2);
static T Distance(const NzVector3& vec1, const NzVector3& vec2);
static float Distancef(const NzVector3& vec1, const NzVector3& vec2);
static NzVector3 Down();
static NzVector3 Forward();
static NzVector3 Left();
static NzVector3 Lerp(const NzVector3& from, const NzVector3& to, T interpolation);
static NzVector3 Normalize(const NzVector3& vec);
static NzVector3 Right();
static T SquaredDistance(const NzVector3& vec1, const NzVector3& vec2);
static NzVector3 Unit();
static NzVector3 UnitX();
static NzVector3 UnitY();

View File

@ -567,6 +567,18 @@ NzVector3<T> NzVector3<T>::Backward()
return vector;
}
template<typename T>
T NzVector3<T>::Distance(const NzVector3& vec1, const NzVector3& vec2)
{
return vec1.Distance(vec2);
}
template<typename T>
float NzVector3<T>::Distancef(const NzVector3& vec1, const NzVector3& vec2)
{
return vec1.Distancef(vec2);
}
template<typename T>
NzVector3<T> NzVector3<T>::Down()
{
@ -615,6 +627,12 @@ NzVector3<T> NzVector3<T>::Right()
return vector;
}
template<typename T>
T NzVector3<T>::SquaredDistance(const NzVector3& vec1, const NzVector3& vec2)
{
return vec1.SquaredDistance(vec2);
}
template<typename T>
NzVector3<T> NzVector3<T>::Unit()
{