2016-02-21 54 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语句。我没有使用绰号,因为它降低了可读性。