Line clipping function

*To*: mathgroup at smc.vnet.net*Subject*: [mg35384] Line clipping function*From*: Selwyn Hollis <slhollis at earthlink.net>*Date*: Wed, 10 Jul 2002 02:20:18 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

This is a follow-up to a recent thread regarding PlotVectorField. To make a long story short, I was trying to superimpose numerically generated curves (from ListPlot) over a vector field (from PlotVectorfield). At first I used PlotRange to get the rectangle I wanted, but that caused problems with the vector field. Anyway, it turned out that the best remedy was to clip the generated data so that the curves would not spill out of the rectangle. With the much appreciated help of Herr Hartmut Wolf, I ended up using the following functions to do the line clipping. I thought it worthwhile to present these "for the record". ----------------------------------------- Attributes[pull]={HoldFirst}; pull[z_, end_, xory_, k_, bounds_]:= With[ {r=(bounds[[xory,k]]-z[[2end,xory]])/(z[[end,xory]]-z[[2end,xory]])}, z[[end]]=r*z[[end]]+(1-r)*z[[2end]]]; clip[zIn_, bounds_]:= Module[{z=zIn}, Scan[Function[end, If[z[[end,1]]<bounds[[1,1]], pull[z,end,1,1,bounds], If[z[[end,1]]>bounds[[1,-1]], pull[z,end,1,-1,bounds]]]; If[z[[end,-1]]<bounds[[-1,1]], pull[z,end,-1,1,bounds], If[z[[end,-1]]>bounds[[-1,-1]], pull[z,end,-1,-1,bounds]]];], {1,-1}]; z]; Example: datapts = Join[{{Random[Real,{-1,0}],Random[Real,{-1,0}]}}, Table[{0.1*i, Random[Real,{0,1}]}, {i,1,9}], {{Random[Real,{1,2}], Random[Real,{1,2}]}}] ListPlot[datapts, PlotJoined -> True, PlotRange -> All]; clippeddatapts= clip[datapts, {{0, 1}, {0, 1}}] ListPlot[clippeddatapts, PlotJoined -> True, PlotRange -> All]; ----------------------------------------- Selwyn Hollis slhollis at mac.com http://www.math.armstrong.edu/faculty/hollis