2016-02-21 55 views
0

我想在C#中的球殼中排列點。我有代碼來安排一組點(我正在進行有限元分析),球體模式的半徑爲double earthRadius。我不知道如何做一個球殼like the type pictured here相同。想法?如何繪製球殼?

for (double x = -earthRadius; x < earthRadius; x += pointIncrement) //taken from http://stackoverflow.com/questions/8671385/sphere-drawing-in-java and slightly altered to make more efficient 
     { 
      for (double y = -earthRadius; y < earthRadius; y += pointIncrement) 
      { 
       for (double z = -earthRadius; z < earthRadius; z += pointIncrement) 
       { 
        if ((x * x) + (y * y) + (z * z) <= earthRadius * earthRadius) 
        { 
         earth.AddPoint(new Vector(x, y, z), 0); 
         totalPoints++; 
        } 
       } 
      } 
     } 
+0

我建議你循環[球形座標](https://en.wikipedia.org/wiki/Spherical_coordinate_system)來查找你的觀點。之後,將它們轉換爲笛卡爾座標。 – gdir

回答

0

我用笛卡爾座標計算出來。我選擇不使用球座標,因爲這是一個次優的解決方案:它使代碼不那麼優雅,並且使用Math.Sin()Math.Cos()會減慢它的速度。這是我得到的最終解決方案:

for (double x = -earthRadius; x < earthRadius; x += pointIncrement) //taken from http://stackoverflow.com/questions/8671385/sphere-drawing-in-java and slightly altered to make more efficient 
     { 
      for (double y = -earthRadius; y < earthRadius; y += pointIncrement) 
      { 
       for (double z = -earthRadius; z < earthRadius; z += pointIncrement) 
       { 
        if ((x * x) + (y * y) + (z * z) <= earthRadius * earthRadius) 
        { 

         if ((x * x) + (y * y) + (z * z) >= (earthRadius - shellThickness) * (earthRadius - shellThickness)) 
         { 

          earth.AddPoint(new Vector(x, y, z), 0); // need to figure out how to calculate masspoint 
          totalPoints++; 
         } 

        } 
       } 
      } 
     } 

請注意,我只是添加了另一個if語句。我沒有使用綽號,因爲它降低了可讀性。