2015-11-04 34 views
-4

如何計算封裝另外兩個球體的最小球體?合併兩個球體以獲得新球體

每個球體在三維空間和半徑中都有一箇中心點。

編輯:

這是我的代碼。我試圖實現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

這不是特定語言和太寬泛。顯示你的代碼。 – Olaf

+1

將數學轉化爲代碼需要幫助嗎?如果你確實向我們展示了你到目前爲止所擁有的東西。如果你需要數學方面的幫助,你幾乎肯定會問錯誤的地方。 –

+0

我編輯了我的帖子。這不應該太難,所以我想我應該問這裏。 – Pilpel

回答

2

那麼我討厭glm,所以這裏只是數學。

假設你的兩個球體有半徑r1, r2和中心c1, c2。包圍球具有中心半徑CR

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的幅度。

+0

我認爲有些情況下,這可能會出錯。當兩個球體具有相同的中心點時,R會出錯。即'sphere1 =(vec3(0,0,0),10.0f); sphere2 =(vec3(0,0,0),20.0f);'。 R出來15應該是20. – Pilpel

+0

@Pilpel啊是的你的權利。將編輯。 – 2015-11-05 11:28:38