簡體   English   中英

控制台3D立方體旋轉

[英]Console 3D Cube Rotating

在控制台中創建“ 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)東西foreach (char spinCube in spinCubeTest)地移動字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM