如何生成/计算十二面体的顶点?

use*_*772 5 c# algorithm graphics

如何在算法上生成十二面体的顶点?

我想要四面体的质心(0, 0, 0).

nib*_*bot 8

来自维基百科:

下面的笛卡尔坐标定义了以原点为中心的十二面体的顶点,并且适当地缩放和定向:

(±1,±1,±1)

(0,±1 /φ,±φ)

(±1 /φ,±φ,0)

(±φ,0,±1 /φ)

其中φ=(1 +√5)/ 2是黄金比例(也写成τ)≈1.618.边长为2 /φ=√5-1.包含球的半径为√3.

我发现这个描述比一大块C#代码更简洁,更有信息.( - :


Sup*_*est 6

由于这个问题现在是 Google 搜索的最高结果(数学上的骗局是 #2),我想我不妨添加一些代码。

下面是一个完整的控制台程序,应该可以编译和运行,并且通常是不言自明的。

该算法基于维基百科文章(谢谢,来自 math.stackoverflow.com 的 mt_

此代码应该为您打印正确的顶点列表。您主要关心的是 method Program.MakeDodecahedron,但是不要只是复制和粘贴它,因为您需要修改它以使用您自己的顶点数据结构而不是我的模拟Vertex对象。您可以轻松使用XNA 的 Vector3,它的构造函数与我的Vertex. 也因为我的Vertex.ToString方法很老套,这个程序在使用时可能会打印一个丑陋的输出表,Vector3所以请记住这一点。

另外,请注意,这是一个(不完美的)演示。例如,如果生成许多四面体,您将不必要地为每次调用重新计算常数(例如黄金比例)。

使用 XNA,尤其是使用 时Microsoft.Xna.Framework,您还可以轻松地以 3D 形式渲染十二面体。为此,您可以修改本教程中的代码。

using System;
using System.Collections.Generic;

namespace DodecahedronVertices
{
    class Program
    {
        static void Main()
        {
            // Size parameter: This is distance of each vector from origin
            var r = Math.Sqrt(3);

            Console.WriteLine("Generating a dodecahedron with enclosing sphere radius: " + r);

            // Make the vertices
            var dodecahedron = MakeDodecahedron(r);

            // Print them out
            Console.WriteLine("       X        Y        Z");
            Console.WriteLine("   ==========================");
            for (var i = 0; i < dodecahedron.Count; i++)
            {
                var vertex = dodecahedron[i];
                Console.WriteLine("{0,2}:" + vertex, i + 1);
            }

            Console.WriteLine("\nDone!");
            Console.ReadLine();
        }

        /// <summary>
        /// Generates a list of vertices (in arbitrary order) for a tetrahedron centered on the origin.
        /// </summary>
        /// <param name="r">The distance of each vertex from origin.</param>
        /// <returns></returns>
        private static IList<Vertex> MakeDodecahedron(double r)
        {
            // Calculate constants that will be used to generate vertices
            var phi = (float)(Math.Sqrt(5) - 1) / 2; // The golden ratio

            var a = 1 / Math.Sqrt(3);
            var b = a / phi;
            var c = a * phi;

            // Generate each vertex
            var vertices = new List<Vertex>();
            foreach (var i in new[] { -1, 1 })
            {
                foreach (var j in new[] { -1, 1 })
                {
                    vertices.Add(new Vertex(
                                        0,
                                        i * c * r,
                                        j * b * r));
                    vertices.Add(new Vertex(
                                        i * c * r,
                                        j * b * r,
                                        0));
                    vertices.Add(new Vertex(
                                        i * b * r,
                                        0,
                                        j * c * r));

                    foreach (var k in new[] { -1, 1 })
                        vertices.Add(new Vertex(
                                            i * a * r,
                                            j * a * r,
                                            k * a * r));
                }
            }
            return vertices;
        }
    }

    /// <summary>
    /// A placeholder class to store data on a point in space. Don't actually use this, write a better class (or just use Vector3 from XNA).
    /// </summary>
    class Vertex
    {
        double x;
        double y;
        double z;

        public Vertex(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public override string ToString()
        {
            var s = String.Format("{0,8:F2},{1,8:F2},{2,8:F2}", x, y, z);

            return s;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我的代码可能非常冗长且分散,我建议您阅读支持折叠 for 循环和其他代码结构的内容。