# Draw two boxes on a slanted plane in a mechanical illustration

Consider:

I need help drawing the two boxes on an inclination.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw (2,3) coordinate (A) - - (8,0) coordinate (B)
- - (2,0) coordinate (C) pic [draw,->] {angle};
\fill[pattern=north west lines](2,-1) rectangle(9,0);
\draw(7,.2 5) node[left] {$\theta$};
\end{tikzpicture}
\end{document}


One way to draw the two inclined boxes is to use a scope with a rotate applied:

## Code:

\documentclass[border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\draw  (2,3) coordinate (A)
-- (8,0) coordinate (B)
-- (2,0) coordinate (C)
-- cycle;
\fill[pattern=north west lines]
(2,-1) rectangle(8,0);
\draw(7,.2 5) node[left] {$\theta$};

\begin{scope}[rotate=atan((3-0)/(2-8))]
\draw  [fill=cyan!40, densely dashed] ([shift={(-0.5,0)}]A) rectangle ++(1,0.5);
\draw  [fill=red!20]                  ([shift={(-0.5,0)}]B) rectangle ++(1,0.5);
\end{scope}
\end{tikzpicture}
\end{document}

\documentclass[border = 5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns, calc, quotes, angles}

\def\inc{25}

\begin{document}
\begin{tikzpicture}

% incline
\draw (0,0) coordinate(O) -- (180-\inc : 7) coordinate(A) |- coordinate (D) ($(O)+(0,-1)$) -- cycle;
\draw[dashed] (O) -| coordinate (C) (A);
\fill[pattern = north west lines] (D) rectangle (O);

% measures
\draw[|<->|] ($(A)+(-0.5, 0)$) -- ($(C)+(-0.5, 0)$) node[midway, left]{$h$};
\pic["$\theta$", <->, draw, angle eccentricity = 1.2, angle radius = 1cm] {angle = A--O--C};

% block
\begin{scope}[rotate=180-\inc]
\draw[shift={(A)}, dashed] ($(A)+(-0.4,0)$) rectangle ++ (0.8,-0.6);
\draw (-0.4, 0) rectangle ++ (0.8,-0.6);
\draw[-latex] (0.4, -1) -- (-0.4, -1) node[above, midway]{${\bf v}$};
\end{scope}
\end{tikzpicture}
\end{document}


Plus a little animation

\documentclass[border = 5pt, tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns, calc, quotes, angles}

\begin{document}

\foreach \inc in {60,58,...,20,22,24,...,60}
{
\begin{tikzpicture}
\useasboundingbox (-8, -1.5) rectangle (1, 6);

% incline
\draw (0,0) coordinate(O) -- (180-\inc : 7) coordinate(A) |- coordinate (D) ($(O)+(0,-1)$) -- cycle;
\draw[dashed] (O) -| coordinate (C) (A);
\fill[pattern = north west lines] (D) rectangle (O);

% measures
\draw[|<->|] ($(A)+(-0.5, 0)$) -- ($(C)+(-0.5, 0)$) node[midway, left]{$h$};
\pic["$\theta$", <->, draw, angle eccentricity = 1.2, angle radius = 1cm] {angle = A--O--C};

% block
\begin{scope}[rotate=180-\inc]
\draw[shift={(A)}, dashed] ($(A)+(-0.4,0)$) rectangle ++ (0.8,-0.6);
\draw (-0.4, 0) rectangle ++ (0.8,-0.6);
\draw[-latex] (0.4, -1) -- (-0.4, -1) node[above, midway]{${\bf v}$};
\end{scope}
\end{tikzpicture}
}
\end{document}


A PSTricks solution only for fun purposes.

\documentclass[pstricks,12pt]{standalone}
\usepackage{pst-eucl}
\begin{document}
\pspicture(-1,-1)(8,5)
\psframe[fillstyle=vlines,hatchsep=1pt,linestyle=none](0,-.5)(7,0)
\pstTriangle[PointName=none,PointSymbol=none](0,0){A}(7,0){B}(0,4){C}
\pstMarkAngle{C}{B}{A}{$\theta$}
\pcline(C)(B)\naput[npos=-.05,labelsep=-\pslinewidth,nrot=:U]{\psframe(1,.5)\psframe(8,0)(9,.5)\pcline{->}(8.25,.75)(8.75,.75)\naput[labelsep=12pt]{\rput{*0}{$\vec{v}$}}}
\pcline[offset=10pt]{|*-|*}(A)(C)\naput{$h$}
\endpspicture
\end{document}


• A dot at the left corner was not intentionally made. It appears as a real bug. – Well Harassed Programmer Jan 5 at 12:14

A Metapost alternative, purely for comparison. Compile this one with lualatex (or work out how to adapt it for plain MP).

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric theta; theta = 19;

path box;
box = unitsquare shifted 1/2 left
xscaled 34 yscaled 21 shifted 1/2 up
rotated -theta;

z0 = origin;
z3 = 288 left;
x4 = x3 = x2; x1 = x0;
y1 = y2 = -34;
z4 = whatever * (z3 rotated -theta);

draw z0--z3 dashed evenly scaled 1/2;
draw z0--z1--z2--z4--cycle;

draw box withcolor 2/3 red;
draw box shifted z4 dashed withdots scaled 1/4 withcolor 2/3 red;

path a, h, v;
a = fullcircle scaled 112 cutbefore (z0--z4) cutafter (z0--z3);
h = (z3--z4) shifted 10 left;
v = subpath (3,2) of box shifted (10 up rotated -theta);

draw a; label.lft("$\theta$", point 1/2 of a);
drawarrow v; label.top("$v$", point 1/2 of v);
drawdblarrow h; label.lft("$h$", point 1/2 of h);

endfig;
\end{mplibcode}
\end{document}

• If one is using Metafun, then instead of unitsquare shifted 1/2 left shifted 1/2 up one can use fullcircle which is defined as unitsquare shifted - center unitsquare – Aditya Jan 7 at 3:08
• @Aditya did you mean fullsquare ? but even if you did, then we only want to shift the box left, not center it... – Thruston Jan 7 at 12:47
• Yes, sorry, I meant full square. I see now that I misread your code. – Aditya Jan 7 at 16:35

Without calculating any rotation, you can use decorations.markings with transform shape.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns,   angles, quotes}
\usetikzlibrary{decorations.markings, arrows.meta, positioning}
\tikzset{mynode/.style={
inner sep=0pt,
text width=1cm,
minimum height=.5cm,
transform shape, draw, anchor=south}}

\begin{document}
\begin{tikzpicture}[>=Stealth]
\draw (2,3) coordinate (A) -- (8,0) coordinate (B)
-- (2,0) coordinate (C) pic ["$\theta$"'above left=-4pt and 12pt, draw, ->, angle radius=28pt] {angle}  -- cycle;

\path [decorate,
decoration={
markings,% switch on markings
mark=at position 0 with {\node[mynode, dashed]{};},
mark=at position 1 with {\node[mynode](V){};
\draw[->] ([yshift=4pt]V.north west) node[above=20pt,transform shape, midway] {$v$} -- ([yshift=4pt]V.north east);}}
]
(A) -- (B);

\fill[pattern=north west lines](C) rectangle ++(6,-1);

\draw[<->] ([xshift=-4pt]A) -- node[left] {$h$} ([xshift=-4pt]C);
\end{tikzpicture}
\end{document}