From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Geometry/Sphere.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Runtime/Geometry/Sphere.cpp (limited to 'Runtime/Geometry/Sphere.cpp') diff --git a/Runtime/Geometry/Sphere.cpp b/Runtime/Geometry/Sphere.cpp new file mode 100644 index 0000000..19ddb3c --- /dev/null +++ b/Runtime/Geometry/Sphere.cpp @@ -0,0 +1,57 @@ +#include "UnityPrefix.h" +#include "Sphere.h" + +Sphere MergeSpheres (const Sphere& inSphere0, const Sphere& inSphere1); +float MergeSpheresRadius (const Sphere& inSphere0, const Sphere& inSphere1); + +void Sphere::Set (const Vector3f* inVertices, UInt32 inHowmany) +{ + m_Radius = 0.0F; + m_Center = Vector3f::zero; + UInt32 i; + for (i=0;i= fLSqr) + return fRDiff >= 0.0 ? inSphere1 : inSphere0; + + float fLength = sqrt (fLSqr); + const float fTolerance = 1.0e-06f; + Sphere kSphere; + + if (fLength > fTolerance) + { + float fCoeff = (fLength + fRDiff) / (2.0 * fLength); + kSphere.GetCenter () = inSphere0.GetCenter () + fCoeff * kCDiff; + } + else + { + kSphere.GetCenter () = inSphere0.GetCenter (); + } + + kSphere.GetRadius () = 0.5F * (fLength + inSphere0.GetRadius () + inSphere1.GetRadius ()); + + return kSphere; +} + +float MergeSpheresRadius (const Sphere& inSphere0, const Sphere& inSphere1) +{ + Vector3f kCDiff = inSphere1.GetCenter() - inSphere0.GetCenter(); + float fLSqr = SqrMagnitude (kCDiff); + float fRDiff = inSphere1.GetRadius () - inSphere0.GetRadius(); + + if (fRDiff*fRDiff >= fLSqr) + return fRDiff >= 0.0 ? inSphere1.GetRadius () : inSphere0.GetRadius (); + + float fLength = sqrt (fLSqr); + return 0.5F * (fLength + inSphere0.GetRadius () + inSphere1.GetRadius ()); +} -- cgit v1.1-26-g67d0