# How to draw a square on cylinder?

Let be a cylinder with radius of the base circle is equal to 3 and the altitude of the cylinder is equal to 2. How can I draw square ABCD so that A, B and C, D lie on two base circles. I tried

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d,calc,backgrounds,patterns}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{2}
\def\angB{0}
\def\phi{120}
\def\angA{{\angB + \phi}}
\def\angC{\phi - 180}
\tdplotsetmaincoords{65}{100}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\begin{scope}[canvas is xy plane at z=0]
\draw[dashed] (\tdplotmainphi:\myr) arc(\tdplotmainphi:\tdplotmainphi+180:\myr);
\coordinate (O) at (0,0);
\coordinate (A) at (\angA:\myr);
\coordinate (B) at (\angB:\myr);
\draw[thick] (\tdplotmainphi:\myr) coordinate(BR) arc(\tdplotmainphi:\tdplotmainphi-180:\myr)
coordinate(BL);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h]
\coordinate (O') at (0,0);
\coordinate (C) at (\angC:\myr);
\coordinate (D) at ($(A) + (C) -(B)$);
\draw [thick](BR) -- (\tdplotmainphi:\myr) (BL) -- (\tdplotmainphi-180:\myr);
\end{scope}
\fill (A) circle[radius=1pt] node[above] {$A$};
\fill (B) circle[radius=1pt] node[above] {$B$};
\fill (C) circle[radius=1pt] node[above] {$C$};
\fill (D) circle[radius=1pt] node[above] {$D$};
\draw[dashed]  (C) -- (B) --   (A)  -- (D) ;
\draw[] (C) -- (D);
\end{tikzpicture}
\end{document}


This code don't true when I change \def\angB{0} into \def\angB{30}. How to get a general way?

• Just by looking at your figure, I would say you have done it. Could you write something about, why you are not satisfied? – hpekristiansen Aug 21 at 2:13
• I drew it randomly. I can't prove ABCD is a square. – Thuy Nguyen Aug 21 at 2:33

Consider three points on the square, say B, C and D. Without loss of generality we can assume that they have the coordinates

 C = (r \cos \phi,r \sin\phi,h)
D = (r \cos (180-\phi),r \sin(180-\phi),h)
B = (r \cos \phi,-r \sin\phi,0)


The conditions that they form a square mean that

 CD = BC


where

 CD = 2r \cos \phi
BC = \sqrt{(2r \sin\phi)^2+h^2}


This yields

 \phi = \acos(h^2/(4r^2))/2


This confirms hpechristiansens numerical result:

 CD = 2 r \cos \left(\frac{1}{2} \acos\left(\frac{h^2}{4 r^2}\right)\right)


MWE

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d,calc,backgrounds,patterns}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{2}
\pgfmathsetmacro{\angA}{acos(\h*\h/(4*\myr*\myr))/2}
\tdplotsetmaincoords{65}{100}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\begin{scope}[canvas is xy plane at z=0]
\draw[dashed] (\tdplotmainphi:\myr) arc(\tdplotmainphi:\tdplotmainphi+180:\myr);
\coordinate (O) at (0,0);
\coordinate (A) at (\angA:\myr);
\coordinate (B) at (180-\angA:\myr);
\draw[thick] (\tdplotmainphi:\myr) coordinate(BR) arc(\tdplotmainphi:\tdplotmainphi-180:\myr)
coordinate(BL);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h]
\coordinate (O') at (0,0);
\coordinate (C) at (-180+\angA:\myr);
\coordinate (D) at (-\angA:\myr);
\draw [thick](BR) -- (\tdplotmainphi:\myr) (BL) -- (\tdplotmainphi-180:\myr);
\end{scope}
\fill (A) circle[radius=1pt] node[above] {$A$};
\fill (B) circle[radius=1pt] node[above] {$B$};
\fill (C) circle[radius=1pt] node[above] {$C$};
\fill (D) circle[radius=1pt] node[above] {$D$};
\draw[dashed]  (C) -- (B) --   (A);
\draw[thick] (A) -- (D) -- (C);
\end{tikzpicture}
\end{document}


• The point A in your code is depend on two numbers \h and \myr. Please let it move :) – minhthien_2016 Aug 21 at 2:59
• I have not looked at the math at all, but I think that there is something wrong - it seems the 'square' is too vertical and thereby not square. – hpekristiansen Aug 21 at 3:00
• @hpekristiansen You are right. I selected the wrong solution. Now the derivation is even simpler. I confirm your result after putting in the numbers (but have an analytic expression). – Schrödinger's cat Aug 21 at 3:17

In this code, the point B can run everywhere on the circle. You can change it at \def\angB{-20}, then you have an option of the square ABCD.

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d,calc,backgrounds,patterns}
\usepackage{fouriernc}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{2}
\pgfmathsetmacro{\d}{sqrt(\h*\h+4*\myr*\myr)/sqrt(2)}
\def\angB{-20}
\def\angA{{\angB + acos((2*\myr*\myr-\d*\d)/(2*\myr*\myr)}}
\tdplotsetmaincoords{65}{100}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\begin{scope}[canvas is xy plane at z=0]
\draw[dashed] (\tdplotmainphi:\myr) arc(\tdplotmainphi:\tdplotmainphi+180:\myr);
\coordinate (O) at (0,0);
\coordinate (A) at (\angA:\myr);
\coordinate (B) at (\angB:\myr);
\coordinate (A') at ($2*(O) - (A)$);
\coordinate (B') at ($2*(O) - (B)$);

\draw[thick] (\tdplotmainphi:\myr) coordinate(BR) arc(\tdplotmainphi:\tdplotmainphi-180:\myr)
coordinate(BL);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h]
\coordinate (O') at (0,0);
\coordinate (C) at  ($(O') - (O) +(A')$);
\coordinate (D) at  ($(O') - (O) +(B')$);
\draw [thick](BR) -- (\tdplotmainphi:\myr) (BL) -- (\tdplotmainphi-180:\myr);
\end{scope}
\foreach \v/\position in { B/below,O/below,A/below,B'/above,A'/above,C/above,D/above} {\draw[draw =black, fill=black] (\v) circle (1pt) node [\position=0.2mm] {$\v$};
}
\draw[thick]    (C) -- (D) ;
\draw[dashed] (A) -- (A') (B) -- (B') (A) -- (B) -- (A') -- (B') -- cycle (B') -- (D) (A') -- (C) (A) --  (D) (B) -- (C);
\end{tikzpicture}
\end{document}


With some calculations by using Maple, coordinates of point A, B, C, D can be choose

then, the code

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\pgfmathsetmacro{\myr}{3}
\pgfmathsetmacro{\h}{2}
\tdplotsetmaincoords{65}{100}
\begin{tikzpicture}[tdplot_main_coords,scale=1,line cap=butt,line join=round]
\begin{scope}[canvas is xy plane at z=0]
\draw[dashed] (\tdplotmainphi:\myr) arc(\tdplotmainphi:\tdplotmainphi+180:\myr);
\coordinate (O) at (0,0);
\coordinate (A) at ({1/4*sqrt(8*\myr*\myr-2*\h*\h)}, {sqrt(1/2*\myr*\myr+(1/8)*\h*\h)});
\coordinate (B) at ({1/4*sqrt(8*\myr*\myr-2*\h*\h)}, -{sqrt(1/2*\myr*\myr+(1/8)*\h*\h)});
\draw[thick] (\tdplotmainphi:\myr) coordinate(BR) arc(\tdplotmainphi:\tdplotmainphi-180:\myr)
coordinate(BL);
\end{scope}
\begin{scope}[canvas is xy plane at z=\h]
\coordinate (O') at (0,0);
\coordinate (C) at ({-1/4*sqrt(8*\myr*\myr-2*\h*\h)}, -{sqrt(1/2*\myr*\myr+(1/8)*\h*\h)});
\coordinate (D) at ({-1/4*sqrt(8*\myr*\myr-2*\h*\h)}, {sqrt(1/2*\myr*\myr+(1/8)*\h*\h)});
\foreach \v/\position in { B/below,O/below,A/below,C/above,D/above} {\draw[draw =black, fill=black] (\v) circle (1pt) node [\position=0.2mm] {$\v$};
`