On Sun, 23 Nov 2008 16:48:59 -0800
Zane Selvans <[EMAIL PROTECTED]> wrote:


> Incidentally, when you do it with ax.plot() instead you can see more  
> easily that the corners where the two sinusoidal functions intersect  
> are getting kind of chopped off by the polygon filling.  Don't know if  
> there's an easy way to fix that - maybe by forcing the list of polygon  
> vertices to always explicitly include the points of intersection  
> between the functions being filled_between?  Or maybe just by  
> increasing the number of vertices, though I assume that would slow  
> things down.

Some time back, I wrote code to generate a fairly complex polyfill in
a plot in R, 

http://www.oplnk.net/~ajackson/weather/Temperature_2000.png

The key bit of code to find the intersections between all the curves
to fill the corners correctly is here (note that this is "R" code):

intersect <- function(a, b, c, d) {
    #   test two line segments for intersection.
    #   modified from segseg in "Computational Geometry in C" 
    #   by Joseph O'Rourke

    p = c(0,0)

   denom = a[1] * ( d[2] - c[2] ) +
           b[1] * ( c[2] - d[2] ) +
           d[1] * ( b[2] - a[2] ) +
           c[1] * ( a[2] - b[2] );

   # If denom is zero, then segments are parallel: handle separately. 
   if (abs(denom) <= 1.0e-10) {
       return (c(p,0)) 
    }

   num =    a[1] * ( d[2] - c[2] ) +
            c[1] * ( a[2] - d[2] ) +
            d[1] * ( c[2] - a[2] );
   if ( (num == 0.0) || (num == denom) ) code = 0;
   s = num / denom;

   num = -( a[1] * ( c[2] - b[2] ) +
            b[1] * ( a[2] - c[2] ) +
            c[1] * ( b[2] - a[2] ) );
   if ( (num == 0.0) || (num == denom) ) code = 0;
   t = num / denom;

   if      ( (0.0 < s) && (s < 1.0) &&
             (0.0 < t) && (t < 1.0) ) {
     code = 1;
     }
   else if ( (0.0 > s) || (s > 1.0) ||
             (0.0 > t) || (t > 1.0) ) {
     code = 0;
     }

   p[1] = a[1] + s * ( b[1] - a[1] );
   p[2] = a[2] + s * ( b[2] - a[2] );

   c(p,code);
}

#   Intersect two lines defined by a series of segments. Assume the
#   lines have common x-values and differ only in y
#   intersect is array of (x,y) values

intersect.lines <- function (y1,y2,x) {
    intersect = array(data = NA, dim = c(2,0), dimnames = NULL)
    for (i in 2:length(x)) {
       a = c(x[i-1], y1[i-1])
       b = c(x[i], y1[i])
       c = c(x[i-1], y2[i-1])
       d = c(x[i], y2[i])
       foo = intersect(a,b,c,d)
       if (foo[3]) {
            intersect = cbind(intersect, foo[1:2])
       }
    }
    intersect
}



-- 
-----------------------------------------------------------------------
| Alan K. Jackson            | To see a World in a Grain of Sand      |
| [EMAIL PROTECTED]          | And a Heaven in a Wild Flower,         |
| www.ajackson.org           | Hold Infinity in the palm of your hand |
| Houston, Texas             | And Eternity in an hour. - Blake       |
-----------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to