# [pstricks] Automate a line drawing routine

Herbert Voss Herbert.Voss at FU-Berlin.DE
Wed Dec 31 11:30:13 CET 2008

Herbert Voss schrieb:
> David Arnold schrieb:
>
>> Case 1: (-1,-1), (2,4)
>>
>> This is the case in your code. The line enters on the bottom boundary
>> and leaves on the top boundary.
>>
>> Case 2: (-4,-2), (4,2)
>>
>> The line enters on the left boundary and leaves on the right boundary.
>>
>> Case 3: (-3,-4), (4,2)
>>
>> The line enters on the bottom boundary and leaves on the right boundary.
>>
>> Case 4: (-4,-2), (2,4)
>
> David,
> only my own last example with a negative scope went
> wrong. But I don't know why. However, this should work
> for you:

this should fit all cases

and a happy new year to all of you, at least for all people
in the far east, the other ones have nearly one day in the
old year :-)

Herbert

\documentclass{article}
\def\LowerL{-5,-5} \def\UpperR{5,5}
\makeatletter
\def\psGetSlope(#1,#2)(#3,#4)#5{%
\pst at dimm=#1pt%
\advance\pst at dimm by -#3pt%
\pst at dimn=#2pt%
\advance\pst at dimn by -#4pt
\pst at divide{\pst at dimn}{\pst at dimm}#5}
%
\def\psSlope{\pst at object{psSlope}}
\def\psSlope at i(#1,#2)(#3,#4){%
\begin at OpenObj% 0-1 lower left  2-3 upper right
\psline{*-*}(#1,#2)(#3,#4)%
\ifdim#2pt=#4pt\psline(\LowerL|#1,#2)(\UpperR|#3,#4)
\else\ifdim#1pt=#3pt\psline(#1,#2|\LowerL)(#3,#4|\UpperR)
\else
\psGetSlope(#1,#2)(#3,#4)\pst at tempA \pst at dimm=\pst at tempA\p@
\psIntersectionPoint(#1,#2)(#3,#4)(\LowerL)(\UpperR|\LowerL){P at 0}%bottom
\ifdim\pst at dimm>\z@
\psIntersectionPoint(#1,#2)(#3,#4)(\LowerL)(\LowerL|\UpperR){P at 1}%left

\psIntersectionPoint(#1,#2)(#3,#4)(\UpperR|\LowerL)(\UpperR){P at 3}%right
\else%

\psIntersectionPoint(#1,#2)(#3,#4)(\UpperR|\LowerL)(\UpperR){P at 1}%right
\psIntersectionPoint(#1,#2)(#3,#4)(\LowerL)(\LowerL|\UpperR){P at 3}%left
\fi%
\psIntersectionPoint(#1,#2)(#3,#4)(\LowerL|\UpperR)(\UpperR){P at 2}%top
\psline{*-*}(#1,#2)(#3,#4)%
\psline(!\psGetNodeCenter{P at 0} P at 0.x P at 0.y Pyth
\psGetNodeCenter{P at 1} P at 1.x P at 1.y Pyth
lt {P at 0.x P at 0.y}{P at 1.x P at 1.y} ifelse)%
(!\psGetNodeCenter{P at 2} P at 2.x P at 2.y Pyth
\psGetNodeCenter{P at 3} P at 3.x P at 3.y Pyth
lt {P at 2.x P at 2.y}{P at 3.x P at 3.y} ifelse)%
\fi\fi%
\end at OpenObj%
\ignorespaces}
\makeatother
\begin{document}

\def\labelFontSize{\scriptscripstyle}
%\psset{unit=.5cm}
\begin{pspicture}(-5.5,-5.5)(6,6)
\psgrid[gridcolor=black!15,gridlabels=0pt,
subgriddiv=1](0,0)(-5,-5)(5,5)%
\psaxes[Dx=5,Dy=5,showorigin=false,arrowscale=2]{<->}%
(0,0)(-5.5,-5.5)(5.5,5.5)[$x$,0][$y$,90]%
\psset{arrowscale=1.7,arrows=<->}%
\psSlope[linecolor=red](-1,-1)(2,4)
\psSlope[linecolor=green](-4,-2)(4,2)
\psSlope[linecolor=blue](-3,-4)(4,2)
\psSlope[linecolor=magenta](-4,-2)(2,4)
\psSlope[linecolor=cyan](-2,2)(1,4)
\psSlope[linestyle=dashed](2,2)(4,1)
\psSlope[linestyle=dashed,linecolor=yellow](-3,4)(1,-4)
\psSlope[linewidth=1.5pt](4,-2)(4,2)
\psSlope[linewidth=1.5pt](-4,-4)(4,-4)
\end{pspicture}

\end{document}