# How to ensure that perpendicular lines appear perpendicular in TikZ

I am drawing a figure to illustrate the relationship between a curve, and its tangent and normal lines at a point. Due to the nature of the curve (cubic polynomial), I'm manually adjusting the x- and y-scales so that the plot appears "nice". This adjustment affects the appearance of the angle at which the tangent and normal intersect. How may I fix it so that these lines appear perpendicular. Here is my MWE

\documentclass{article}
\usepackage{pgf,tikz,amsmath}
\begin{document}
Find the equation of the tangent and normal lines shown below.
\begin{center}
\begin{tikzpicture}[>=latex,x=2.5cm,y=0.35cm,font=\footnotesize]
\draw[thick,->,color=black] (-3,0) -- (3,0) node[above right] {$x$};
\draw[thick,->,color=black] (0,-15) -- (0,12) node[below right] {$y$};
\foreach \y in {-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8}
\draw[shift={(0,\y)},color=black] (-2pt,0pt);
\draw[thick,smooth,samples=100,domain=-2.1:2.2,color=black] plot(\x,{3*(\x)^3-(\x)^2-10*(\x)+2}) node[above] {$y=3x^3-x^2-10x+2$};
\draw[thick,smooth,samples=100,domain=0:3.0,color=blue] plot(\x,{-16+29*(\x)/4});
\draw[thick,smooth,samples=100,domain=0:3.0,color=red] plot(\x,{-1141/232-4*(\x)/29});
\draw[thick,dashed] (3/2,-41/8)--(3/2,0);
\node[above] at (3/2,0) {$\tfrac{3}{2}$};
\end{tikzpicture}
\end{center}
\end{document}


Technically the answer to your question is given in section 13.5.4 The Syntax of Distance Modifiers of pgfmanual v3.1.4b. Call the point where the tangent touches the curve p1 and another point on the tangent p2, then the normal is given by

\draw[red,thick]  ($(p1)!3cm!90:(p2)$)  -- (p1);


where 90 is the angle and 3cm the distance.

\documentclass{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{calc}
\begin{document}
Find the equation of the tangent and normal lines shown below.
\begin{center}
\begin{tikzpicture}[>=latex,x=2.5cm,y=0.35cm,font=\footnotesize]
\draw[thick,->,color=black] (-3,0) -- (3,0) node[above right] {$x$};
\draw[thick,->,color=black] (0,-15) -- (0,12) node[below right] {$y$};
\foreach \y in {-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8}
\draw[shift={(0,\y)},color=black] (-2pt,0pt);
\draw[thick,smooth,samples=100,domain=-2.1:2.2,color=black] plot(\x,{3*(\x)^3-(\x)^2-10*(\x)+2}) node[above] {$y=3x^3-x^2-10x+2$};
\draw[thick,blue] (0,{-16+29*(0)/4}) -- (3,{-16+29*(3)/4})
coordinate[pos=0] (p0) coordinate[pos=0.5] (p1) coordinate[pos=1] (p2);
\draw[red,thick]  ($(p1)!3cm!90:(p2)$)  -- (p1);
\draw[thick,dashed] (3/2,-41/8)--(3/2,0);
\node[above] at (3/2,0) {$\tfrac{3}{2}$};
\end{tikzpicture}
\end{center}
\end{document}


However, you want to let TikZ do all the hard work for you. This answer comes with a style that installs the tangent frame at a given point of the curve. (To avoid dimensions too large errors, we attach the tangent at a non-smooth plot that we do not draw.) The tangent is the x direction and the normal the y direction in a path in which the use tangent switches on the tangent frame. This is the result:

\documentclass{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{decorations.markings}
\begin{document}
Find the equation of the tangent and normal lines shown below.
\begin{center}
\begin{tikzpicture}[>=latex,x=2.5cm,y=0.35cm,font=\footnotesize,
tangent/.style={
decoration={
markings,% switch on markings
mark=
at position #1
with
{
\coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
\coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
\coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
}
},
postaction=decorate
},
use tangent/.style={
shift=(tangent point-#1),
x=(tangent unit vector-#1),
y=(tangent orthogonal unit vector-#1)
},
use tangent/.default=1]
\draw[thick,->,color=black] (-3,0) -- (3,0) node[above right] {$x$};
\draw[thick,->,color=black] (0,-15) -- (0,12) node[below right] {$y$};
\foreach \y in {-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8}
\draw[shift={(0,\y)},color=black] (-2pt,0pt);
\path[tangent=0.77,samples=101,domain=-2.1:2.2]plot(\x,{3*(\x)^3-(\x)^2-10*(\x)+2}) node[above] {$y=3x^3-x^2-10x+2$};
\draw[thick,smooth,samples=101,domain=-2.1:2.2,color=black] plot(\x,{3*(\x)^3-(\x)^2-10*(\x)+2}) node[above] {$y=3x^3-x^2-10x+2$};
\draw [blue, thick, use tangent] (-1,0) -- (1,0);
\draw [red, thick, use tangent] (0,0) -- (0,5);
\draw[thick,dashed] (3/2,-41/8)--(3/2,0);
\node[above] at (3/2,0) {$\tfrac{3}{2}$};
\end{tikzpicture}
\end{center}
\end{document}


• Just what I needed, thanks! – DJJerome Sep 23 '19 at 10:34