Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write the shortest program you can that displays a red 3D cube.


  • The cube must be red, have 6 rendered sides, and be displayed in 3D.
  • Your program cannot load any textures.
  • More than 1 side must be visible.
  • The result does not need to be rotatable.
share|improve this question
i think u need to add a condition that more than 1 side is visible. –  user13107 Mar 4 at 3:19
Only few languages would do the job for "must use either Direct3D or OpenGL" and like have been said, include a sample and How many sides? Is there a degree? –  Yawz Mar 4 at 10:27
What does "red, have 6 sides and be 3D" mean? Did you mean that there needs to be shading? That we have to actually draw 6 sides, but only 2 or 3 have to be visible? Did you mean that the displayed result has to rotatable? –  Quincunx Mar 4 at 19:02
@Quincunx sorry if what I said was unclear. The cube has to be a solid red colour and 6 sides must be rendered but only 2 or 3 sides must be visible. The result doesn't have to be rotatable. –  icedvariables Mar 4 at 19:30
What do you mean by 'cannot load any textures'? Do you mean that you can't simply load an image of a red cube or that you can't use textures to make the cube red? –  Steve Mar 5 at 15:58

10 Answers 10

T-SQL, 168 chars (but no OpenGL)

Decidedly not using OpenGL or Direct3D because there is absolutely no way to do that in SQL. I think I have a pretty nice little cube, anyway.


Exploiting the fact that errors of high severity are raised in red:

srs errr

share|improve this answer
Nice rule bending! –  Ismael Miguel Mar 3 at 23:09
There are some trailing spaces in cube's 2nd line. Without them is just 156 characters long. If you specify the code block's language as “lang-sql”, the cube will be red even in the source code. –  manatwork Mar 4 at 19:22
Yeah, I deliberately didn't super-golf this one because it is technically against the rules since it uses no OpenGL/Direct3D. People can vote me up if they like but I shouldn't win here. –  Jonathan Van Matre Mar 4 at 19:46
If lazy answers like this are upvoted, what incentive is there to work on real solutions? –  Steve Mar 5 at 16:13
I don't consider it a lazy answer - I drew my cube by hand! How is that more lazy than using a built-in Cuboid/cube function in Mathematica/Sage? Toward your question of incentive, I offer my own incentive for this answer: self-amusement. I fully expected a healthy heap of downvotes, but I made (and honestly advertised) this non-compliant answer for my own delight. If your work isn't for your own pleasure, what is it good for? –  Jonathan Van Matre Mar 5 at 16:27

Mathematica 25


Mathematica graphics

Instructions for testing this answer without Mathematica installed:

  1. Download http://pastebin.com/3dnGNHCP and save it as *.CDF
  2. Dowload the free CDF environment from Wolfram Research at https://www.wolfram.com/cdf-player/ (not a small file)
  3. The cube can be "dragged" to rotate it!
share|improve this answer
Kudos for including CDF instructions! –  Jonathan Van Matre Mar 3 at 22:15
@JonathanVanMatre Thanks! One of the major problems for using Mathematica solutions in this site is the need for a non-free environment to test them. That can be overcome (to a certain extent) by sharing CDF documents. –  belisarius Mar 3 at 22:20
Does mathematica use OpenGL? –  TheDoctor Mar 3 at 22:30
@TheDoctor Short answer:"yes".Long answer: "You can configure it" (support.wolfram.com/kb/3303) –  belisarius Mar 3 at 22:54

May i be the first to...

Processing, 513 110

void setup(){size(640,360,P3D);}void draw(){fill(255,0,0);translate(width/2,height/2,0);rotateX(0.7);box(99);}


share|improve this answer
So far the only actual OpenGL answer.. –  TheDoctor Mar 3 at 22:31
Mathematica uses OGL and D3D under the hood, although the problem becomes "How do you prove it was used when this particular graphic was rendered?" There is probably a way to do that in MM; I just don't know what it is. I upvoted you because I like your choice of perspective. –  Jonathan Van Matre Mar 3 at 23:07
You can reduce some characters by using a smaller size i.e. size(99,99,P3D); and then use 50 instead of width/2 and height/2 in your translate(). Also, you can remove the 0 in the 0.7, i.e. make it rotateX(.7);. Also remember to scale down the box. Finally, you can move everything into setup() and delete the void draw() altogether. –  ace Mar 3 at 23:13
Too bad there's no award for most improved, because it would surely be yours. –  Jonathan Van Matre Mar 5 at 18:07

Sage, 17

Note: after some research I realised that this does not use OpenGL.



enter image description here

I can finally beat Mathematica!

It uses Jmol (which comes with the Sage installation) to show the cube, which allows rotation by dragging.

share|improve this answer
Does this use OpeGL? –  TheDoctor Mar 3 at 22:31
Nice:). How many chars does it cost to remove the numbers? –  belisarius Mar 3 at 22:31
@belisarius I'm quite new to this Sage thing, and after going through the documentation I can't seem to find a way to remove the axes. Sorry. –  ace Mar 3 at 23:05
@TheDoctor Sorry, no. Added a note in the answer to state that. –  ace Mar 3 at 23:06
Looks like OpenGL/Direct has been dropped from the requirements. –  Steve Mar 5 at 16:13

TXT, 12 bytes

This is a TXT file with ANSI escape codes and UTF-8 characters. I can show its contents as a C string:


or as a hexdump:

0000000: 1b5b 3331 6de2 aca2 1b5b 6d0a            .[31m....[m.

This is the result when it is "executed" with cat on a terminal.

You can see the small cube, solid red, with 3 visible faces and no texture loaded:

share|improve this answer

Windows Batch, 51 chars

@echo off
color 0c
echo   _
echo /!_!
echo |/_/

I know this is cheating, but at least I get bronze (at the time this was posted). Partially inspired by Jonathan van Matre's answer.

share|improve this answer
Haha, chutzpah! –  Jonathan Van Matre Mar 4 at 15:20

Processing, 68 Chars

Ultra-golf version of @TheDoctor 's answer, using tips from @Ace, plus one of my own (rotation.)

void setup(){size(99,99,P3D);fill(#FF0000);translate(9,9,0);box(9);} 

Produces a pathetically small cube of just 9 units per side (image at left.) Changing the 9's to 25's we get a larger cube (image at right.)

By keeping the cube far from the centre of the window, we can see the bottom and right hand side without needing to rotate (if it was in the centre of the window, they would be hidden.

enter image description here

share|improve this answer
Colour in Processing is a 4-byte value: alpha(opaqueness),R,G,B. The # notation implies an opaqueness of FF. I just found I can save 1 character by specifiying the colour as -65536, which gets coerced into 0xFFFF0000 unsigned. -9766=-38*(256+1) gives a pale pink cube. –  steveverrill Mar 5 at 11:27

Mathematica, 21


enter image description here

share|improve this answer

Postscript 920

Requires mat.ps.

/olddiv/div load def/div{dup 0 eq{pop pop 
0 10]/theta[0 0 0]/eye[0 0 20]/Rot 3 ident>>def/makerot{theta
0 get roty theta 1 get rotx matmul theta 2 get rotz matmul}def/proj{DICT
begin cam{sub}vop Rot matmul 0 get aload pop eye aload pop 4 3 roll div 
exch neg 4 3 roll add 1 index mul 4 1 roll 3 1 roll sub mul exch end}dup
0 disp put def/v[[1 1 -1][-1 1 -1][-1 -1 -1][1 -1 -1][1 1 1][-1 1 1][-1 -1 1][1 -1 1]]def
/fv[[0 1 2 3][0 4 5 1][1 5 6 2][2 6 7 3][3 7 4 0][4 7 6 5]]def/R 20 def/H -3 def/ang 0 def 
{300 700 translate 1 70 dup dup scale div setlinewidth
disp begin/cam[ang sin R mul H ang cos R mul]def/theta[ang H R atan 0]def/Rot makerot def end 
fv{{v exch get proj}forall moveto lineto lineto lineto closepath 1 0 0 setrgbcolor fill}forall
fv{{v exch get proj} forall moveto lineto lineto lineto closepath 0 setgray stroke}forall
showpage/ang ang 3 add def}loop

Ungolfed and commented.

/olddiv/div load def
/div{dup 0 eq{pop pop 100000}{olddiv}ifelse}def

/disp <<
    /cam [ 0 0 10 ] % Camera position
    /theta [ 0 0 0 ] % Rotation sequence
    /eye [ 0 0 20 ] % Eye relative to image surface
    /Rot 3 ident
>> def

/makerot {
    theta 0 get roty
    theta 1 get rotx matmul
    theta 2 get rotz matmul
} def

% Ax Ay Az
/proj { DICT begin
    %3 array astore
    %dup == flush
    cam {sub}vop %Camera translation
    Rot matmul %Camera rotation
    0 get aload pop % Dx Dy Dz
    eye aload pop % Dx Dy Dz Ex Ey Ez
    4 3 roll div % Dx Dy Ex Ey Ez/Dz
    exch neg % Dx Dy Ex Ez/Dz -Ey
    4 3 roll add % Dx Ex Ez/Dz Dy-Ey
    1 index mul % Dx Ex Ez/Dz Ez(Dy-Ey)/Dz
    4 1 roll 3 1 roll % Ez(Dy-Ey)/Dz Ez/Dz Dx Ex
    sub mul exch % Ez(Dx-Ex)/Dz Ez(Dy-Ey)/Dz
    %pstack ()=
end } dup 0 disp put def

/v [[  1  1  -1 ] %cube vertices
    [ -1  1  -1 ]
    [ -1 -1  -1 ]
    [  1 -1  -1 ]
    [  1  1   1 ]
    [ -1  1   1 ]
    [ -1 -1   1 ]
    [  1 -1   1 ]] def
/fv [[ 0 1 2 3 ] %cube faces out of vertices
     [ 0 4 5 1 ]
     [ 1 5 6 2 ]
     [ 2 6 7 3 ]
     [ 3 7 4 0 ]
     [ 4 7 6 5 ]
     ] def

/R 20 def
/H -3 def
/ang 0 def

    300 700 translate
    1 70 dup dup scale div setlinewidth

    disp begin
        /cam [ ang sin R mul  H  ang cos R mul ] def
        /theta [ ang  H R atan  0 ] def
        /Rot makerot def

    fv {
        { v exch get proj } forall
        moveto lineto lineto lineto closepath
        1 0 0 setrgbcolor fill
    } forall

    fv {
        { v exch get proj } forall
        moveto lineto lineto lineto closepath
        0 setgray stroke
    } forall

    /ang ang 3 add def
} loop
share|improve this answer




share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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