3
\$\begingroup\$

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

Rules:

  • 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.
\$\endgroup\$
9
  • 8
    \$\begingroup\$ i think u need to add a condition that more than 1 side is visible. \$\endgroup\$
    – user13107
    Commented Mar 4, 2014 at 3:19
  • \$\begingroup\$ 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? \$\endgroup\$
    – Aesthetic
    Commented Mar 4, 2014 at 10:27
  • \$\begingroup\$ 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? \$\endgroup\$
    – Justin
    Commented Mar 4, 2014 at 19:02
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 4, 2014 at 19:30
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 5, 2014 at 15:58

11 Answers 11

29
\$\begingroup\$

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.

RAISERROR('
      @@@
   @@@@@@@@@            
@@@@@@@@@@@@@@@
x@@@@@@@@@@@@&&
xxx@@@@@@@@&&&&
xxxxx@@@&&&&&&&
xxxxxx&&&&&&&&&
 xxxxx&&&&&&&
   xxx&&&&
     x&&
',15,1)

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

srs errr

\$\endgroup\$
5
  • \$\begingroup\$ Nice rule bending! \$\endgroup\$ Commented Mar 3, 2014 at 23:09
  • \$\begingroup\$ 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. \$\endgroup\$
    – manatwork
    Commented Mar 4, 2014 at 19:22
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 4, 2014 at 19:46
  • \$\begingroup\$ If lazy answers like this are upvoted, what incentive is there to work on real solutions? \$\endgroup\$ Commented Mar 5, 2014 at 16:13
  • 7
    \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 5, 2014 at 16:27
13
\$\begingroup\$

Mathematica 25

Graphics3D@{Red,Cuboid[]}

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!
\$\endgroup\$
4
  • \$\begingroup\$ Kudos for including CDF instructions! \$\endgroup\$ Commented Mar 3, 2014 at 22:15
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 3, 2014 at 22:20
  • \$\begingroup\$ Does mathematica use OpenGL? \$\endgroup\$
    – TheDoctor
    Commented Mar 3, 2014 at 22:30
  • \$\begingroup\$ @TheDoctor Short answer:"yes".Long answer: "You can configure it" (support.wolfram.com/kb/3303) \$\endgroup\$ Commented Mar 3, 2014 at 22:54
10
\$\begingroup\$

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:

"\033[31m\xe2\xac\xa2\033[m\n"

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:
cube.txt

\$\endgroup\$
3
  • 3
    \$\begingroup\$ This is a really clever answer, but txt doesn't technically meet the criteria for a programming language \$\endgroup\$
    – DJMcMayhem
    Commented May 15, 2016 at 5:14
  • 4
    \$\begingroup\$ But you could use /// or PHP as a workaround. \$\endgroup\$
    – jimmy23013
    Commented May 15, 2016 at 11:03
  • \$\begingroup\$ I´d use some *nix shell for that. \$\endgroup\$
    – Titus
    Commented Jan 6, 2017 at 0:06
7
\$\begingroup\$

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);}

example

\$\endgroup\$
4
  • \$\begingroup\$ So far the only actual OpenGL answer.. \$\endgroup\$
    – TheDoctor
    Commented Mar 3, 2014 at 22:31
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 3, 2014 at 23:07
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – user12205
    Commented Mar 3, 2014 at 23:13
  • 1
    \$\begingroup\$ Too bad there's no award for most improved, because it would surely be yours. \$\endgroup\$ Commented Mar 5, 2014 at 18:07
6
\$\begingroup\$

Sage, 17

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

cube(color='red')

Output:

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.

\$\endgroup\$
7
  • \$\begingroup\$ Does this use OpeGL? \$\endgroup\$
    – TheDoctor
    Commented Mar 3, 2014 at 22:31
  • \$\begingroup\$ Nice:). How many chars does it cost to remove the numbers? \$\endgroup\$ Commented Mar 3, 2014 at 22:31
  • \$\begingroup\$ @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. \$\endgroup\$
    – user12205
    Commented Mar 3, 2014 at 23:05
  • \$\begingroup\$ @TheDoctor Sorry, no. Added a note in the answer to state that. \$\endgroup\$
    – user12205
    Commented Mar 3, 2014 at 23:06
  • \$\begingroup\$ Looks like OpenGL/Direct has been dropped from the requirements. \$\endgroup\$ Commented Mar 5, 2014 at 16:13
3
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 5, 2014 at 11:27
3
\$\begingroup\$

Mathematica 9, 21

Image3D@{{{{3,0,0}}}}

enter image description here


Mathematica 10, 17

Image3D@{{{Red}}}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Haha, chutzpah! \$\endgroup\$ Commented Mar 4, 2014 at 15:20
1
\$\begingroup\$

Postscript 920

Requires mat.ps.

/olddiv/div load def/div{dup 0 eq{pop pop 
100000}{olddiv}ifelse}def(mat.ps)run/disp<</cam[0
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
(mat.ps)run

/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
    %pstack()=
    Rot matmul %Camera rotation
    0 get aload pop % Dx Dy Dz
    eye aload pop % Dx Dy Dz Ex Ey Ez
    %pstack()=
    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
    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
\$\endgroup\$
1
\$\begingroup\$

JAVASCRIPT - TQUERY (108 chars)

w=tQuery.createWorld().boilerplate().start();tQuery.createCube().addTo(w).setBasicMaterial().color(0xff0000)
<script src="https://jeromeetienne.github.io/tquery/build/tquery-bundle.js"></script>

http://jsfiddle.net/Gy4w7/335/

\$\endgroup\$
0
\$\begingroup\$

RBX.Lua, 83 bytes

s=Instance.new("Part",workspace)s.BrickColor=BrickColor.new(21)s.Size=Vector3.new()

Result:

Result

Explanation:

s=Instance.new("Part",workspace)  Instances a new Brick in the world.
s.BrickColor=BrickColor.new(21)   Makes it red. 21 is the color code for "Bright red".
s.Size=                           We have to set the size, because a brick isn't cubic.
Vector3.new()                     Initiates a [0, 0, 0] size. The brick rounds this up
                                  to the minimum size.
\$\endgroup\$

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