I am trying to draw an experimental setup involving a horseshoe magnet. I am having difficulty of hiding lines that should not appear, and I am unsure how to use shading to indicate the different surfaces. I would like the following TikZ picture to have the horseshoe (draw with thick and red options) to not be a net and be like a solid. Here is my code:
\documentclass{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{tikz-3dplot}
\begin{document}
\Large
\tdplotsetmaincoords{60}{55}
\begin{tikzpicture}[scale=15,tdplot_main_coords]
\tdplotsetrotatedcoords{0}{90}{90}
\begin{scope}[tdplot_rotated_coords]
\draw[thick,red] (-0.05,0.1,0.15) -- (-0.05,-0.05,0.15) .. controls +(0,-0.1,0) and +(0,-0.1,0) .. (0.05,-0.05,0.15) -- (0.05,0.1,0.15);
\draw[thick,red] (-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) .. controls +(0,-0.2,0) and +(0,-0.2,0) .. (0.15,-0.05,0.15) -- (0.15,0.1,0.15);
\draw[thick,red] (-0.05,0.1,.25) -- (-0.05,-0.05,0.25) .. controls +(0,-0.1,0) and +(0,-0.1,0) .. (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- (0.15,0.1,0.25) -- (0.15,0.1,0.15);
\draw[thick,red] (-0.15,0.1,0.25) -- (-0.15,-0.05,0.25) .. controls +(0,-0.2,0) and +(0,-0.2,0) .. (0.15,-0.05,0.25) -- (0.15,0.1,0.25);
\draw[thick,red,fill=white] (0.05,0.1,0.25) -- (0.15,0.1,0.25) -- (0.15,0.1,0.15) -- (0.05,0.1,0.15) -- (0.05,0.1,0.25);
\draw[fill=black!10] (0,0,0) -- (0,0,.4) -- (0,.3,.4) -- (0,.3,0) -- (0,0,0);
\draw[thick,red] (-0.05,0.1,0.25) -- (-0.05,-0.05,0.25);
\draw[thick,red,fill=white] (-0.05,0.1,0.25) -- (-0.05,0.1,0.15) -- (-0.15,0.1,0.15) -- (-0.15,0.1,0.25) -- (-0.05,0.1,0.25);
\draw (0,.3,0) -- +(0,.2,.1);
\draw (0,.3,.4) -- +(0,.2,-0.1);
\node at (-0.075,.05,.25) {N};
\draw[thick,fill=white] (0,0.125,0.175) -- (0,0.175,0.175) -- (0,0.175,0.225) -- (0,0.125,0.225) -- (0,0.125,0.175);
\draw[thick] (0,0.125,0.175) -- (0,0.175,0.225);
\draw[thick] (0,0.175,0.175) -- (0,00.125,0.225);
\end{scope}
\end{tikzpicture}
\end{document}
Which produces
Also is it possible to make the curved inner and outer surfaces a particular shade of grey, and the other flat surfaces another shade of grey?
EDIT: Here is the final picture which would not have been possible without Marmot's answer (see below). It includes two more diagrams from the front and side. I think it is a good showcase of the abilities of TiKz-3d
\documentclass{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
% fix wrong implementation of xy canvas https://tex.stackexchange.com/a/48776/121799
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
\def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
\def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
\def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
\tikz@canvas@is@plane
}
\tikzoption{canvas is yz plane at x}[]{%
\def\tikz@plane@origin{\pgfpointxyz{#1}{0}{0}}%
\def\tikz@plane@x{\pgfpointxyz{#1}{0}{0}}%
\def\tikz@plane@y{\pgfpointxyz{#1}{1}{0}}%
\tikz@canvas@is@plane}
\makeatother
\begin{document}
\tdplotsetmaincoords{60}{55}
\begin{tikzpicture}[scale=15]
\tdplotsetrotatedcoords{0}{90}{90}
\path[use as bounding box] (-0.1,-0.3) rectangle (0.6,0.4);
\begin{scope}[tdplot_rotated_coords]
% inner u right half
\draw (0.05,0.1,0.15) -- (0.05,-0.05,0.15) --
plot[variable=\x,domain=360:180,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.15)
-- (-0.05,-0.05,0.15) -- (-0.05,-0.05,0.25) --
plot[variable=\x,domain=180:360,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.25)
-- (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- cycle;
\shade[top color=gray!20,bottom color=gray!80,middle color=gray!20]
(0.05,0.1,0.15) -- (0.05,-0.05,0.15) --
plot[variable=\x,domain=360:180,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.15)
-- (-0.05,-0.05,0.15) -- (-0.05,-0.05,0.25) --
plot[variable=\x,domain=180:360,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.25)
-- (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- cycle;
% filled u in the front
\draw[fill=gray!30,canvas is xy plane at z=0.25]
(-0.15,0.1) -- (-0.15,-0.05) arc(180:360:0.15) -- (0.15,-0.05) -- (0.15,0.1) --
(0.05,0.1) -- (0.05,-0.05) arc(360:180:0.05) -- (-0.05,-0.05) -- (-0.05,0.1) -- cycle;
% top square on the right
\draw[fill=gray!10] (0.05,0.1,0.25) -- (0.15,0.1,0.25) -- (0.15,0.1,0.15) -- (0.05,0.1,0.15) -- (0.05,0.1,0.25);
% plane
\draw[fill=black!10] (0,0,0) -- (0,0,.4) -- (0,.3,.4) -- (0,.3,0) -- (0,0,0);
% left front triangle
\draw[fill=gray!30] (-0.15,0.1,0.25) -- (-0.05,0.1,0.25) -- (-0.05,-0.05,0.25);
% top square on the left
\draw[fill=gray!10] (-0.05,0.1,0.25) -- (-0.05,0.1,0.15) -- (-0.15,0.1,0.15) -- (-0.15,0.1,0.25) -- (-0.05,0.1,0.25);
% face on the left
\draw (-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) --
plot[variable=\x,domain=180:225,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.15)
--
plot[variable=\x,domain=225:180,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.25)
--(-0.15,-0.05,0.25) -- (-0.15,0.1,0.25) -- cycle;
\shade[top color=gray!20,bottom color=gray!80,middle color=gray!20]
(-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) --
plot[variable=\x,domain=180:225,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.15)
--
plot[variable=\x,domain=225:180,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.25)
--(-0.15,-0.05,0.25) -- (-0.15,0.1,0.25) -- cycle;
% further elements
\draw (0,.3,0) -- +(0,.2,.1);
\draw (0,.3,.4) -- +(0,.2,-0.1);
\node[canvas is xy plane at z=0.25,transform shape,scale=0.2]
at (-0.1,0.05) {N};
\draw[thick,fill=white] (0,0.125,0.175) -- (0,0.175,0.175) -- (0,0.175,0.225) -- (0,0.125,0.225) -- (0,0.125,0.175);
\draw[thick] (0,0.125,0.175) -- (0,0.175,0.225);
\draw[thick] (0,0.175,0.175) -- (0,00.125,0.225);
\draw[thick,red,-latex] (0,0.125,0.175) -- +(-0.05,0,0) node[above] {$z$};
\draw[thick,blue,-latex] (0,0.125,0.175) -- +(0,.1,0) node[left] {$y$};
\draw[thick,-latex] (0,0.125,0.175) -- +(0,0,.1) node[below] {$x$};
\end{scope}
\tdplotsetmaincoords{90}{90}
\tdplotsetrotatedcoords{0}{90}{90}
\begin{scope}[xshift=0.4cm,yshift=0.2cm,tdplot_rotated_coords,scale=0.5]
\draw (0.05,0.1,0.15) -- (0.05,-0.05,0.15) --
plot[variable=\x,domain=360:180,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.15)
-- (-0.05,-0.05,0.15) -- (-0.05,-0.05,0.25) --
plot[variable=\x,domain=180:360,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.25)
-- (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- cycle;
\shade[top color=gray!20,bottom color=gray!80,middle color=gray!20]
(0.05,0.1,0.15) -- (0.05,-0.05,0.15) --
plot[variable=\x,domain=360:180,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.15)
-- (-0.05,-0.05,0.15) -- (-0.05,-0.05,0.25) --
plot[variable=\x,domain=180:360,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.25)
-- (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- cycle;
% filled u in the front
\draw[fill=gray!30,canvas is xy plane at z=0.25]
(-0.15,0.1) -- (-0.15,-0.05) arc(180:360:0.15) -- (0.15,-0.05) -- (0.15,0.1) --
(0.05,0.1) -- (0.05,-0.05) arc(360:180:0.05) -- (-0.05,-0.05) -- (-0.05,0.1) -- cycle;
% top square on the right
\draw[fill=gray!10] (0.05,0.1,0.25) -- (0.15,0.1,0.25) -- (0.15,0.1,0.15) -- (0.05,0.1,0.15) -- (0.05,0.1,0.25);
% plane
\draw[fill=black!10] (0,0,0) -- (0,0,.4) -- (0,.3,.4) -- (0,.3,0) -- (0,0,0);
% left front triangle
\draw[fill=gray!30] (-0.15,0.1,0.25) -- (-0.05,0.1,0.25) -- (-0.05,-0.05,0.25);
% top square on the left
\draw[fill=gray!10] (-0.05,0.1,0.25) -- (-0.05,0.1,0.15) -- (-0.15,0.1,0.15) -- (-0.15,0.1,0.25) -- (-0.05,0.1,0.25);
% face on the left
\draw (-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) --
plot[variable=\x,domain=180:225,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.15)
--
plot[variable=\x,domain=225:180,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.25)
--(-0.15,-0.05,0.25) -- (-0.15,0.1,0.25) -- cycle;
\shade[top color=gray!20,bottom color=gray!80,middle color=gray!20]
(-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) --
plot[variable=\x,domain=180:225,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.15)
--
plot[variable=\x,domain=225:180,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.25)
--(-0.15,-0.05,0.25) -- (-0.15,0.1,0.25) -- cycle;
% further elements
\node[canvas is xy plane at z=0.25,transform shape,scale=0.2]
at (-0.1,0.05) {N};
\draw[thick,red,-latex] (0,0.125,0.175) -- +(-0.1,0,0) node[above] {$z$};
\draw[thick,blue,-latex] (0,0.125,0.175) -- +(0,.1,0) node[left] {$y$};
\end{scope}
\tdplotsetmaincoords{90}{0}
\tdplotsetrotatedcoords{0}{90}{90}
\begin{scope}[xshift=0.3cm,yshift=-0.2cm,tdplot_rotated_coords,scale=0.5]
\draw (0.05,0.1,0.15) -- (0.05,-0.05,0.15) --
plot[variable=\x,domain=360:180,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.15)
-- (-0.05,-0.05,0.15) -- (-0.05,-0.05,0.25) --
plot[variable=\x,domain=180:360,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.25)
-- (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- cycle;
\shade[top color=gray!20,bottom color=gray!80,middle color=gray!20]
(0.05,0.1,0.15) -- (0.05,-0.05,0.15) --
plot[variable=\x,domain=360:180,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.15)
-- (-0.05,-0.05,0.15) -- (-0.05,-0.05,0.25) --
plot[variable=\x,domain=180:360,smooth] ({0.05*cos(\x)},{-0.05+0.05*sin(\x)},0.25)
-- (0.05,-0.05,0.25) -- (0.05,0.1,0.25) -- cycle;
% filled u in the front
\fill[gray!30,canvas is xy plane at z=0.25]
(-0.15,0.1) -- (-0.15,-0.05) arc(180:360:0.15) -- (0.15,-0.05) -- (0.15,0.1) --
(0.05,0.1) -- (0.05,-0.05) arc(360:180:0.05) -- (-0.05,-0.05) -- (-0.05,0.1) -- cycle;
% top square on the right
\draw[fill=gray!10] (0.05,0.1,0.25) -- (0.15,0.1,0.25) -- (0.15,0.1,0.15) -- (0.05,0.1,0.15) -- (0.05,0.1,0.25);
% plane
\draw[fill=black!10] (0,0,0) -- (0,0,.4) -- (0,.3,.4) -- (0,.3,0) -- (0,0,0);
% left front triangle
\draw[fill=gray!30] (-0.15,0.1,0.25) -- (-0.05,0.1,0.25) -- (-0.05,-0.05,0.25);
% top square on the left
\draw[fill=gray!10] (-0.05,0.1,0.25) -- (-0.05,0.1,0.15) -- (-0.15,0.1,0.15) -- (-0.15,0.1,0.25) -- (-0.05,0.1,0.25);
% face on the left
\draw (-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) --
plot[variable=\x,domain=180:225,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.15)
--
plot[variable=\x,domain=225:180,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.25)
--(-0.15,-0.05,0.25) -- (-0.15,0.1,0.25) -- cycle;
\shade[top color=gray!20,bottom color=gray!80,middle color=gray!20]
(-0.15,0.1,0.15) -- (-0.15,-0.05,0.15) --
plot[variable=\x,domain=180:225,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.15)
--
plot[variable=\x,domain=225:180,smooth] ({0.15*cos(\x)},{-0.05+0.15*sin(\x)},0.25)
--(-0.15,-0.05,0.25) -- (-0.15,0.1,0.25) -- cycle;
% further elements
\draw[thick,fill=white] (0,0.125,0.175) -- (0,0.175,0.175) -- (0,0.175,0.225) -- (0,0.125,0.225) -- (0,0.125,0.175);
\draw[thick] (0,0.125,0.175) -- (0,0.175,0.225);
\draw[thick] (0,0.175,0.175) -- (0,00.125,0.225);
\draw[thick,blue,-latex] (0,0.125,0.175) -- +(0,.1,0) node[left] {$y$};
\draw[thick,-latex] (0,0.125,0.175) -- +(0,0,.1) node[below] {$x$};
\node[anchor=center,scale=1.5] at (-0.25,0.05,0.2) {N};
\end{scope}
\end{tikzpicture}
\end{document}
Note that the labelled axis are not in the correct order, the x
and z
axis have been swapped because that is how I referenced them in my formulae for my report.