2016-05-25 9 views
1

私はプログラミングのこの非常に新しいですが、今私は多くの球(最大36、最小3)の中心を計算するプログラムを作成する必要がある、各球は4点X、Y、Zを持っています。4点で球中心を計算するには?

:これは球1のためのポイントの私の最初のセットは次のようであることを意味し、この

bolas[n].xyz[row,element] 

のような構造で、私のプログラムは、リスト内のポイントデータのiストレージそれでTXTファイルを読み込む原因となります私は、Javaコードを変換し、実装するいくつかのいずれかを見つけ

bolas[0].xyz[0,0] 

ウェブ上serching:

bolas[0] = 
row0. -> [0] [1] [2] 
row1. -> [0] [1] [2] 
row2. -> [0] [1] [2] 
row3. -> [0] [1] [2] 

はので、私は球1で私の行1からXの値を使用したい場合は、私はこれをしなければなりませんそれはC#球の中心を計算するために、彼は1つのクラスを作成しましたが、私は非常に新しく、クラスの要素をどのように使用するかわからず、データをクラスに導入する必要があります。ここでクラスである:私は私のデータ球数を変量することができるが、私は36球各4と点X、Y、Zの最大値を有することを特徴とする方法

/// <summary> 
/// Given four points in 3D space, solves for a sphere such that all four points 
/// lie on the sphere's surface. 
/// </summary> 
/// <remarks> 
/// 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. 
/// </remarks> 
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 } 
      }; 

    /// <summary> 
    /// The centre of the resulting sphere. 
    /// </summary> 
    public double[] Centre 
    { 
     get { return new double[] { this.m_X0, this.m_Y0, this.m_Z0 }; } 
    } 

    /// <summary> 
    /// The radius of the resulting sphere. 
    /// </summary> 
    public double Radius 
    { 
     get { return this.m_Radius; } 
    } 

    /// <summary> 
    /// Whether the result was a valid sphere. 
    /// </summary> 
    public bool Valid 
    { 
     get { return this.m_Radius != 0; } 
    } 

    /// <summary> 
    /// Computes the centre of a sphere such that all four specified points in 
    /// 3D space lie on the sphere's surface. 
    /// </summary> 
    /// <param name="a">The first point (array of 3 doubles for X, Y, Z).</param> 
    /// <param name="b">The second point (array of 3 doubles for X, Y, Z).</param> 
    /// <param name="c">The third point (array of 3 doubles for X, Y, Z).</param> 
    /// <param name="d">The fourth point (array of 3 doubles for X, Y, Z).</param> 
    public CircumcentreSolver(double[] a, double[] b, double[] c, double[] d) 
    { 
     this.Compute(a, b, c, d); 
    } 

    /// <summary> 
    /// Evaluate the determinant. 
    /// </summary> 
    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 < 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 < 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 < 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 < 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 < 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); 
     } 
    } 

    /// <summary> 
    /// Recursive definition of determinate using expansion by minors. 
    /// </summary> 
    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 < n; j1++) // Do each column. 
      { 
       for (i = 1; i < n; i++) // Create minor. 
       { 
        j2 = 0; 
        for (j = 0; j < 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; 
    } 
} 

。私は十分にそれを明確に説明願ってい

ballCent[0]= 
center-> [0][1][2] //center of the sphere[x][y][z]. 
radius-> [0]  //radius of the sphere. 

、私は英語のネイティブスピーカーではないよ、私は本当にの助けに感謝:私はストレージ多分何かのような別の一覧で生じたセンターは、できれば それは非常に有用であろうコミュニティー。 PS。私は個人的に私のデータを使ってプログラムのJava版を試してみました。 ここにリンク: http://www.convertalot.com/sphere_solver.html

+0

これは簡単な作業ではありません。基本的には、4変数と4式で非線形システムを解く必要があります。その問題を最初に解決する方法を研究する必要があります。 – InBetween

+0

インターネットでコードを見つけて、それを理解しようとせずに単に動作させようとするのは、間違いなく道のりです。 – InBetween

+0

コーディングを忘れて、どうやってこれを手で解決しますか?それを手動で行う方法がわからない場合は、どのように動作中のアルゴリズムをプログラムするふりをしますか?手動でそれを行う方法を理解すれば、それをどのようにプログラムするか、必要なツールを考え始めることができます。誰もここであなたの仕事をするつもりはありません。 – InBetween

答えて

-1

最初の2つの点を使用して線を定義します。後の2つと同じことをしてください。 2つの線の間のPOIを探します。

+0

3次元空間では、2点間の等距離の範囲は線ではなく平面です。 –

+0

@EdwardPetersあなたはどういうことを言っていますか?どの次元空間でも、2つの点には明確な線があります。 – sdgdf

+0

**の間の線**は明確に定義されていますが、有用ではありません。球の中心がその線上に置かれる理由はない、またはあなたが描く2本の線が交差することはない)代わりに、等距離の**点がある点を考慮したい。 4つの入力点が球の中心になります。 –

関連する問題