5

(sorry for my broken language)

I'm a beginner in Python, but I have no choice, I need it for a project, and for this one I have to create ascii isometric cube by programming. I don't know really how to do it, so I began with the idea to find coordinates of "corners" (not the right word but...) to draw a tile

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

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

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

What I began to do

#! /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 () )

I find some coordonates, but they are wrong in a sense. I'm a bit confused. I already work with tiles but I don't really know how to do it this time... Any idea ?

krshk
  • 353
  • 1
  • 2
  • 8
  • you could render the thing into standard image and then convert to ASCII art see my [Image to ASCII art conversion](http://stackoverflow.com/a/32987834/2521214) – Spektre Apr 05 '16 at 12:38

2 Answers2

1

Here's something I whipped up quickly. It takes arguments for the width and height of the cube. Because the slope of the edges can differ, it does not gracefully handle different slopes; it just uses a period character for the slanted edges (and a pipe for vertical edges). Here's the code:

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)

Which prints:

                 .......                
             ....       ....            
         ....               ....        
     ....                       ....    
 ....                               ... 
|...                                ...|
|   ....                        ....   |
|       ....                ....       |
|           ....        ....           |
|               .... ...               |
|                   |                  |
|                   |                  |
|                   |                  |
|                   |                  |
|                   |                  |
......              |              .... 
      .....         |         .....     
           .....    |    .....          
                ....|....               
                    .                   
Wolfgang
  • 3,470
  • 1
  • 20
  • 36
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
-2

I would suggest rebuilding the script using a recursive function. This way you will be able to mostly get rid of the coordinates, as you would probably be building up (and afterwards down) from the middle. Also, you could split up the cube horizontally, because one half would be fairly easy to adapt the other one.

Martijn Luyckx
  • 402
  • 2
  • 12
  • Hi Martijn Luyckx. I think I understand what you mean. I check the link for recursive function, and the function to build a Pascal triangle could be interesting, but, except the fact that the demonstration just create one line and put it in a boucle exceeds the recursion limit, I don't know how to implement it for my project. I think coordinates are useful because I explain the purpose for a "cube", but it have to be adaptative for 2x3 blocks, etc. Argh, first time I'm so confused !!! – krshk Nov 10 '15 at 13:10