如何计算封装其他两个球体的最小球体?
每个球体在 3d 空间中都有一个中心点和一个半径。
编辑:
这是我的代码。我正在尝试实现 merge() 函数,但我不知道如何实现。
#include <gl\glm\glm.hpp>
class Sphere
{
public:
Sphere();
Sphere(const glm::vec3 &point, float radius);
void set(const glm::vec3 &point, float radius);
void reset();
bool isReset() const;
const glm::vec3& getCenter() const { return _point; }
float radius() const { return _radius; }
void merge(const Sphere &other);
bool operator==(const Sphere &other) const {
return (_point == other._point && _radius == other._radius);
}
bool operator!=(const Sphere &other) const {
return !operator==(other);
}
private:
glm::vec3 _point;
float _radius;
};
Run Code Online (Sandbox Code Playgroud)
小智 6
嗯,我讨厌glm,所以这里只是数学。
假设你的两个球体有半径r1, r2和中心c1, c2。封闭球体的中心C半径为R:
如果球体相互包围:
|c1 - c2| + r1 < r2 反之亦然,取更大的球体。
否则从图中可以清楚地看出
R = (r1 + r2 + |c1 - c2|) / 2C = c1 + (c2 - c1) * (R - r1) / |c2 - c1| (线性插值)其中c1, c2, C是向量,是向量|v|的大小v。