%! % --- procedure defined -------------------------------------------------- % [A B C] [[..][..] ...] -> intersection of line with closed polygon p /intersection { 1 dict begin /p exch def /n p length def /f exch def /A f 0 get def /B f 1 get def /C f 2 get def /P p n 1 sub get def /fP P 0 get A mul P 1 get B mul add C add def /i 0 def [ n { % look at each side in turn /Q p i get def /fQ Q 0 get A mul Q 1 get B mul add C add def fP fQ mul 0 lt { % an interior intersection /s fQ fQ fP sub div def /t 1 s sub def [ P 0 get s mul Q 0 get t mul add P 1 get s mul Q 1 get t mul add ] }{ fQ 0 eq { Q } if } ifelse /i i 1 add def /fP fQ def /P Q def } repeat ] end } def % --- switching coordinate systems ---------------------------------------- /user-to-default-matrix { matrix currentmatrix matrix defaultmatrix matrix invertmatrix matrix concatmatrix } def % [A B C] M /line-transform { 1 dict begin matrix invertmatrix /M exch def aload pop /C exch def /B exch def /A exch def % M = % [ a' b' 0 % c' d' 0 % e' f' 1 ] [ M 0 get A mul M 1 get B mul add M 2 get A mul M 3 get B mul add M 4 get A mul M 5 get B mul add C add ] end } def % --- sample code --------------------------------------------------------- % page-setup -------------------------------------------------------------- /S 100 def % specify the page size in the default coordinates /w 72 8.5 mul def /h 72 11 mul def /square [[-1 -1][1 -1][1 1][-1 1]] def /page-begin { gsave w 2 div h 2 div translate S dup scale 1 S div setlinewidth /M user-to-default-matrix def gsave 1 0 0 setrgbcolor newpath -8 0 moveto 8 0 lineto 0 -8 moveto 0 8 lineto stroke grestore newpath square 3 get aload pop moveto square { aload pop lineto } forall closepath stroke } def /page-end { % display the equation of the line /Helvetica-Bold findfont 0.16 scalefont setfont -2 -3 moveto ell 0 get ( ) cvs show (x + ) show ell 1 get ( ) cvs show (y + ) show ell 2 get ( ) cvs show ( = 0) show grestore showpage } def % --- drawing the line ------------------------------------------------ -4 1 4 { /i exch def page-begin % specify the line to be drawn /ell [2 1 i neg] def ell M line-transform % the line in default coordinates [[0 0][w 0][w h][0 h]] intersection /a exch def a length 1 gt { newpath a 0 get aload pop M itransform moveto a 1 get aload pop M itransform lineto stroke } if ell square intersection /b exch def b { newpath aload pop 0.025 0 360 arc fill } forall page-end } for