控制台3D立方体旋转

时间:2011-01-25 19:59:11

标签: c# console rotation cube

在Console中创建“3D”立方体并旋转它(掷骰子样式)有什么好算法?

我们将不胜感激。

3 个答案:

答案 0 :(得分:4)

我不知道这是否有资格作为您正在寻找的“算法”,但您可以随时进行完整的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();
        }
    }
}   

输出中的示例帧:

                        #
                     ######
                 ####   #  ##
              ####      #   ##
           ####         #     ##
        ###             #      ##
      ##                #       ###
      ###               #         ##
      # ##              #           ##
      #   ##            #          ###
      #    ##           #       ###  #
      #      ##         #    ###     #
      #       ##        #####        #
      #         ##    ###            #
      #          #####  #            #
      #            #    #            #
      #            #    #            #
      #            #    #            #
      #            #    #            #
      #            #    #            #
      #            #    #            #
      #            #  #####          #
      #            ###    ##         #
      #        #####        ##       #
      #     ###    #         ##      #
      #  ###       #           ##    #
      ###          #            ##   #
      ##           #              ## #
        ##         #               ###
         ###       #                ##
           ##      #             ###
            ##     #         ####
              ##   #      ####
               ##  #   ####
                 ######
                   #
               #
              ######
            ## #    ###
           ##  #       ###
         ##    #          ####
        ##     #              ####
      ##       #                 ##
     ##        #                ###
    #          #               ## #
   ##          #             ##   #
   # ####      #            ##    #
   #     ####  #           #      #
   #        ####         ##       #
   #           #####   ##         #
   #           #    ####          #
   #           #      #           #
   #           #      #           #
   #           #      #           #
   #           #      #           #
   #           #      #           #
   #           #      #           #
   #          ####    #           #
   #         ##   #####           #
   #       ##         ####        #
   #      #           #  ####     #
   #    ##            #      #### #
   #   ##             #          ##
   # ##               #          #
   ###                #        ##
   ##                 #       ##
    ####              #     ##
        ####          #    ##
            ###       #  ##
               ###    # ##
                  ######
                      #
          #
          ##############
          #             ##########
         ##                     ##
         ##                     ##
        ###                    ###
        # #                    # #
        # #                    # #
       ## #                   ## #
       #  #                   #  #
      ##  #                   #  #
      ##############         #   #
      #   #         ##########   #
      #   #                  #   #
      #   #                  #   #
      #   #                  #   #
      #   #                  #   #
      #   #                  #   #
      #   #                  #   #
      #   #                  #   #
      #   #                  #   #
      #   ##########         #   #
      #   #         ##############
      #  #                   #  ##
      #  #                   #  #
      # ##                   # ##
      # #                    # #
      # #                    # #
      ###                    ###
      ##                     ##
      ##                     ##
      ##########             #
                ##############
                             #

答案 1 :(得分:1)

我认为没有一个好的“3D立方体ascii艺术算法”。我只想使用3D立方体动画 - 您可以在设计时或运行时创建它 - 并使用普通的ASCII艺术生成器。

答案 2 :(得分:0)

你可以创建一个while循环,它显示一个字符数组,生成立方体的3D ascii动画,直到你希望它停止。使用foreach (char spinCube in spinCubeTest)之类的东西来相应地移动字符。