Index: lib/matplotlib/collections.py
===================================================================
RCS file: /cvsroot/matplotlib/matplotlib/lib/matplotlib/collections.py,v
retrieving revision 1.19
diff -u -r1.19 collections.py
--- lib/matplotlib/collections.py	31 Mar 2005 19:34:59 -0000	1.19
+++ lib/matplotlib/collections.py	20 May 2005 10:18:37 -0000
@@ -444,3 +444,18 @@
             verts = self._segments[i%Nsegments]
             vertsall.extend([(x+ox, y+oy) for x,y in verts])
         return vertsall
+
+    def get_lines(self):
+        'return seq of lines in collection'
+        if self._offsets is None:
+            offsets = [(0,0)]
+        else:
+            offsets = self._offsets
+        Noffsets = len(offsets)
+        Nsegments = len(self._segments)
+        lines = []
+        for i in range(max(Noffsets, Nsegments)):
+            ox, oy = offsets[i%Noffsets]
+            segment = self._segments[i%Nsegments]
+            lines.append([(x+ox, y+oy) for x,y in segment])
+        return lines
Index: lib/matplotlib/legend.py
===================================================================
RCS file: /cvsroot/matplotlib/matplotlib/lib/matplotlib/legend.py,v
retrieving revision 1.27
diff -u -r1.27 legend.py
--- lib/matplotlib/legend.py	15 Apr 2005 16:02:13 -0000	1.27
+++ lib/matplotlib/legend.py	20 May 2005 10:18:37 -0000
@@ -37,6 +37,55 @@
 from transforms import Bbox, Point, Value, get_bbox_transform, bbox_all,\
      unit_bbox, inverse_transform_bbox, lbwh_to_bbox
 
+
+def segments_intersect(s1, s2):
+    """ Return True if s1 and s2 intersect. """
+    (x1, y1), (x2, y2) = s1
+    (x3, y3), (x4, y4) = s2
+
+    den = ((y4-y3) * (x2-x1)) - ((x4-x3)*(y2-y1))
+
+    n1 = ((x4-x3) * (y1-y3)) - ((y4-y3)*(x1-x3))
+    n2 = ((x2-x1) * (y1-y3)) - ((y2-y1)*(x1-x3))
+
+    if den == 0:
+        # lines parallel
+        return False
+
+    u1 = n1/den
+    u2 = n2/den
+
+    return 0.0 <= u1 <= 1.0 and 0.0 <= u2 <= 1.0
+
+
+def line_cuts_bbox(line, bbox):
+    """ Return True if and only if line cuts bbox. """
+    minx, miny, width, height = bbox.get_bounds()
+    maxx = minx + width
+    maxy = miny + height
+
+    n = len(line)
+    if n == 0:
+        return False
+
+    if n == 1:
+        return bbox.contains(line[0])
+    
+    p1 = line[0]
+    for p2 in line[1:]:
+        segment = (p1, p2)
+        # See if the segment cuts any of the edges of bbox
+        for edge in (((minx, miny), (minx, maxy)),
+                     ((minx, miny), (maxx, miny)),
+                     ((maxx, miny), (maxx, maxy)),
+                     ((minx, maxy), (maxx, maxy))):
+            if segments_intersect(segment, edge):
+                return True
+        p1=p2
+
+    return False
+
+
 class Legend(Artist):
     """
     Place a legend on the axes at location loc.  Labels are a
@@ -276,6 +325,7 @@
         handles = get_handles(ax)
         vertices = []
         bboxes = []
+        lines = []
 
         inv = ax.transAxes.inverse_xy_tup
         for handle in handles:
@@ -289,7 +339,7 @@
 
                 # XXX need a special method in transform to do a list of verts
                 averts = [inv(v) for v in zip(xt, yt)]
-                vertices.extend(averts)
+                lines.append(averts)
 
             elif isinstance(handle, Patch):
 
@@ -304,13 +354,14 @@
                 bboxes.append(bbox)
 
             elif isinstance(handle, LineCollection):
-                verts = handle.get_verts()
+                hlines = handle.get_lines()
                 trans = handle.get_transform()
-                tverts = trans.seq_xy_tups(verts)
-                averts = [inv(v) for v in tverts]
-                vertices.extend(averts)
-
-        return [vertices, bboxes]
+                for line in hlines:
+                    tline = trans.seq_xy_tups(line)
+                    aline = [inv(v) for v in tline]
+                    lines.extend(line)
+                    
+        return [vertices, bboxes, lines]
 
     def draw_frame(self, b):
         'b is a boolean.  Set draw frame to b'
@@ -384,7 +435,7 @@
         lower-left corner of the legend. All are axes coords.
         """
 
-        verts, bboxes = self._auto_legend_data()
+        verts, bboxes, lines = self._auto_legend_data()
 
         consider = [self._loc_to_axes_coords(x, width, height) for x in range(1, len(self.codes))]
 
@@ -400,6 +451,10 @@
                 if legendBox.overlaps(bbox):
                     badness += 1
 
+            for line in lines:
+                if line_cuts_bbox(line, legendBox):
+                    badness += 1
+
             if badness == 0:
                 return ox, oy
 
