控制台 3D 立方体旋转

发布于 2024-10-14 09:54:02 字数 61 浏览 3 评论 0原文

在控制台中创建“3D”立方体并旋转它(掷骰子风格)的好算法是什么?

有创意的答案将不胜感激。

What is a good algorithm for creating an "3D" cube in Console and rotating it (dice roll style)?

Creative answers would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-10-21 09:54:02

我不知道这是否符合您正在寻找的“算法”,但您始终可以进行完整的 3D 计算。

用于绘制立方体线框的示例代码(我使用 OpenTK 中的矢量/矩阵类,您也可以从 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();
        }
    }
}   

来自输出的示例帧:

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

I don't know if this qualifies as an "algorithm" you'd be looking for, but you could always just do the full 3D calculations.

Example code for drawing the cube wireframe (I'm using the Vector/Matrix classes from OpenTK, you could get them from XNA or some other library as well)

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

Sample Frames from Output:

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

奈何桥上唱咆哮 2024-10-21 09:54:02

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

I don't think there's a good "3D cube ascii art algorithm." I would just use a 3D cube animation - which you could create at design time or run time - and use a normal ASCII art generator.

柒七 2024-10-21 09:54:02

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

you could create a while loop which displays an array of characters generating a 3D ascii animation of the cube until you want it to stop. use something like foreach (char spinCube in spinCubeTest) to move the characters accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文