[pstricks] Automate a line drawing routine

Michael Sharpe msharpe at ucsd.edu
Mon Dec 29 23:48:46 CET 2008


On Dec 29, 2008, at 12:27 PM, David Arnold wrote:

> Jurgen et al,
>
> I would not pick points P and Q that lie on the boundary. So I would  
> want the arrows at the end of the lines where the lines exit on a  
> boundary of the grid.
>

Here's a really clumsy solution that uses only TeX and pstricks--- 
doubtless it would have been better to craft one in PostScript.

Michael

\documentclass[dvips]{article}
\usepackage{pstricks-add}
\newcount\cntc
\newcount\cntd
\newdimen\dima
\newdimen\dimb
\newdimen\dimc
\newdimen\dimd
\newdimen\dime
%\newdimen\dimf
\newdimen\dimg
\newdimen\dimh
\pssetlength\dimg{5.01}
\dimh=-\dimg
\newdimen\slope
\newdimen\islope%inverse slope
\newcount\cntA\newcount\cntB
\def\getends(#1,#2)(#3,#4){%P=(#1,#2), Q=(#3,#4)
\psdots[linecolor=blue,dotscale=1.5](#1,#2)(#3,#4)
\cntc=#3\advance\cntc by -#1
\cntd=#4\advance\cntd by -#2
\pssetlength\dima{#1}
\pssetlength\dimb{#2}
\pssetlength\dimc{\the\cntc}%x1-x0
\pssetlength\dimd{\the\cntd}%y1-y0
\divide\dimd by \cntc %slope now in \dimd
\slope=\dimd%
\divide\dimc by \cntd %inverse slope now in dimc
\islope=\dimc
%compute intersection with x=5: y0+slope*(5-x0)
\cntA=5\advance\cntA by -#1 %(5-x0)
\dime=\dimb\advance\dime by \the\cntA\slope%y0+slope*(5-x0)
\ifdim\dime<\dimg %
   \ifdim\dime>\dimh %
     \pnode(5,\dime){P\the\cntB}\advance\cntB by 1 %
   \fi%
\fi
%compute intersection with x=-5: y0-slope*(5+x0)
\cntA=5\advance\cntA by #1 %(5+x0)
\dime=\dimb\advance\dime by -\the\cntA\slope%y0-slope*(5+x0)
\ifdim\dime<\dimg %
   \ifdim\dime>\dimh%
     \pnode(-5,\dime){P\the\cntB}\advance\cntB by 1 %
   \fi%
\fi
%compute intersection with y=5: x0+islope*(5-y0)
\cntA=5\advance\cntA by -#2 %(5-y0)
\dime=\dima\advance\dime by \the\cntA\islope%x0+islope*(5-y0)
\ifdim\dime<\dimg %
   \ifdim\dime>\dimh %
     \pnode(\dime,5){P\the\cntB}\advance\cntB by 1 %
   \fi%
\fi
%compute intersection with y=-5: x0-islope*(5+y0)
\cntA=5\advance\cntA by #2 %(5-y0)
\dime=\dima\advance\dime by -\the\cntA\islope%x0-islope*(5+y0)
\ifdim\dime<\dimg %
   \ifdim\dime>\dimh %
     \pnode(\dime,-5){P\the\cntB}\advance\cntB by 1 %
   \fi%
\fi%
}
\begin{document}
\begin{pspicture}(-6,-6)(6,6)
\SpecialCoor
\def\labelFontSize{\scriptscripstyle}
\psset{arrows=<->,arrowscale=2,unit=.5cm,mathLabel=true}
\psgrid[gridcolor=lightgray,gridlabels=0pt,subgriddiv=1](0,0)(-5,-5) 
(5,5)
\psaxes[Dx=5,Dy=5,showorigin=false](0,0)(-5.5,-5.5)(5.5,5.5)
\uput[0](5.5,0){$x$}
\uput[90](0,5.5){$y$}
\getends(-1,-3)(2,4)
\psline(P0)(P1)
\end{pspicture}
\end{document}


More information about the PSTricks mailing list