用Python绘制等距ASCII立方体

5

对不起我的语言表达有些破碎。

我是Python的初学者,但我别无选择,我需要它来完成一个项目,在这个项目中我必须通过编程创建ascii等距立方体。我不知道该如何做,所以我先从想法开始,找到“角落”的坐标(不是正确的词)来绘制瓷砖。

#what I expect really :
- for a 2 wide 
        .-⁻``⁻-.
    .-⁻`        `⁻-.
    |              |
    |              |
    `⁻-.        .-⁻`
        `⁻-..-⁻`    
- for 3 wide
            .-⁻``⁻-.
        .-⁻`        `⁻-.
    .-⁻`                `⁻-.
    |                      |
    |                      |
    `⁻-.                .-⁻`
        `⁻-.        .-⁻`
            `⁻-..-⁻`

# what I except for the beginning
- 2 wide
        .-⁻``⁻-.
    .-⁻`        `⁻-.
    `⁻-.        .-⁻`
        `⁻-..-⁻`

- 3 wide (,etc.)
            .-⁻``⁻-.
        .-⁻`        `⁻-.
    .-⁻`                `⁻-.
    `⁻-.                .-⁻`
        `⁻-.        .-⁻`
            `⁻-..-⁻`

我开始做的事情

#! /usr/bin/env python
import numpy as np
x = 2 // number of tiles
y = 2 // idem
z = 2 // elevation, not used yet.
w = 4 // wideness of a tile (.-⁻` ---> 4 characters)
s = range ( x * y ) // just to apply a number to a corner
c = 0 // counter

def makeMatrix ( x, y ):
   matrix = np.full ( y*2*h+z, x*2*w), '.', dtype=str )
   return matrix

def getOut ():
   global x, y, w, h, c
   for i in range ( int(x) ):
      for j in range ( int(y) ):
         cx = ( j - i ) * w
         cy = ( j + i )
         dec = w
         cx += dec
         matrix[cy][cx] = str ( s[c] )
         c += 1
   return matrix

matrix = makeMatrix ( x, y )
print ( getOut () )

我找到了一些坐标,但从某种意义上说它们是错误的。我有点困惑。 我已经使用瓦片进行过操作,但这次我不太清楚该怎么做... 你有什么想法吗?


你可以将这个东西渲染成标准图像,然后转换成ASCII艺术。请查看我的图像转换为ASCII艺术 - Spektre
2个回答

1

这是我快速编写的一些内容。它接受立方体的宽度和高度参数。由于边缘的斜率可能不同,因此它不能很好地处理不同的斜率;它只是使用句点字符表示倾斜的边缘(以及垂直边缘的管道符号)。以下是代码:

from __future__ import division # For Python 2: make integer division produce float results. (Otherwise the cube is mangled.)
from math import sqrt

def draw_cube(width, height):
    cube = [[' ']*width for row in range(height)]
    vertices = {
        'tc': (width//2, 0),
        'tl': (0, int(.25*height)),
        'tr': (width-1, int(.25*height)),
        'cc': (width//2, int(.5*height)),
        'bl': (0, int(.75*height)),
        'br': (width-1, int(.75*height)),
        'bc': (width//2, height-1)
    }
    edges = (
        ('tc', 'tl'),
        ('tc', 'tr'),
        ('tl', 'cc'),
        ('tl', 'bl'),
        ('tr', 'cc'),
        ('tr', 'br'),
        ('bl', 'bc'),
        ('br', 'bc'),
        ('cc', 'bc')
    )

    for edge in edges:
        v1 = vertices[edge[0]]
        v2 = vertices[edge[1]]
        x1 = v1[0]
        y1 = v1[1]
        x2 = v2[0]
        y2 = v2[1]
        if x1 > x2: # Always moving left to right
            x1, x2 = x2, x1
            y1, y2 = y2, y1
        try:
            m = (y2-y1)/(x2-x1)
        except ZeroDivisionError:
            c = '|'
            for yy in range(min(y1, y2), max(y1, y2)):
                cube[yy][x1] = c
        else:
            c = '.'
            yy = y1
            for xx in range(x1, x2):
                cube[int(yy)][xx] = c
                yy += m

    cube_str = '\n'.join(''.join(row) for row in cube)
    return cube_str

x = draw_cube(40,20)
print(x)

这会打印什么:

                 .......                
             ....       ....            
         ....               ....        
     ....                       ....    
 ....                               ... 
|...                                ...|
|   ....                        ....   |
|       ....                ....       |
|           ....        ....           |
|               .... ...               |
|                   |                  |
|                   |                  |
|                   |                  |
|                   |                  |
|                   |                  |
......              |              .... 
      .....         |         .....     
           .....    |    .....          
                ....|....               
                    .                   

-2
我建议使用递归函数重建脚本。这样,您将能够大部分摆脱坐标,因为您可能会从中间开始构建(然后向下)。此外,您可以水平分割立方体,因为其中一半相对容易适应另一半。

嗨,Martijn Luyckx。我想我明白你的意思。我查看了递归函数的链接,构建帕斯卡三角形的函数可能很有趣,但是,除了演示只创建一行并将其放入循环以超过递归限制之外,我不知道如何为我的项目实现它。我认为坐标很有用,因为我解释了“立方体”的目的,但它必须适应2x3块等。啊,第一次我这么困惑!!! - krshk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接