From 55cb971ae63fe8565f0f58da229b5dff3dac6d6e Mon Sep 17 00:00:00 2001
From: Phil Rosenberg <p.d.rosenberg@gmail.com>
Date: Mon, 15 Jun 2015 19:48:58 +0100
Subject: [PATCH] Change to the notcrossed function in plfill. The existing
 method sometimes flagged two lines as nearly parallel when they were actually
 nearly perpendicular.

---
 include/plplot.h | 10 ++++++----
 src/plfill.c     | 17 +++++++----------
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/include/plplot.h b/include/plplot.h
index 3b8eaf9..e6866a0 100644
--- a/include/plplot.h
+++ b/include/plplot.h
@@ -151,12 +151,14 @@
 
 #if defined ( PL_DOUBLE ) || defined ( DOUBLE )
 typedef double   PLFLT;
-#define PLFLT_MAX    DBL_MAX
-#define PLFLT_MIN    DBL_MIN
+#define PLFLT_MAX        DBL_MAX
+#define PLFLT_MIN        DBL_MIN
+#define PLFLT_EPSILON    DBL_EPSILON
 #else
 typedef float    PLFLT;
-#define PLFLT_MAX    FLT_MAX
-#define PLFLT_MIN    FLT_MIN
+#define PLFLT_MAX        FLT_MAX
+#define PLFLT_MIN        FLT_MIN
+#define PLFLT_EPSILON    FLT_EPSILON
 #endif
 
 #if ( defined ( PL_HAVE_STDINT_H ) && !defined ( __cplusplus ) ) || \
diff --git a/src/plfill.c b/src/plfill.c
index 6a91601..648d867 100644
--- a/src/plfill.c
+++ b/src/plfill.c
@@ -1940,7 +1940,7 @@ notcrossed( PLINT * xintersect, PLINT * yintersect,
             PLINT xA1, PLINT yA1, PLINT xA2, PLINT yA2,
             PLINT xB1, PLINT yB1, PLINT xB2, PLINT yB2 )
 {
-    PLFLT factor, factor_NBCC, fxintersect, fyintersect;
+    PLFLT factor, fxintersect, fyintersect, limit;
     // These variables are PLFLT not for precision, but to
     // avoid integer overflows if they were typed as PLINT.
     PLFLT xA2A1, yA2A1, xB2B1, yB2B1;
@@ -1969,15 +1969,12 @@ notcrossed( PLINT * xintersect, PLINT * yintersect,
     xB2B1 = xB2 - xB1;
     yB2B1 = yB2 - yB1;
 
-    factor      = xA2A1 * yB2B1 - yA2A1 * xB2B1;
-    factor_NBCC = PL_NBCC * ( fabs( xA2A1 ) + fabs( yB2B1 ) + fabs( yA2A1 ) + fabs( xB2B1 ) );
-    if ( fabs( factor ) <= factor_NBCC )
-    {
-        if ( fabs( factor ) > 0. )
-            status = status | PL_NEAR_PARALLEL;
-        else
-            status = status | PL_PARALLEL;
-    }
+    factor = xA2A1 * yB2B1 - yA2A1 * xB2B1;
+    limit  = PLFLT_EPSILON * 2. * MAX( xA2A1 * yB2B1, yA2A1 * xB2B1 );
+    if ( factor == 0 )
+        status = status | PL_PARALLEL;
+    else if ( fabs( factor ) < limit )
+        status = status | PL_NEAR_PARALLEL;
     else
     {
         xB1A1 = xB1 - xA1;
-- 
2.1.4

