在控制台中创建“3D”立方体并旋转它(掷骰子风格)的好算法是什么?
创造性的答案将不胜感激。
我不知道这是否符合您正在寻找的“算法”,但您总是可以只进行完整的 3D 计算。
绘制立方体线框的示例代码(我使用的是 OpenTK 的 Vector/Matrix 类,您也可以从 XNA 或其他库中获取它们)
using System;
using System.Linq;
using OpenTK;
class Cubes
{
static void Main() {
var resolution = 25;
var points = from i in Enumerable.Range(1, 8) select new Vector3(i / 4 % 2 * 2 - 1, i / 2 % 2 * 2 - 1, i % 2 * 2 - 1);
var lines = from a in points
from b in points
where (a - b).Length == 2 // adjacent points
&& a.X + a.Y + a.Z > b.X + b.Y + b.Z // select each pair once
select new { a, b };
var t = 0f;
while (true) {
t += .1f;
var projection = Matrix4.CreatePerspectiveFieldOfView(.8f, 1, .01f, 100f);
var view = Matrix4.LookAt(2 * new Vector3((float)Math.Sin(t), .5f, (float)Math.Cos(t)), Vector3.Zero, Vector3.UnitY);
Console.Clear();
foreach (var line in lines) {
for (int i = 0; i < resolution; i++) {
var point = (1f / resolution) * (i * line.a + (resolution - 1 - i) * line.b); // interpolate a point between the two corners
var p1 = 5 * Vector3.Transform(point, view * projection) + new Vector3(30, 20, 0);
Console.SetCursorPosition((int)p1.X, (int)p1.Y);
Console.Write("#");
}
}
Console.ReadKey();
}
}
}
来自输出的样本帧:
#
######
#### # ##
#### # ##
#### # ##
### # ##
## # ###
### # ##
# ## # ##
# ## # ###
# ## # ### #
# ## # ### #
# ## ##### #
# ## ### #
###### ##
####
####
####
####
####
####
########
# ### ## #
# ##### ###
# ### # ## #
# ### # ## #
### # ## #
## # ## #
## # ###
### # ##
## # ###
## # ####
## # ####
## # ####
######
#
#
######
## # ###
## # ###
## # ####
## # ####
## # ##
## # ###
#####
## # ## #
# #### # ## #
# #### # # #
# #### ## #
# ##### ###
#######
####
####
####
####
####
####
# #### # #
# ## ##### #
# ## #### #
########
# ## # #### #
# ## # ##
#####
### # ##
## # ##
#### # ##
#### # ##
### # ##
### # ##
######
#
#
##############
###########
## ##
## ##
### ###
####
####
## # ## #
####
## # # #
################
## ###########
####
####
####
####
####
####
####
####
#############
## ##############
#####
####
# ## # ##
####
####
### ###
## ##
## ##
###########
##############
#
我不认为有一个好的“3D 立方体 ascii 艺术算法”。我只会使用 3D 立方体动画 - 您可以在设计时或运行时创建 - 并使用普通的 ASCII 艺术生成器。
您可以创建一个 while 循环,该循环显示一组字符,生成立方体的 3D ascii 动画,直到您希望它停止。使用类似的东西foreach (char spinCube in spinCubeTest)相应地移动字符。