title: How to calculate the sphere center with 4 points?
body: <p>The thing is that i am very new on this of programming and now i need to make a program which calculate the center of many spheres(maximum 36, minimum 3), each sphere has 4 points X,Y,Z. be cause my program read a TXT file with the points data i storage it in a List, with an structure like this </p>
  
  <pre><code>bolas[n].xyz[row,element]
  </code></pre>
  
  <p>this means that my first set of points for the sphere 1 is like this:</p>
  
  <pre><code>bolas[0] = 
   row0.  -&gt; [0] [1] [2]
   row1.  -&gt; [0] [1] [2]
   row2.  -&gt; [0] [1] [2]
   row3.  -&gt; [0] [1] [2]
  </code></pre>
  
  <p>so if i want to use the value of X from my row 1 in the sphere one i have to do this:</p>
  
  <pre><code>bolas[0].xyz[0,0]
  </code></pre>
  
  <p>serching on the web i found some one who transform a java code and implement it for c# to calculate the center of the sphere, he created one Class but i am very new and i have no idea how to use the elements on his class, how should i introduce my data into his class and how do i get the result; here is the class:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Given four points in 3D space, solves for a sphere such that all four points
  /// lie on the sphere's surface.
  /// &lt;/summary&gt;
  /// &lt;remarks&gt;
  /// Translated from Javascript on http://www.convertalot.com/sphere_solver.html, originally
  /// linked to by http://stackoverflow.com/questions/13600739/calculate-centre-of-sphere-whose-surface-contains-4-points-c.
  /// &lt;/remarks&gt;
  public class CircumcentreSolver
  {
      private const float ZERO = 0;
      private double m_X0, m_Y0, m_Z0;
      private double m_Radius;
      private double[,] P = 
              {
                  { ZERO, ZERO, ZERO },
                  { ZERO, ZERO, ZERO },
                  { ZERO, ZERO, ZERO },
                  { ZERO, ZERO, ZERO }
              };
  
      /// &lt;summary&gt;
      /// The centre of the resulting sphere.
      /// &lt;/summary&gt;
      public double[] Centre
      {
          get { return new double[] { this.m_X0, this.m_Y0, this.m_Z0 }; }
      }
  
      /// &lt;summary&gt;
      /// The radius of the resulting sphere.
      /// &lt;/summary&gt;
      public double Radius
      {
          get { return this.m_Radius; }
      }
  
      /// &lt;summary&gt;
      /// Whether the result was a valid sphere.
      /// &lt;/summary&gt;
      public bool Valid
      {
          get { return this.m_Radius != 0; }
      }
  
      /// &lt;summary&gt;
      /// Computes the centre of a sphere such that all four specified points in
      /// 3D space lie on the sphere's surface.
      /// &lt;/summary&gt;
      /// &lt;param name="a"&gt;The first point (array of 3 doubles for X, Y, Z).&lt;/param&gt;
      /// &lt;param name="b"&gt;The second point (array of 3 doubles for X, Y, Z).&lt;/param&gt;
      /// &lt;param name="c"&gt;The third point (array of 3 doubles for X, Y, Z).&lt;/param&gt;
      /// &lt;param name="d"&gt;The fourth point (array of 3 doubles for X, Y, Z).&lt;/param&gt;
      public CircumcentreSolver(double[] a, double[] b, double[] c, double[] d)
      {
          this.Compute(a, b, c, d);
      }
  
      /// &lt;summary&gt;
      /// Evaluate the determinant.
      /// &lt;/summary&gt;
      private void Compute(double[] a, double[] b, double[] c, double[] d)
      {
          P[0, 0] = a[0];
          P[0, 1] = a[1];
          P[0, 2] = a[2];
          P[1, 0] = b[0];
          P[1, 1] = b[1];
          P[1, 2] = b[2];
          P[2, 0] = c[0];
          P[2, 1] = c[1];
          P[2, 2] = c[2];
          P[3, 0] = d[0];
          P[3, 1] = d[1];
          P[3, 2] = d[2];
  
          // Compute result sphere.
          this.Sphere();
      }
  
      private void Sphere()
      {
          double r, m11, m12, m13, m14, m15;
          double[,] a =
                  {
                      { ZERO, ZERO, ZERO, ZERO },
                      { ZERO, ZERO, ZERO, ZERO },
                      { ZERO, ZERO, ZERO, ZERO },
                      { ZERO, ZERO, ZERO, ZERO }
                  };
  
          // Find minor 1, 1.
          for (int i = 0; i &lt; 4; i++)
          {
              a[i, 0] = P[i, 0];
              a[i, 1] = P[i, 1];
              a[i, 2] = P[i, 2];
              a[i, 3] = 1;
          }
          m11 = this.Determinant(a, 4);
  
          // Find minor 1, 2.
          for (int i = 0; i &lt; 4; i++)
          {
              a[i, 0] = P[i, 0] * P[i, 0] + P[i, 1] * P[i, 1] + P[i, 2] * P[i, 2];
              a[i, 1] = P[i, 1];
              a[i, 2] = P[i, 2];
              a[i, 3] = 1;
          }
          m12 = this.Determinant(a, 4);
  
          // Find minor 1, 3.
          for (int i = 0; i &lt; 4; i++)
          {
              a[i, 0] = P[i, 0] * P[i, 0] + P[i, 1] * P[i, 1] + P[i, 2] * P[i, 2];
              a[i, 1] = P[i, 0];
              a[i, 2] = P[i, 2];
              a[i, 3] = 1;
          }
          m13 = this.Determinant(a, 4);
  
          // Find minor 1, 4.
          for (int i = 0; i &lt; 4; i++)
          {
              a[i, 0] = P[i, 0] * P[i, 0] + P[i, 1] * P[i, 1] + P[i, 2] * P[i, 2];
              a[i, 1] = P[i, 0];
              a[i, 2] = P[i, 1];
              a[i, 3] = 1;
          }
          m14 = this.Determinant(a, 4);
  
          // Find minor 1, 5.
          for (int i = 0; i &lt; 4; i++)
          {
              a[i, 0] = P[i, 0] * P[i, 0] + P[i, 1] * P[i, 1] + P[i, 2] * P[i, 2];
              a[i, 1] = P[i, 0];
              a[i, 2] = P[i, 1];
              a[i, 3] = P[i, 2];
          }
          m15 = this.Determinant(a, 4);
  
          // Calculate result.
          if (m11 == 0)
          {
              this.m_X0 = 0;
              this.m_Y0 = 0;
              this.m_Z0 = 0;
              this.m_Radius = 0;
          }
          else
          {
              this.m_X0 = 0.5 * m12 / m11;
              this.m_Y0 = -0.5 * m13 / m11;
              this.m_Z0 = 0.5 * m14 / m11;
              this.m_Radius = System.Math.Sqrt(this.m_X0 * this.m_X0 + this.m_Y0 * this.m_Y0 + this.m_Z0 * this.m_Z0 - m15 / m11);
          }
      }
  
      /// &lt;summary&gt;
      /// Recursive definition of determinate using expansion by minors.
      /// &lt;/summary&gt;
      private double Determinant(double[,] a, double n)
      {
          int i, j, j1, j2;
          double d = 0;
          double[,] m = 
                  {
                      { ZERO, ZERO, ZERO, ZERO },
                      { ZERO, ZERO, ZERO, ZERO },
                      { ZERO, ZERO, ZERO, ZERO },
                      { ZERO, ZERO, ZERO, ZERO }
                  };
  
          if (n == 2)
          {
              // Terminate recursion.
              d = a[0, 0] * a[1, 1] - a[1, 0] * a[0, 1];
          }
          else
          {
              d = 0;
              for (j1 = 0; j1 &lt; n; j1++) // Do each column.
              {
                  for (i = 1; i &lt; n; i++) // Create minor.
                  {
                      j2 = 0;
                      for (j = 0; j &lt; n; j++)
                      {
                          if (j == j1) continue;
                          m[i - 1, j2] = a[i, j];
                          j2++;
                      }
                  }
  
                  // Sum (+/-)cofactor * minor.
                  d = d + System.Math.Pow(-1.0, j1) * a[0, j1] * this.Determinant(m, n - 1);
              }
          }
  
          return d;
      }
  }
  </code></pre>
  
  <p>How i said my data sphere numbers may variate, but i have a maximum of 36 spheres each one with 4 points x,y,z.
  It will be very useful if i could storage the resultant centers in another List, maybe something like:</p>
  
  <pre><code>ballCent[0]=
  center-&gt; [0][1][2] //center of the sphere[x][y][z].
  radius-&gt; [0]       //radius of the sphere.
  </code></pre>
  
  <p>i hope i explain it clear enough,i am not an English native speaker, i really appreciate the help of the community. 
  PS. i personally tried the java version of the program with my data and it works just perfect for me. 
  here is the link:
  http://www.convertalot.com/sphere_solver.html</p>