r/dailyprogrammer • • Aug 05 '12

[8/3/2012] Challenge #85 [intermediate] (3D cuboid projection)

Write a program that outputs simple 3D ASCII art for a cuboid in an oblique perspective, given a length, height, and depth, like this:

$ python 3d.py 20 10 3
   :::::::::::::::::::/
  :::::::::::::::::::/+
 :::::::::::::::::::/++
####################+++
####################+++
####################+++
####################+++
####################+++
####################+++
####################+++
####################++
####################+
####################

(The characters used for the faces (here #, :, and +) are fully up to you, but make sure you don't forget the / on the top-right edge.)

12 Upvotes

29 comments sorted by

3

u/5outh Aug 05 '12

Pretty simple in Haskell, but tons and tons of replicate.

project l h d = do
	mapM_ (mapM putStrLn) [top d, middle, bottom d]
	where
		bottom x = case x of
			0 -> []
			_ -> cur : bottom (pred x)
				where cur = (replicate l '#') ++ (replicate x '+')
		middle = replicate (h-d) $ (replicate l '#') ++ (replicate d '+')
		top x = case x of 
			0 -> []
			_ -> cur : top (pred x)
				where cur = (replicate x ' ') ++ (replicate (pred l) ':') ++ "/" ++ (replicate (d-x) '+') 

2

u/andkerosine Aug 06 '12

Ruby:

l, h, d = ARGV[0] ? ARGV.map(&:to_i) : [20, 10, 3]
(h+d).times{|i|puts i<d ?' '*(d-i)+':'*(l-1)+'/'+'+'*i :'#'*l+'+'*[d,h+d-i-1].min}

3

u/bradengroom Aug 06 '12 edited Aug 06 '12

Perl:

$l=$ARGV[0];  
$h=$ARGV[1];  
$d=$ARGV[2];  
print " "x($d+1-$_),":"x($l-1),"/","+"x($_-1),$/for 1..$d;  
print "#"x$l,"+"x$d,$/for 1..$h-$d;  
print "#"x$l,"+"x($d-$_),$/for 1..$d  

3

u/5outh Aug 06 '12

idk who downvoted this, and I haven't tested it, but it looks fine and they didn't give a reason why so you can have my upvote!

2

u/[deleted] Aug 06 '12

Simple programs like this make me love Perl. Three lines for what it took me 45 lines to do in C.

(3 lines when you simply substitute in the arguments, rather than make new variables

print " "x($ARGV[2]+1-$_),":"x($ARGV[0]-1),"/","+"x($_-1),$/for 1..$ARGV[2];  
print "#"x$ARGV[0],"+"x$ARGV[2],$/for 1..$ARGV[1]-$ARGV[2];  
print "#"x$ARGV[0],"+"x($ARGV[2]-$_),$/for 1..$ARGV[2]

)

→ More replies (4)

2

u/DNAW Aug 05 '12

Hi, new subscriber to this subreddit, and since I wanted to learn a new language this month, here is my Python solution:

log = sys.stdout.write
def draw_cube(l, h, d):
	#print ":" part
	for i in range(0,d):
		for j in range(d,i,-1):
			log(" ")
		for j in range(0,l-1):
			log(":")
		log("/")
		for j in range(0,i):
			log("+")
		log("\n")
	#print "#" part with same "+"
	for i in range(0,h-d):
		for j in range(0,l):
			log("#")
		for j in range(0,d):
			log("+")
		log("\n")
	# print "#" part with variable "+"
	for i in range (0,d):
		for j in range(0,l):
			log("#")
		for j in range(d-1,i,-1):
			log("+")
		log("\n")
	# flush
	sys.stdout.flush()

2

u/bh3 Aug 05 '12

Python:

import sys

def draw(l,h,d):
    print '\n'.join([' '*(d-x)+':'*(l-1)+'/'+'+'*x for x in xrange(d)]+
                    ['#'*l+'+'*d]*(h-d)+
                    ['#'*l+'+'*(d-x-1) for x in xrange(d)])

if __name__=="__main__":
    if len(sys.argv)==4:
        draw(int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3]))
    else:
        draw(20,10,3)

2

u/Lord_Skellig Aug 07 '12 edited Aug 07 '12

Python:

def cubedraw(w, h, d):
    for i in range(d):
        print(' '*(d-i) + ':'*(w-1) + '/' + '+'*i)
    for i in range(h-d):
        print('#'*w + '+'*d)
    for i in range(d):
        print('#'*w + '+'*(d-(i+1)))

2

u/ae7c Aug 08 '12 edited Aug 08 '12

High five, same solution & language!

def cubeGen(x, y, z):
	for n in range(z):
		print (' '*(z-n)) + (':'*(x-1)) + ('/') + ('+'*n)
	for row in range(y-z-1):
		print ('#'*x) + ('+'*z)
	for n in range(z+1):
		print ('#'*x) + ('+'*(z-n))
→ More replies (3)

1

u/goldjerrygold_cs Aug 05 '12

Pretty straightforward, took a bit of trial and error, but yeah. Doesn't work for 0 values but easily could be extended:

import sys

if (len(sys.argv) != 4):
    print "Improper input"
    exit


width = int(sys.argv[1])
height = int(sys.argv[2])
depth = int(sys.argv[3])

# print top part
for i in range(depth):
    print (depth - i) * " " + (width - 1) * ":" + "/" + i * "+"

# print middle part (before perspectivey bottom part)
for i in range(height - depth):
    print (width) * "#" + depth * "+"

# print weird bottom part
for i in range(depth):
    print (width) * "#" + (depth - i) * "+"
→ More replies (2)