# Recreating a square with fixed baseline and height (3d)

I am trying to recreate the following image, as close as possible.

The point is that the circumference of the bottom, plus the height of the cube needs to be 30. Which gives

30 = 4x + h

so I want to be able to draw the cube where I vary the value of x. Below is what I have done so far

\documentclass{minimal}
\usepackage{pgfplots,tikz}
\usetikzlibrary{shapes,arrows,calc}
\begin{document}

\newcommand\pgfmathsinandcos[3]{%
\pgfmathsetmacro#1{sin(#3)}%
\pgfmathsetmacro#2{cos(#3)}%
}

\begin{tikzpicture}

\pgfmathsetmacro\AngleFuite{30}
\pgfmathsetmacro\coeffReduc{.3}
\pgfmathsetmacro\clen{2}
\pgfmathsinandcos\sint\cost{\AngleFuite}

\begin{scope} [x     = {(\coeffReduc*\cost,-\coeffReduc*\sint)},
y     = {(1cm,0cm)},
z     = {(0cm,1cm)}]

\pgfmathsetmacro{\x}{5} \pgfmathsetmacro{\h}{30-4*\x}

\path coordinate (A) at (-\h/2,\x/2,\x/2)
coordinate (B) at (-\h/2,-\x/2,\x/2)
coordinate (C) at (-\h/2,-\x/2,-\x/2)
coordinate (D) at (-\h/2,\x/2,-\x/2)
coordinate (A1) at (\h/2,\x/2,\x/2)
coordinate (B1) at (\h/2,-\x/2,\x/2)
coordinate (C1) at (\h/2,-\x/2,-\x/2)
coordinate (D1) at (\h/2,\x/2,-\x/2)
coordinate[label=$h$,left=0.65em] (M1) at ($(B)!.5!(C)$)
coordinate[label=$x$,below=2em] (M2) at ($(C1)!.5!(C)$)
coordinate[label=$x$,below=1.75em] (M3) at ($(C1)!.5!(D1)$);

\fill [blue, fill opacity = 0.2]   (A)--(B)--(C)--(D) -- cycle;
\fill [blue, fill opacity = 0.3]   (C)--(D)--(D1)--(C1) -- cycle;
\fill [blue, fill opacity = 0.2]   (A1)--(B1)--(C1)--(D1) -- cycle;
\fill [blue, fill opacity = 0.2]   (B1)--(C1)--(C)--(B) -- cycle;
\fill [blue, fill opacity = 0.1]   (A1)--(D1)--(D)--(A) -- cycle;

\draw (A) -- (B) -- (C) -- (D) -- (A) -- (A1) -- (B1) -- (C1) -- (D1) -- (A1) ;
\draw (B) -- (B1) ; \draw (C) -- (C1) ; \draw (D) -- (D1) ;

\end{scope}

\end{tikzpicture}

\end{document}


My main problem, is that the code is very messy. And still does not quite do what I want it do do. I still having problems rotating the cube properly, The backlines are not shaded, and the whole coordinate thingy seems very messy and unproffesional. A tad smaller figure would also be much appreciated, I tried scaling the whole image, but alas this also shrank the labels.

Any tips suggestions to a better code, or ways to improve mine?

-

run with xelatex

\documentclass[pstricks]{standalone}
\usepackage{pst-solides3d}
\begin{document}

\psset{Decran=15,viewpoint=20 20 50 rtp2xyz,lightsrc=viewpoint}
\multido{\iX=1+1}{7}{%
\begin{pspicture}(-5,-5)(5,13)
\psSolid[object=parallelepiped,
a=\iX,b=a,c=30 a 4 mul sub,
rm=0,incolor=blue!40,hollow,
fillcolor=red!80,
action=draw**]
\rput[r](5,5){\huge$x=\iX$}
\end{pspicture}
\clearpage}

\end{document}


-
There are times when pstricks seems to do things very easily. This one is a great example. Thanks. Oh, and +1 – hpesoj626 Dec 7 '12 at 14:04