Sphere::Distance now returns distance between center and point

No more negative distances


Former-commit-id: 1688b82e88621e20312b6b28090ed63941b3e02a
This commit is contained in:
Lynix 2013-07-31 14:18:27 +02:00
parent fc56553021
commit 84ebd6043c
1 changed files with 7 additions and 7 deletions

View File

@ -44,7 +44,7 @@ NzSphere<T>::NzSphere(const NzSphere<U>& sphere)
template<typename T> template<typename T>
bool NzSphere<T>::Contains(T X, T Y, T Z) const bool NzSphere<T>::Contains(T X, T Y, T Z) const
{ {
return Distance(X, Y, Z) < F(0.0); return SquaredDistance(X, Y, Z) <= radius*radius;
} }
/* /*
template<typename T> template<typename T>
@ -62,7 +62,7 @@ template<typename T>
T NzSphere<T>::Distance(T X, T Y, T Z) const T NzSphere<T>::Distance(T X, T Y, T Z) const
{ {
NzVector3<T> distance(X-x, Y-y, Z-z); NzVector3<T> distance(X-x, Y-y, Z-z);
return distance.GetLength() - radius; return distance.GetLength();
} }
template<typename T> template<typename T>
@ -74,10 +74,10 @@ T NzSphere<T>::Distance(const NzVector3<T>& point) const
template<typename T> template<typename T>
NzSphere<T>& NzSphere<T>::ExtendTo(T X, T Y, T Z) NzSphere<T>& NzSphere<T>::ExtendTo(T X, T Y, T Z)
{ {
T distance = Distance(X, Y, Z); T distance = SquaredDistance(X, Y, Z);
if (distance > F(0.0)) if (distance > radius*radius)
radius += distance; radius = std::sqrt(distance);
return *this; return *this;
} }
@ -122,7 +122,7 @@ bool NzSphere<T>::Intersect(const NzBox<T>& box) const
template<typename T> template<typename T>
bool NzSphere<T>::Intersect(const NzSphere& sphere) const bool NzSphere<T>::Intersect(const NzSphere& sphere) const
{ {
return Distance(sphere.x, sphere.y, sphere.z) <= sphere.radius; return SquaredDistance(sphere.x, sphere.y, sphere.z) - radius*radius <= sphere.radius*sphere.radius;
} }
template<typename T> template<typename T>
@ -210,7 +210,7 @@ template<typename T>
T NzSphere<T>::SquaredDistance(T X, T Y, T Z) const T NzSphere<T>::SquaredDistance(T X, T Y, T Z) const
{ {
NzVector3<T> distance(X-x, Y-y, Z-z); NzVector3<T> distance(X-x, Y-y, Z-z);
return distance.SquaredLength() - radius*radius; return distance.SquaredLength();
} }
template<typename T> template<typename T>