如何生成/计算十二面体的顶点? [英] How to generate/calculate vertices of dodecahedron?

查看:124
本文介绍了如何生成/计算十二面体的顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能算法地生成十二面体的顶点?

How can I algorithmically generate the vertices of dodecahedron?

我希望四面体的质心位于(0,0,0)

I would like the tetrahedron's centroid to be at (0, 0, 0).

推荐答案

由于现在的问题是Google搜索的最高结果(数学问题是#2),我想我可能会添加一些代码。

Since the question is now the top result for a Google search (the dupe at math is #2), I figured I might as well add some code.

完整的控制台程序位于下方,应该进行编译和运行,并且一般不需要进行解释。

A full console program is below and should compile and run and be generally self-explanatory.

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

这段代码应该为您打印一个正确的顶点列表。你关心的主要是方法 Program.MakeDodecahedron ,但是不要复制和粘贴它,因为你需要修改它来使用你自己的顶点数据结构,而不是我的模拟顶点对象。您可以轻松使用 XNA的Vector3 ,它具有与我的 Vertex 完全相同的签名的构造函数。另外,因为我的 Vertex.ToString 方法很不方便,所以当与 Vector3 一起使用时,该程序可能会打印一个丑陋的输出表,因此保留记住。

This code should print a correct list of vertices for you. Your concern is mostly with the method Program.MakeDodecahedron, however don't just copy and paste it because you need to modify this to use your own vertex data structure instead of my mock Vertex object. You could easily use XNA's Vector3, which has a constructor with the exact same signature as my Vertex. Also because my Vertex.ToString method is hacky, this program may print an ugly output table when used with Vector3 so keep that in mind.

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

Also, note that this is a(n imprefect) demonstration. For instance, if generating many tetrahedra, you would be needlessly recalculating constants (such as the golden ratio) for each call.

使用XNA,特别是如果使用 Microsoft.Xna.Framework ,你也可以很容易地用3D渲染你的十二面体。您可以为此调整本教程中的代码。

With XNA, especially if you use Microsoft.Xna.Framework, you can also easily render your dodecahedron in 3D. You can adapt the code from this tutorial for this purpose.

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;
        }
    }
}

详细和分散,我建议阅读它支持for循环和其他代码结构的折叠。

As my code is probably quite verbose and spread out, I'd recommend reading it in something which supports folding of for-loops and other code structures.

这篇关于如何生成/计算十二面体的顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆