[pstricks] Automate a line drawing routine

Juergen Gilg gilg at acrotex.net
Wed Dec 31 10:49:04 CET 2008


Dear David,

here a refinement - only for positive slopes however:

\documentclass{article}
\usepackage{pstricks,pstricks-add}
\begin{document}
\pagestyle{empty}
\newcommand{\posSlope}[4]{%
\psset{unit=0.5}
\begin{pspicture}(-6,-6)(6,6)
\psgrid[gridcolor=lightgray,gridlabels=0pt,subgriddiv=1](0,0)(-5,-5)(5,5)
\psaxes[labels=none,ticks=none]{->}(0,0)(-5.5,-5.5)(5.5,5.5)
\Cnode[radius=0.05cm,fillstyle=solid,fillcolor=blue,linecolor=blue](#1,#2){A}
\Cnode[radius=0.05cm,fillstyle=solid,fillcolor=blue,linecolor=blue](#3,#4){B}
\psplot[arrows=<->,linecolor=blue]%
{#4 #2 sub #3 #1 sub div  #2 5 add #1 5 add div ge {5 neg #2 sub #3 #1 
sub #4 #2 sub div mul #1 add} {-5} ifelse}%
{#4 #2 sub #3 #1 sub div  5 #4 sub 5 #3 sub div ge {5 #2 sub #3 #1 sub 
#4 #2 sub div mul #1 add} {5} ifelse}%
{#4 #2 sub #3 #1 sub div x #1 sub mul #2 add}
\uput[0](5.5,0){$x$}
\uput[90](0,5.5){$y$}
\end{pspicture}
\resetOptions
}

%Point A must be left of point B (#1 < #3)!!!
%NO VERTICAL LINES possible with that approach.
\posSlope{-4}{-2}{2}{4} \posSlope{-4}{-2}{4}{2}
%First Argument     x-Koordinate Point A
%Second Argument    y-Koordinate Point A
%Third Argument     x-Koordinate Point B
%Fourth Argument    y-Koordinate Point B

\posSlope{-2}{-4}{2}{4} \posSlope{-2}{-4}{4}{2}

\posSlope{-2}{-2}{2}{2} \posSlope{-2}{4}{4}{4}
\end{document}

Regards,

Juergen

Michael Sharpe wrote:
> On Dec 30, 2008, at 3:09 PM, Herbert Voss wrote:
>
>   
>> Michael Sharpe schrieb:
>>
>>     
>>> I think there's a problem here that goes back to the code for
>>> \psIntersectionPoint, which gives a Postscript division by 0  error
>>> where the lines are parallel. This makes your macro fail when the  
>>> line
>>> is either horizontal or vertical. (Try \psSlope(-4,2)(4,2).)
>>>       
>> Michael,
>> this is not the problem with my example \psSlope(2,2)(4,1)
>> it is the negative slope, which needs the nodes sorted in another
>> order.
>>
>> Herbert
>>
>>
>>     
>>> I think there needs to be a test in the code for the case where the
>>> absolute value of the determinant of dX1 dY1//dX2 dY2 is small-- 
>>> say, <.
>>> 01 times the area of ABCD, and in this case, the intersection could  
>>> be
>>> declared to be 100 times the larger of (dX1,dY1), (dX2,dY2) plus the
>>> basepoint. The current code tests only for dX1 or dX2 small, which is
>>> not sufficient to prevent PostScript errors.
>>>       
>
> Herbert,
>
> The problem happens only when you have too lines that are parallel and  
> horizontal.
>
> Try this: I get a PostScript error due to division by 0:
>
> \pnode(1,1){P1}\pnode(3,1){P2}\pnode(1,2){P3}\pnode(4,2){P4}
> \psIntersectionPoint(P1)(P2)(P3)(P4){Q}
>
> Now try this modification of your \psIntersectionPoint code, which  
> implements what I wrote earlier:
>
> \makeatletter
> \def\mypsIntersectionPoint(#1)(#2)(#3)(#4)#5{%
>      \pst at getcoor{#1}\pst at tempA
>      \pst at getcoor{#2}\pst at tempB
>      \pst at getcoor{#3}\pst at tempC
>      \pst at getcoor{#4}\pst at tempd
> \pnode(!%
>       \pst at tempA /YA exch \pst at number\psyunit div def
>       /XA exch \pst at number\psxunit div def
>       \pst at tempB /YB exch \pst at number\psyunit div def
>       /XB exch \pst at number\psxunit div def
>       \pst at tempC /YC exch \pst at number\psyunit div def
>       /XC exch \pst at number\psxunit div def
>       \pst at tempd /YD exch \pst at number\psyunit div def
>       /XD exch \pst at number\psxunit div def
>      /dY1 YB YA sub def
>      /dX1 XB XA sub def
>      /dY2 YD YC sub def
>      /dX2 XD XC sub def
>      /XAC XC XA sub def
>      /YAC YC YA sub def
>      /det dX1 dY2 mul dX2 dY1 mul sub def % determinant
>      /Leng1 dX1 dY1 Pyth def
>      /Leng2 dX2 dY2 Pyth def
>      Leng1 Leng2 mul .01 mul det abs gt %angle between lines <.01  
> radians
>      {Leng1 Leng2 lt {100 dY2 mul 100 dX2 mul XC add exch YC add}
>      {100 dY1 mul 100 dX1 mul XA add exch YA add} ifelse}
>      %else angle between not small
>      {XAC dY2 mul YAC dX2 mul sub det div dup dX1 mul XA add exch dY1  
> mul YA add }
>        ifelse
>      ){#5}%
> }
> \makeatother
>
> \pnode(1,1){P1}\pnode(3,1){P2}\pnode(1,2){P3}\pnode(4,2){P4}
> \psIntersectionPoint(P1)(P2)(P3)(P4){Q}
>
> This time, I get no error message---the intersection of these parallel  
> line has been declared to be far away on the longer line segment,  
> safely out of the picture. Perhaps the 100 should be 1000, and .01  
> radians should be .001 radians.)
>
> Michael
> _______________________________________________
> PSTricks mailing list
> PSTricks at tug.org
> http://tug.org/mailman/listinfo/pstricks
>
>
>   

-- 
 Jürgen Gilg
 Austr. 59
 70376 Stuttgart
-------------------------------------
 Tel       0711.59 27 88
 e-Mail    gilg at acrotex.net
 Websites  www.acrotex.net
           www.brueckenkurs-physik.de
           www.gilligan-online.de

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://tug.org/pipermail/pstricks/attachments/20081231/0eef3f7b/attachment.html 


More information about the PSTricks mailing list