合并两个球体以获得一个新球体

时间:2015-11-04 21:59:05

标签: math

如何计算封装另外两个球体的最小球体?

每个球体在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;
};

1 个答案:

答案 0 :(得分:3)

我讨厌glm,所以这里只是数学。

假设您的两个球体具有半径r1, r2和中心c1, c2。封闭球体的中心C半径R

enter image description here

如果球体相互包围:

|c1 - c2| + r1 < r2反之亦然,采取更大的范围。

否则从图中可以看出

  • R = (r1 + r2 + |c1 - c2|) / 2
  • C = c1 + (c2 - c1) * (R - r1) / |c2 - c1|(线性插值)

其中c1, c2, C向量,而|v|是向量v的大小。