如何画出球壳?

时间:2016-02-21 05:42:37

标签: c# vector geometry drawing

我试图用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++;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分: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语句。我(愚蠢地)没有使用&amp;&amp;运算符,因为我认为它降低了可读性。