# Find cylinder tangent in 3D

I am trying to draw a complicated form that is loosely based on a half cylinder. I am using tikz-3dplot to be able to accurately draw arcs in planes other than the xy-plane and to nicely set the viewing angle. Now I need to find the tangent on that cylinder to be able to fill the front area that can be seen. Based on the accepted answer in this question, I tried to compute the angle at which the arc has its maximum in the direction perpendicular to the cylinder axis (here y). Unfortunately, I can not use this angle to draw the arc in the rotated coordinate system, since I would need to transform it before...

The following code should make things more clear. It draws axes for the normal and rotated coordinate systems and one arc of my cylindrical shape. I need to find the tangent to that arc to extrude the shape in direction of the y-axis.

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{55}{40}
\begin{tikzpicture}[tdplot_main_coords]

% find directions of projection
\path (1,0,0);
\pgfgetlastxy{\axisxx}{\axisxy}
\path (0,1,0);
\pgfgetlastxy{\axisyx}{\axisyy}
\path (0,0,1);
\pgfgetlastxy{\axiszx}{\axiszy}
% angle of tangent
\pgfmathsetmacro{\tangang}{atan(-\axisyy/\axisyx)+180}

\draw[->] (0, 0, 0) -- (4, 0, 0) node (x) [anchor=north] {x};
\draw[->] (0, 0, 0) -- (0, 4, 0) node (y) [anchor=north] {y};
\draw[->] (0, 0, 0) -- (0, 0, 4) node [anchor=east] (z) {z};

%set theta plane to xz-plane
\tdplotsetthetaplanecoords{0}

\draw[tdplot_rotated_coords, red, ->] (0, 0, 0) -- (2, 0, 0) node [anchor=east] (xx) {x'};
\draw[tdplot_rotated_coords, red, ->] (0, 0, 0) -- (0, 2, 0) node [anchor=north] (yy) {y'};

\draw[tdplot_rotated_coords] (0, 0, 0) + (90:5) arc (90:270:5);

\draw[red, ->] (0, 0) -- ++(7*\axisyy, -7*\axisyx);
\draw[tdplot_rotated_coords, red, dashed, ->] (0, 0, 0,) -- ++(\tangang:5);
\end{tikzpicture}
\end{document}


As you can see in the output of this code, the computed angle finds the position of the tangent nicely (red line), but if that angle is used in the rotated coordinate system, I do not get the right place to draw the tangent (dashed red line).

Does anybody know how to find the tangent while working with tikz-3dplot?

Unfortunately, things like the calc library only work in screen units and for this problem there is no reason to go back (although I did verify that the conversion works).

Instead I found the intersection of the perpendicular to the arc using the intersections tikzlibrary.

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,intersections}
\begin{document}
\tdplotsetmaincoords{55}{40}
\begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0, 0, 0) -- (4, 0, 0) node (x) [anchor=north] {x};
\draw[->] (0, 0, 0) -- (0, 4, 0) node (y) [anchor=north] {y};
\draw[->] (0, 0, 0) -- (0, 0, 4) node [anchor=east] (z) {z};

%set theta plane to xz-plane
\tdplotsetthetaplanecoords{0}

\draw[tdplot_rotated_coords, red, ->] (0, 0, 0) -- (2, 0, 0) node [anchor=east] (xx) {x'};
\draw[tdplot_rotated_coords, red, ->] (0, 0, 0) -- (0, 2, 0) node [anchor=north] (yy) {y'};

\draw[tdplot_rotated_coords, name path=myarc] (0, 0, 0) + (90:5) arc (90:270:5);

\path[name path=myline] (0,0,0) -- ($(0,0,0)!7!-90:(0,1,0)$);
\path[name intersections={of=myarc and myline}] coordinate (A) at (intersection-1);

\draw[->] (0,0,0) -- (A);

\draw (A) -- ++(0,4,0);

\end{tikzpicture}
\end{document}


• If I use the direction in screen coordinates, I can not draw the arc in the rotated coordinate system. This renders tikz-3dplot useless for this case, which would be a real bummer. Moreover, if I understand it right, I would have to calculate the radius, because i only know it in the rotated coordinate system? I would rather like to find the angle in the rotated coordinate system... – Dux Jul 1 '15 at 9:15
• Your approach of using the intersection is such a nice hack! It is probably the nicer solution if one does not need several tangents. – Dux Jul 1 '15 at 14:54

I think I found a solution that computes the actual angle:

I calculated the projections of the rotated axes similar to what I did with the original 3D coordinate system before. Then I computed the angle of the x-axis (here the y'-axis) to the horizontal of the image plane alpha_k. I used this angle to compute the right angle in the rotated coordinate system (alpha_k, rot).

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{55}{40}
\begin{tikzpicture}[tdplot_main_coords]

%set theta plane to xz-plane
\tdplotsetthetaplanecoords{0}

% find directions of projection
\path (1,0,0);
\pgfgetlastxy{\axisxx}{\axisxy}
\path (0,1,0);
\pgfgetlastxy{\axisyx}{\axisyy}
\path (0,0,1);
\pgfgetlastxy{\axiszx}{\axiszy}
% angle of tangent
\pgfmathsetmacro{\tanang}{atan(-\axisyy/\axisyx)+180}

% find directions of projection in rotated system
\path[tdplot_rotated_coords] (1,0,0);
\pgfgetlastxy{\raxisxx}{\raxisxy}
\path[tdplot_rotated_coords] (0,1,0);
\pgfgetlastxy{\raxisyx}{\raxisyy}
\path[tdplot_rotated_coords] (0,0,1);
\pgfgetlastxy{\raxiszx}{\raxiszy}
% angle of tangent
\pgfmathsetmacro{\rtang}{atan(-\raxiszy/\raxiszx)+180}
\pgfmathsetmacro{\angkorr}{atan(\raxisyy/\raxisyx)}
\pgfmathsetmacro{\rtanang}{\rtang+\angkorr/2}

\draw[->] (0, 0, 0) -- (4, 0, 0) node (x) [anchor=north] {x};
\draw[->] (0, 0, 0) -- (0, 4, 0) node (y) [anchor=north] {y};
\draw[->] (0, 0, 0) -- (0, 0, 4) node [anchor=east] (z) {z};
\draw[->, dashed] (0, 0) -- (4*\axisyy, -4*\axisyx);

\draw[tdplot_rotated_coords, red, ->] (0, 0, 0) -- (2, 0, 0)
node [anchor=east] (xx) {x'};
\draw[tdplot_rotated_coords, red, ->] (0, 0, 0) -- (0, 2, 0)
node [anchor=north] (yy) {y'};
\draw[tdplot_rotated_coords, red, dashed]
(0, 0) + (90:2) arc [start angle=90-\angkorr, delta angle=\angkorr, radius=2]
node [anchor=north west] (angkorr) {\tiny$\alpha_k$}
-- (0, 0);

\draw[tdplot_rotated_coords] (0, 0, 0) + (90:5) arc (90:270:5);

\draw[red, ->] (0, 0) -- ++(7*\axisyy, -7*\axisyx)
node [anchor=west] (tanang) {\tiny$\alpha_t$};
\draw[tdplot_rotated_coords, red, dashed, ->] (0, 0, 0,) -- ++(\tanang:5);
\draw[tdplot_rotated_coords, dashed, ->] (0, 0, 0,) -- ++(\rtang:5)
node [anchor=north] (tanangrot) {\tiny$\alpha'_t$};
\draw[tdplot_rotated_coords, dotted] (0, 0, 0,) -- ++(\rtanang:5)
node [anchor=west] (rtanang) {\tiny$\alpha_t^{rot} = \alpha_t+\alpha_k/2$}
-- ++(0, 0, 3);

\end{tikzpicture}
\end{document}


Note, that in the case presented here, since the rotated xy-plane is equal to the original xz-plane (theta equals 0 degrees), I would get the same result when starting with the projections of the original coordinate system. As soon as theta is different from zero however, I need to use the rotated axes.

I believe there is a tiny deviation between the angle in the original system and the rotated system. I am not sure yet whether this are calculation inaccuracies or if there is a small flaw in my line of thoughts...