# [pstricks] tangent and orthogonal line

Herbert Voss Herbert.Voss at FU-Berlin.DE
Mon Apr 6 08:37:54 CEST 2009

Hi all,
I played a bit with tangent and orthogonal lines of a curve
given by several points and not a function. The following
example can draw a tangent or an othogonal line to a curve
defined by three points. Parameters are the coordinates of
the three points, the x value, and dx

If it may be useful, we can put it into one of the packages.

Herbert

\documentclass{minimal}
\usepackage{pst-3dplot}%  for the linear equation system solver
\makeatletter
\def\psOrthLine{\pst at object{psOrthLine}}
\def\psOrthLine at i(#1,#2)(#3,#4)(#5,#6)#7#8{%
\begin at OpenObj%
[[#1 dup dup mul exch 1 #2]
[#3 dup dup mul exch 1 #4]
[#5 dup dup mul exch 1 #6]]
tx at 3DPlotDict begin SolveLinEqSystem end %
/abc ED	     	   % save the solution as array
abc aload pop    	   % a b c on stack
exch #7          	   % a c b x
mul add exch     	   % c+b*x a
#7 dup mul mul add 	   % a*x^2+b*x+c
/y0 ED 		   % save value
abc aload pop pop exch % b a
#7 mul 2 mul add       % b+2*a*x0
neg 1 exch div         % mOrth
#8 mul /dy ED          % mOrth*dx
[
#7 #8 add y0 dy add \tx at ScreenCoor % x0+dx y0 +dy
#7 y0 \tx at ScreenCoor   % x0 y0
\ifshowpoints true \else false \fi
\tx at setlinejoin 	   % 0 1 or 2	       %
\tx at Line }%            % load the pro function
\end at OpenObj\ignorespaces}
\def\psTangentLine{\pst at object{psTangentLine}}
\def\psTangentLine at i(#1,#2)(#3,#4)(#5,#6)#7#8{%
\begin at OpenObj%
[[#1 dup dup mul exch 1 #2]
[#3 dup dup mul exch 1 #4]
[#5 dup dup mul exch 1 #6]]
tx at 3DPlotDict begin SolveLinEqSystem end
/abc ED
abc aload pop    % a b c on stack
exch #7          % a c b x
mul add exch     % c+b*x a
#7 dup mul mul add /y0 ED
abc aload pop pop exch  % b a
#7 mul 2 mul add        % b+2*a*x0=mTan
dup                     % mTan mTan
#8 mul /dy1 ED          % mTan*dx
#8 neg mul /dy2 ED      % mTan*-dx
[
#7 #8 add y0 dy1 add \tx at ScreenCoor % x0+dx y0 +dy1
#7 #8 sub y0 dy2 add \tx at ScreenCoor % x0-dx y0 +dy2
\ifshowpoints true \else false \fi
\tx at setlinejoin 		       %
\tx at Line }%
\end at OpenObj\ignorespaces}
\makeatother
\begin{document}

\psset{unit=2}
\begin{pspicture}[showgrid=true](1,-1)(4,1)
\pscurve[showpoints=true](2.1,-0.2)(2.5,0.2)(3.2,0.235)(3.8,-0.2)
\psOrthLine[arrows=->,linecolor=red](2.5,0.2)(3.2,0.235)(3.8,-0.2){3}{0.1}

\psTangentLine[arrows=<->,linecolor=blue](2.5,0.2)(3.2,0.235)(3.8,-0.2){3}{0.5}
\end{pspicture}

\end{document}