title: Minimum Bounding Sphere for a Single 3D Triangle
body: <p><strong>--Original Post--</strong></p>
  
  <p>I'm trying to calculate the minimum bounding sphere for a 3-dimensional triangle.
  The triangle is given by the points point0, point1, point2.</p>
  
  <p>The code below was created from <a href="http://en.wikipedia.org/wiki/Circumscribed_circle#Barycentric_coordinates_from_cross-_and_dot-products" rel="nofollow">http://en.wikipedia.org/wiki/Circumscribed_circle#Barycentric_coordinates_from_cross-_and_dot-products</a></p>
  
  <p>For the sample input (0,0,10), (0,10,0), (10,0,0), the following code gives:</p>
  
  <p>radius = 8.164967, which is correct (verified by using Google Sketchup to draw a model).</p>
  
  <p>minSphereCenter = (10, 3.333..., 1.111...), which is incorrect.</p>
  
  <p>The correct center point would be (3.333..., 3.333..., 3.333...).</p>
  
  <hr>
  
  <p>What am I doing wrong?</p>
  
  <p>Note: I know that this will not give the minimum bounding sphere for obtuse triangles.</p>
  
  <p><strong>--Solution--</strong></p>
  
  <pre><code>class Triangle{
    Vector3f point0, point1, point2;
  
    Vector3f minSphereCenter;
  
    float minSphereRadius;
  
    private void calculateMinimumBoundingSphere() {
      minSphereRadius=
        (point0.distance(point1)*point1.distance(point2)*point2.distance(point0))/
                  (2*(new Vector3f().cross(new Vector3f().sub(point0, point1), new Vector3f().sub(point1, point2)).length()));
  
      float divisor=2*(new Vector3f().cross(new Vector3f().sub(point0, point1), new Vector3f().sub(point1, point2)).lengthSquared());
  
      float
        i=point1.distanceSquared(point2)*new Vector3f().sub(point0, point1).dot(new Vector3f().sub(point0, point2))/
          divisor,
  
        j=point0.distanceSquared(point2)*new Vector3f().sub(point1, point0).dot(new Vector3f().sub(point1, point2))/
          divisor,
  
        k=point0.distanceSquared(point1)*new Vector3f().sub(point2, point0).dot(new Vector3f().sub(point2, point1))/
          divisor;
  
        minSphereCenter=new Vector3f(point0).scale(i).add(new Vector3f(point1).scale(j)).add(new Vector3f(point2).scale(k));
  
        System.out.println(minSphereCenter);
        System.out.println(minSphereRadius);
  
      }
  </code></pre>