# How can I draw a transparent cube in 3D perspective?

I would like to draw a transparent, flattened cube' in perspective with dotted lines from both vanishing points to each of the vertices of the 3D solid. Is there a PSTricks package for this?

It is much like Jan Hlavacek's answer to What is the easiest way to draw 3D cube with TikZ? but with the vanishing points and dotted lines from these, and then the cube' should be transparent. Unfortunately I do not know TikZ.

Update

The following code shows what I would like to achieve:

\documentclass{article}

\begin{document}

\begin{figure}
\def\HAa{-5.25 }
\def\HBa{12.5 }
\def\Ab{-0.85 }
\def\Bb{3.05 }
\def\Ia{-1.8 }
\def\Ja{2.65 }
\centering
\psset{unit=0.765cm}
\begin{pspicture}(\HAa,\Ab)(\HBa,\Bb)
\pnode(!\HAa 0){H1}
\psdot(H1)
\pnode(!\HBa 0){H2}
\psdot(H2)
\pnode(!\Ia 0){P9}
\pnode(!\Ja 0){P10}
\pcline[linestyle=dashed](H1)(P9)
\pcline[linestyle=dashed](P10)(H2)
\pnode(!0 \Ab){P1}
\pnode(!0 \Bb){P2}
\pnode(!\Ia \HAa \Ia sub \HAa div \Ab mul){P3}
\pnode(!\Ia \HAa \Ia sub \HAa div \Bb mul){P4}
\pnode(!\Ja \HBa \Ja sub \HBa div \Ab mul){P5}
\pnode(!\Ja \HBa \Ja sub \HBa div \Bb mul){P6}
\psIntersectionPoint(H1)(P5)(H2)(P3){P7}
\psIntersectionPoint(H1)(P6)(H2)(P4){P8}
\multido{\i=1+1}{8}{\psdot(P\i)}
\pcline(P1)(P2)
\pcline(P1)(P3)
\pcline(P2)(P4)
\pcline(P3)(P4)
\pcline(P1)(P5)
\pcline(P2)(P6)
\pcline(P5)(P6)
\psset{linestyle=dotted}
\pcline(P3)(H1)
\pcline(P4)(H1)
\pcline(P5)(H1)
\pcline(P6)(H1)
\pcline(P3)(H2)
\pcline(P4)(H2)
\pcline(P5)(H2)
\pcline(P6)(H2)
\pcline(P7)(P8)
\end{pspicture}
\end{figure}

\end{document}


I would like to make this drawing when knowing just the width, depth, and height of the box plus the point of view, instead of maunally calculating the \def values.

P.S. I do not know how to include images, sorry.

Update 2

I have changed the code a bit. (The \def names are changed and the hiddens lines in the cubiod are dashed instead of dotted.)

\documentclass{article}

\begin{document}

\begin{figure}
\def\venstre{-5.25 }
\def\hoejre{12.5 }
\def\vinkel{-0.85 }
\def\hoejde{3.05 }
\def\dybde{-1.8 }
\def\bredde{2.65 }
\centering
\psset{unit=0.684cm}
\begin{pspicture}(\venstre,\vinkel)(\hoejre,\hoejde)
\pnode(!\venstre 0){H1}
\pnode(!\hoejre 0){H2}
\psdot(H1)
\psdot(H2)
\pnode(!\dybde 0){P9}
\pnode(!\bredde 0){P10}
\pcline[linestyle=dashed](H1)(P9)
\pcline[linestyle=dashed](P10)(H2)
\pnode(!0 \vinkel){P1}
\pnode(!0 \hoejde){P2}
\pnode(!\dybde \venstre \dybde sub \venstre div \vinkel mul){P3}
\pnode(!\dybde \venstre \dybde sub \venstre div \hoejde mul){P4}
\pnode(!\bredde \hoejre \bredde sub \hoejre div \vinkel mul){P5}
\pnode(!\bredde \hoejre \bredde sub \hoejre div \hoejde mul){P6}
\psIntersectionPoint(H1)(P5)(H2)(P3){P7}
\psIntersectionPoint(H1)(P6)(H2)(P4){P8}
\multido{\i=1+1}{8}{\psdot(P\i)}
\pcline(P1)(P2)
\pcline(P1)(P3)
\pcline(P2)(P4)
\pcline(P3)(P4)
\pcline(P1)(P5)
\pcline(P2)(P6)
\pcline(P5)(P6)
\psset{linestyle=dotted}
\pcline(P3)(H1)
\pcline(P4)(H1)
\pcline(P7)(H1)
\pcline(P8)(H1)
\pcline(P7)(H2)
\pcline(P8)(H2)
\pcline(P5)(H2)
\pcline(P6)(H2)
\psset{linestyle=dashed}
\pcline(P7)(P3)
\pcline(P7)(P5)
\pcline(P7)(P8)
\pcline(P8)(P4)
\pcline(P8)(P6)
\end{pspicture}
\end{figure}

\end{document}


Update 3

Here is what I ended up with:

\documentclass{article}

\usepackage{pst-solides3d}

\begin{document}

\begin{figure}
\psset{viewpoint=10 45 8 rtp2xyz,Decran=5}
\begin{pspicture}[solidmemory](-5,-5)(6,12)
\psSolid[object=point,args=6 0 1.42,name=L]
\psSolid[object=point,args=-12 9.75 1.42,name=R]
\psSolid[object=line,linecolor=red!40,linestyle=dashed,linewidth=2pt,args=R L]
\psSolid[object=parallelepiped,a=4.56,b=4.56,c=3.90,RotZ=-8,fillcolor=yellow!40,name=Cube,action=draw*](0 0 2)
\multido{\iA=0+1}{8}{%
\psSolid[object=point,definition=solidgetsommet,args=Cube \iA,name=C\iA]
}
\multido{\iA=0+1}{8}{%
\psSolid[object=line,linecolor=blue!40,linestyle=dotted,args=L C\iA]
\psSolid[object=line,linecolor=blue!40,linestyle=dotted,args=R C\iA]
}
\psSolid[object=parallelepiped,a=4.56,b=4.56,c=3.90,RotZ=-8,name=Cube,action=draw](0 0 2)
\end{pspicture}
\end{figure}

\end{document}

-
At present, this is very much a "do-it-for-me" question. Please edit your post to give some indication of what you've tried so far. – Andrew Uzzell Oct 18 '12 at 10:21
It was not meant as that; sorry. I really just want to know if there is a PSTricks package for doing this. (Way too many details regarding the drawing itself, I see that now.) – Svend Tveskæg Oct 18 '12 at 10:31
I think pst-solides3d could be of interest for you. You could have a look at some of Herbert's answers, he usually provides excellent answers at a fraction of the hassle of a TikZ answer. – Tom Bombadil Oct 18 '12 at 11:40
@Tom Bombadil: Arrrh, interesting! I only had a look at the pst-3d package. – Svend Tveskæg Oct 18 '12 at 11:44
I cannot find anything (for me) useful in the answers from @Herbert so I will contact him and ask if there is a package or if he has a solution. – Svend Tveskæg Oct 18 '12 at 13:22

\documentclass{article}
\usepackage{pst-solides3d}

\begin{document}

\psset{viewpoint=10 60 15 rtp2xyz,Decran=5}
\begin{pspicture}[solidmemory](-5,-5)(6,12)
\psSolid[object=point,args=10 -36 0,name=L]
\psSolid[object=point,args=-36 10 0,name=R]
\psSolid[object=line,linecolor=blue,linestyle=dashed,linewidth=2pt,args=R L]
\psSolid[object=parallelepiped,a=6,b=3,c=3,RotZ=30,name=Cube,action=draw*](0 0 2)
\multido{\iA=0+1}{8}{%
\psSolid[object=point,definition=solidgetsommet,args=Cube \iA,name=C\iA]}
\multido{\iA=0+1}{8}{%
\psSolid[object=line,linecolor=blue!40,linestyle=dotted,args=L C\iA]
\psSolid[object=line,linecolor=blue!40,linestyle=dotted,args=R C\iA]}
\psSolid[object=parallelepiped,a=6,b=3,c=3,RotZ=30,name=Cube,action=draw](0 0 2)
\end{pspicture}

\end{document}


-
Wow! This answer is hereby accepted. Thank you to both Tom Bombadil and Herbert. – Svend Tveskæg Oct 28 '12 at 12:19
Then it is not a cube. Use object=parallelepiped,a=..,b=..,c=.. – Herbert Oct 28 '12 at 12:53
draw the box twice, at the first time with action=none and the second time with action=draw. This one before \end{pspicture} – Herbert Oct 28 '12 at 15:48
draw it also as a solid object with linestyle=dashed, args=L R – Herbert Oct 28 '12 at 16:11
if it should be a hiddenline, then you have to use action=none,name=... for all objects which should be drawn together with taken hidden lines and surfaces into account. AT the very end you use \psSolid[object=fusion,base=...] where base gets all defined names. You can find simple examples on PSTricks.tug.org – Herbert Oct 28 '12 at 16:41

Unfortunately, I don't know pstricks, but here's A TikZ solution:

## Code

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{calc,intersections}

\begin{document}

\begin{tikzpicture}
\coordinate (V1) at (-7,1);
\coordinate (V2) at (5,1);

\fill (V1) circle (0.05) node[left] {V1};
\fill (V2) circle (0.05) node[right] {V2};

\pgfmathsetmacro{\cubelength}{3}

\coordinate (A) at (0,0);
\coordinate (B) at ($(A)!\cubelength cm!(V2)$);
\coordinate (D) at ($(A)!\cubelength cm!(V1)$);

\path[name path=BV1] (B) -- (V1);
\path[name path=DV2] (D) -- (V2);

\path[name intersections={of=BV1 and DV2}] (intersection-1);
\coordinate (C) at (intersection-1);

\coordinate (E) at (0,3);

\path[name path=Bup] (B) -- ++(0,\cubelength);
\path[name path=Dup] (D) -- ++(0,\cubelength);
\path[name path=EV1] (E) -- (V1);
\path[name path=EV2] (E) -- (V2);

\path[name intersections={of=Bup and EV2}] (intersection-1);
\coordinate (F) at (intersection-1);

\path[name intersections={of=Dup and EV1}] (intersection-1);
\coordinate (H) at (intersection-1);

\path[name path=FV1] (F) -- (V1);
\path[name path=HV2] (H) -- (V2);

\path[name intersections={of=FV1 and HV2}] (intersection-1);
\coordinate (G) at (intersection-1);

\foreach \x in {A,B,C,D,E,F,G,H} \fill (\x) circle (0.05) node[below right,circle] {\x};

\foreach \s\e in {A/B,D/A,A/E,B/F,D/H,E/F,F/G,G/H,H/E} \draw (\s) -- (\e);

\foreach \s\e in {B/C,C/D,C/G} \draw[densely dashed] (\s) -- (\e);

\foreach \p in {C,D,G,H} \draw[dotted] (\p) -- (V1);

\foreach \p in {B,C,F,G} \draw[dotted] (\p) -- (V2);

\path[name path=V1V2] (V1) -- (V2);

\draw[name intersections={of={Bup and V1V2},name=R}] (V2) -- (R-1);

\draw[name intersections={of={Dup and V1V2},name=L}] (V1) -- (L-1);

\draw[dotted] (L-1) -- (R-1);

\end{tikzpicture}

\end{document}


## Output

-
This is exactly what I want (but with PSTricks)! I will, however, accept your answer if Herbert or someone else do not give a PSTricks solution. – Svend Tveskæg Oct 28 '12 at 12:17

Here's something to get you started using the pst-solides3d package as requested.

I've borrowed experience from Werner's excellent answer to pst-solides3d and hidden lines

Your first step in constructing the solid is to specify its verticies:

  sommets=
0 0 0
3 0 0
3 3 0
0 3 0
0 3 1
0 0 1
3 0 1
3 3 1,


You can check if you got it right by using

  action=draw,num=all,show=all,


which gives

Finally you construct the solid by forming the faces- join the vertices together in an anti-clockwise direction

  faces={
[2 1 0 3] % bottom face
[6 5 0 1] % left face
[7 6 1 2] % front face
[4 7 2 3] % right face
[3 0 5 4] % back face
[7 4 5 6] % top face
}]%


The complete MWE is below. See page 34-35 of pst-solides3d for a very similar example.

\documentclass{article}
\usepackage{pst-solides3d}% http://ctan.org/pkg/pst-solides3d

\begin{document}

\begin{pspicture}(-2,-1)(3,4)
\psset{viewpoint=25 8 5,Decran=100}
\psSolid[object=new,
fillcolor=green!60,
opacity=0.3,
action=draw*,%num=all,show=all,
sommets=
0 0 0
3 0 0
3 3 0
0 3 0
0 3 1
0 0 1
3 0 1
3 3 1,
faces={
[2 1 0 3] % bottom face
[6 5 0 1] % left face
[7 6 1 2] % front face
[4 7 2 3] % right face
[3 0 5 4] % back face
[7 4 5 6] % top face
}]%
\end{pspicture}
\end{document}


And if you want to colour the sides, you can use fcol

\documentclass{article}
\usepackage{pst-solides3d}% http://ctan.org/pkg/pst-solides3d

\begin{document}

\begin{pspicture}(-2,-1)(3,4)
\psset{viewpoint=25 8 5,Decran=100,solidmemory}
\psSolid[object=new,
fillcolor=green!60,
name=Mycube,
opacity=0.3,
action=none,%num=all,show=all,
sommets=
0 0 0
3 0 0
3 3 0
0 3 0
0 3 1
0 0 1
3 0 1
3 3 1,
faces={
[2 1 0 3] % bottom face
[6 5 0 1] % left face
[7 6 1 2] % front face
[4 7 2 3] % right face
[3 0 5 4] % back face
[7 4 5 6] % top face
}]%
\psSolid[fcol=2 (Apricot)
3 (ForestGreen)
5 (WildStrawberry),
\end{pspicture}
\end{document}

-
This looks very promising. However, it does not seem to be in "true" perspective, i.e., have vanishing points (which I would like to mark and draw dotted lines from to each of the vertices). Am I mistaken about this? – Svend Tveskæg Oct 19 '12 at 0:41
@SvendMortensen can you meet me in chat? – cmhughes Oct 19 '12 at 0:46
Sure, if I figure out how to do that. :) – Svend Tveskæg Oct 19 '12 at 0:47
Here you go: chat.stackexchange.com/rooms/info/6166/… ... I hope this is correct; I have never used chat rooms here before. – Svend Tveskæg Oct 19 '12 at 0:52
I have updated my initial post with come code that, when complied, shows that I will like to achieve. – Svend Tveskæg Oct 19 '12 at 5:07

Tikz is a very powerful package with many examples available. Have a look at this example:

Is this something like what you are after?

-
My reading of the question is that Svend wants a PSTricks answer. – Loop Space Oct 18 '12 at 11:18
@Andrew Stacey: Exactly. I have no experience at all using TikZ so it is not an option for me, unfortunately. – Svend Tveskæg Oct 18 '12 at 11:42
@Jason Whyte: Yes it is but with PSTricks. I have never taken the time to learn TikZ. – Svend Tveskæg Oct 18 '12 at 11:49