[pstricks] Automate a line drawing routine

Herbert Voss Herbert.Voss at FU-Berlin.DE
Tue Dec 30 22:50:58 CET 2008


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:

\documentclass{article}
\usepackage{pstricks-add}
\def\LowerL{-5,-5} \def\UpperR{5,5}
\makeatletter
\def\psSlope{\pst at object{psSlope}}
\def\psSlope at i(#1,#2)(#3,#4){%
  \begin at ClosedObj% 0-1 lower left  2-3 upper right
  \psIntersectionPoint(#1,#2)(#3,#4)(\LowerL)(\UpperR|\LowerL){P at 0}%bottom
  \psIntersectionPoint(#1,#2)(#3,#4)(\LowerL)(\LowerL|\UpperR){P at 1}%left
  \psIntersectionPoint(#1,#2)(#3,#4)(\LowerL|\UpperR)(\UpperR){P at 2}%top
  \psIntersectionPoint(#1,#2)(#3,#4)(\UpperR|\LowerL)(\UpperR){P at 3}%right
  \psline{*-*}(#1,#2)(#3,#4)
%\psdots[dotstyle=x,dotscale=2](P at 0)(P at 1)(P at 2)(P at 3)
% now find the points with shortest distance to (0,0)
  \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)
  \end at ClosedObj%
  \ignorespaces}
\makeatother
\begin{document}

%\psset{unit=.5cm}
\begin{pspicture}(-5.5,-5.5)(6,6)
\def\labelFontSize{\scriptscripstyle}
\psgrid[gridcolor=lightgray,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(2,2)(4,1)
\end{pspicture}

\end{document}

Herbert


More information about the PSTricks mailing list