带python的等轴测ascii立方体

2024-05-29 08:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

(抱歉我的语言不通)

我是Python的初学者,但我别无选择,我需要它作为一个项目,而对于这个项目,我必须通过编程创建ascii等轴测立方体。 我真的不知道该怎么做,所以我从寻找“角”的坐标(不是正确的词而是…)开始画一个瓷砖

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

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

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

我开始做的事

^{pr2}$

我找到了一些协调人,但他们在某种意义上是错的。我有点困惑。 我已经开始用瓷砖了,但这次我真的不知道怎么做。。。 有什么想法吗?在


Tags: the项目for编程asciietcwhatexpect
2条回答

我建议使用recursive函数重建脚本。 这样你就可以基本上去掉坐标,因为你可能会从中间开始建立(然后向下)。另外,你可以水平地拆分立方体,因为其中一半很容易适应另一半。在

我很快就想出了一些东西。它接受立方体宽度和高度的参数。因为边的坡度可能不同,所以它不能很好地处理不同的坡度;它只对倾斜的边使用句点字符(对垂直边使用管道)。代码如下:

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)

哪个打印:

^{pr2}$

相关问题 更多 >

    热门问题