我怎样才能算法生成十二面体的顶点?
我想四面体的重心是在(0, 0, 0)
我怎样才能算法生成十二面体的顶点?
我想四面体的重心是在(0, 0, 0)
由于这个问题现在是谷歌搜索的顶部结果(在数学方面的重复数据删除技术是#2),我想我也可以添加一些代码。
一个完整的控制台程序是下面,应该编译和运行,并通常不言自明。
该算法是基于维基百科的文章 (感谢你, mt_从math.stackoverflow.com )
此代码应打印您顶点的正确的列表。 您的关注是大多与方法Program.MakeDodecahedron
,但不只是复制和粘贴,因为你需要修改这个使用自己的顶点数据结构,而不是我的模拟的Vertex
对象。 你可以很容易地使用XNA的的Vector3 ,它具有完全相同的签名我的构造Vertex
。 也因为我的Vertex.ToString
方法是哈克,当用于该程序可以打印一个丑陋的输出表Vector3
所以记住这一点。
此外,请注意,这是一个(N imprefect)演示。 例如,如果产生许多四面体,你会为每个呼叫不必要地重新计算的常数(如黄金比例)。
随着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;
}
}
}
由于我的代码可能过于冗长,传播出去,我建议你的东西,它支持for循环和其他代码结构的折叠阅读它。
从维基百科 :
下面笛卡尔坐标限定十二面体以原点为中心,并适当地缩放和取向的顶点:
(±1,±1,±1)
(0,±1 /φ,±φ)
(±1 /φ,φ±,0)
(±φ,0,±1 /φ)
其中,φ=(1 +√5)/ 2是黄金比例(也写作τ)≈1.618。 边长为2 /φ=√5 - 1.含有球具有√3的半径。
我发现,这种描述方式比一大块的C#代码更简洁,内容翔实。 ( - :
以P为中心的十二面体的直角坐标(0,0,0),由半径为r的球体限定顶点由下式给出:
P(±R /√3,±R /√3,±R /√3)
P(0,±R /(√3* Z),±(R * Z)/√3)
P(±R /(√3* Z),±(R * Z)/√3,0)
P(±(R * Z)/√3,0,±R /(√3* Z))
其中,φ=(1 +√5)/ 2是黄金比例(也写作τ)≈1.618。
下面是在0顶点为中心的黎曼表面立体投影。 (对不起,我无法找到如何发布的数学符号)
其中T是黄金比例,让= 1 / T ^ 2,让复共轭对B + -IC用b = SQRT来定义(5)/ 4和c = SQRT(3)/ 4。 0,120和240度旋转,这三点,所以你现在有9分,所有的单位圆内。
每一个点映射到使用地图z中的单位圆外的图像 - > -1 / Z。 在零和无穷添加一个点,你现在有十二面体的所有顶点。
如果你希望你的球体上的十二面体,做平常立体背地图,使得单位圆走上赤道。 通过刻写的一般过程,这个也给你一个顶点为中心的立方体或四面体,但分别由约37.76或22.24度旋转。