5

Hi guys I'm trying to draw a really simple cube in different ways but there is something that is bugging me, why the vertices are so imperfect?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \draw (0,0,0) -- (0,1,0) -- (0,1,1) -- (1,1,1) -- (1,1,0) -- (1,0,0) -- (0,0,0) -- (0,0,1) -- (1,0,1) -- (1,0,0);
    \draw (0,1,0) -- (1,1,0);
    \draw (1,0,1) -- (1,1,1);
    \draw (0,0,1) -- (0,1,1);
\end{tikzpicture}
\end{document}

cento18
  • 201
  • What's the issue with the vertices? Could you add a picture or explanation of what you're after? One possibility is https://tex.stackexchange.com/a/30102/86 – Andrew Stacey Mar 27 '22 at 20:42
  • When I have to draw cubes like this, I often use miter limit to avoid thos peaky vertices that drop over other edges. But it's not always enough. – SebGlav Mar 27 '22 at 21:33

3 Answers3

8

You can try this code

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz,tikz-3dplot} 
\begin{document}
    \tdplotsetmaincoords{70}{120}
\begin{tikzpicture}[tdplot_main_coords,line cap=butt,line join=round,c/.style={circle,fill,inner sep=1pt},
        declare function={a=2;}]
        \path
        (a,-a,-a) coordinate (A)
        (a,a,-a) coordinate (B)
        (-a,a,-a) coordinate (C)
        (-a,-a,-a) coordinate (D)
        (a,-a,a) coordinate (E)
        (a,a,a) coordinate (F)
        (-a,a,a) coordinate (G)
        (-a,-a,a) coordinate (H)
        (0,0,0)  coordinate (O)
        ;
\draw[dashed] (D) -- (H) (D) -- (A) (D) -- (C);
\draw (H) -- (E) -- (F) -- (G) -- cycle (A) -- (E) (B) -- (F) (C) -- (G) (A) -- (B) -- (C);
\path foreach \p/\g in {A/-90,B/-90,C/-90,D/-90,E/90,F/90,G/90,H/90}{(\p)node[c]{}+(\g:2.5mm) node{$\p$}};  
\end{tikzpicture}
\end{document}

enter image description here

You can change the number 120 in the line \tdplotsetmaincoords{70}{120} to get another view.

4

This is so much simple with pstricks – more precisely with pst-solides3d:

\documentclass[border=6pt, pstricks, svgnames]{standalone}
\usepackage{pst-solides3d}

\begin{document}

\begin{pspicture}(-10,-6 )(10,12) \axesIIIDarrows=->, arrowsize=5pt, arrowinset=0.1, axisnames={x, y, z} \psSolid[object=cube, a=2, action =draw, fillcolor=Lavender] \end{pspicture*}

\end{document}

enter image description here

Bernard
  • 271,350
1

For fun with Asymptote.

cube

// http://asymptote.ualberta.ca/
import three;
size(4cm);
draw(unitcube,yellow+opacity(.5));
draw(unitbox,blue);
dot(unitbox,red);
Black Mild
  • 17,569