15
\$\begingroup\$

How can we depict a turned shape simply and nicely using ascii characters in 3D space?

I suggest this method :

Instead of a circle we use a square rotated by 45° for the cutting section so that we only need '/' and '\' characters to draw it.

 /\
/  \  
\  /
 \/

And we use '_' character for the profiles: upper, lower and median.

  _________     
 /\        \      
/  \________\    
\  /        /       
 \/________/    

Isn't it Turning complete? Well, if you agree, write a full program or a function taking an unsigned integer value N , representing a number of steps , producing a 3d shape as described below.

The profile of this turned shape has the form of a stair step curve raising from 0 to N steps and lowering back to 0 where each step is 2 char('/') high and 5 char long ('_').

Maybe some examples describes it more clearly.

For N = 0 you may output nothing but it's not mandatory to handle it.

  N = 1

   _____ 
  /\    \ 
 /  \____\ 
 \  /    / 
  \/____/ 
.
      N = 2

        _____
       /\    \  
   ___/_ \    \__ 
  /\    \ \    \ \ 
 /  \____\ \____\_\ 
 \  /    / /    / / 
  \/____/ /    /_/ 
      \  /    / 
       \/____/ 
.
       N = 3
             _____  
            /\    \ 
        ___/_ \    \__ 
       /\    \ \    \ \ 
   ___/_ \    \ \    \ \__ 
  /\    \ \    \ \    \ \ \ 
 /  \____\ \____\ \____\_\_\ 
 \  /    / /    / /    / / / 
  \/____/ /    / /    / /_/ 
      \  /    / /    / / 
       \/____/ /    /_/ 
           \  /    / 
            \/____/ 

Rules :
- Margins are not specified.
- Standard loopholes are forbidden.
- Standard input/output methods.
- Shortest answer in bytes wins.

\$\endgroup\$
7
\$\begingroup\$

Charcoal, 70 69 bytes

NθFθ«J⊕×⁷⊕ι⁰≔⊗⊕ιι↙ι↑←×⁵_P↖²→↗ι×⁴_↖ι←×⁵_↓P↙²→↘ιJ⁻×⁹θ⊗ι⊖ι__↗ι←P←__↖ι←__

Try it online! Link is to verbose version of code. Explanation:

NθFθ«

Input N and loop that many times.

J⊕×⁷⊕ι⁰

Jump to the (middle) right corner of the front slice.

≔⊗⊕ιι

Get the size of the slice.

↙ι↑←×⁵_P↖²→↗ι×⁴_↖ι←×⁵_↓P↙²→↘ι

Draw the front slice.

J⁻×⁹θ⊗ι⊖ι

Jump to the bottom (that we can see) of the back slice.

__↗ι←P←__↖ι←__

Draw the back slice.

\$\endgroup\$
7
\$\begingroup\$

JavaScript (ES8),  322 282 278  272 bytes

Builds the output line by line.

f=(n,y=0,Y=(k=y>2*n)?4*n-y:y,S='___/_25__,/\\    \\25,\\/____/14_/,\\ 14,\\/____/,_____, \\____\\, /    /, \\    \\,_\\, /, \\'.split`,`)=>~Y?(y-2*n?''.padEnd(n*5-(Y>>1)*5-3-y%2-k)+S[y?Y?k*2+y%2:4:5]:'/ 03').replace(/\d/g,n=>S[+n+6].repeat(Y-(n>2||-k)>>1))+`
`+f(n,y+1):''

Try it online!

How?

For each row \$0 \le y \le 4n\$, we define:

$$k=\cases{ 0,&\text{if $y \le 2n$}\\ 1,&\text{if $y > 2n$} }$$ $$Y=\cases{ y,&\text{if $k=0$}\\ 4n-y,&\text{if $k=1$} }$$

Each row is first converted into a main pattern. A main pattern may contain digits: they are placeholders for repeated sub-patterns that are expended afterwards.

The middle row (when \$y=2n\$) is a special case which is processed separately. For all other rows, we compute the main pattern ID \$p\$ with:

$$p=\cases{ 5,&\text{if $y=0$}\\ 4,&\text{if $y \neq 0, Y=0$}\\ 2k+(y \bmod 2),&\text{otherwise}\\ }$$

There are no leading spaces for the middle row. For all other rows, the number \$s\$ of leading spaces is given by:

$$s=5n-5\left\lfloor\frac{Y}{2}\right\rfloor-3-(y\bmod 2)-k$$

There are inner sub-patterns (marked with a digit \$\le2\$) and outer sub-patterns (marked with a digit \$>2\$), which are repeated \$n_1\$ and \$n_2\$ times respectively:

$$n_1=\left\lfloor\frac{Y+k}{2}\right\rfloor\\ n_2=\left\lfloor\frac{Y-1}{2}\right\rfloor$$

The above formulae apply to the middle row as well, but are irrelevant for the first and last rows, which do not have sub-patterns.

Below is what we get for \$n=3\$:

  y Y k |  p  |  s  |   before .replace()  | n1 | n2 |      after .replace()
--------+-----+-----+----------------------+----+----+-----------------------------
  0 0 0 |  5  | 12  | ............_____    |  0 | -1 | ............_____          
  1 1 0 |  1  | 11  | .........../\    \25 |  0 |  0 | .........../\    \         
  2 2 0 |  0  |  7  | .......___/_25__     |  1 |  0 | .......___/_ \    \__      
  3 3 0 |  1  |  6  | ....../\    \25      |  1 |  1 | ....../\    \ \    \ \     
  4 4 0 |  0  |  2  | ..___/_25__          |  2 |  1 | ..___/_ \    \ \    \ \__  
  5 5 0 |  1  |  1  | ./\    \25           |  2 |  2 | ./\    \ \    \ \    \ \ \ 
  6 6 0 | n/a | n/a | / 03                 |  3 |  2 | /  \____\ \____\ \____\_\_\
  7 5 1 |  3  |  0  | \ 14                 |  3 |  2 | \  /    / /    / /    / / /
  8 4 1 |  2  |  1  | .\/____/14_/         |  2 |  1 | .\/____/ /    / /    / /_/ 
  9 3 1 |  3  |  5  | .....\ 14            |  2 |  1 | .....\  /    / /    / /    
 10 2 1 |  2  |  6  | ......\/____/14_/    |  1 |  0 | ......\/____/ /    /_/     
 11 1 1 |  3  | 10  | ..........\ 14       |  1 |  0 | ..........\  /    /        
 12 0 1 |  4  | 11  | ...........\/____/   |  0 | -1 | ...........\/____/         
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.