Index: trunk/magic/remove/src/diffedpixels.c
===================================================================
--- trunk/magic/remove/src/diffedpixels.c	(revision 27341)
+++ trunk/magic/remove/src/diffedpixels.c	(revision 27342)
@@ -240,5 +240,5 @@
 ocur at pt0.y and pt2.y
 
-Example 1
+Type 1
 
                 3
@@ -250,5 +250,5 @@
                    1                                line 2_3  y >= pt.y
 **************************
-Example 2
+
                 3
               C
@@ -260,5 +260,5 @@
 
 **************************
-Example 3
+If The left side corners have the same x we also have a Type 1
 
         3       2
@@ -282,6 +282,4 @@
 nameCorners(psPlane pt[4])
 {
-    psPlane   pt0, pt1, pt2, pt3;
-
     // sort points top to bottom
     int i;
@@ -297,51 +295,37 @@
         }
     }
+    psPlane bl, br;
     // sort bottom two points in x
     if (pt[3].x < pt[2].x) {
-        psPlane tmpPt = pt[3];
-        pt[3] = pt[2];
-        pt[2] = tmpPt;
-    }
-    // now we know that p[3] point is pt1 - the lower right corner
-    pt1 = pt[3];
-    
-    // And pt[2] is left most of the bottom 2  (either pt0 or pt2)
-
-    // find which of the top two is to the left
+        bl = pt[3];
+        br = pt[2];
+    } else {
+        bl = pt[2];
+        br = pt[3];
+    }
+    // sort the top two points in x
+    psPlane tl, tr;
     if (pt[0].x < pt[1].x) {
-        pt3 = pt[0];
-        // now decide which of pt[1] and pt[2] is pt0 - on the left)
-        if (pt[1].x < pt[2].x) {
-            pt0 = pt[1];
-            pt2 = pt[2];
-        } else {
-            pt0 = pt[2];
-            pt2 = pt[1];
-        }
-    } else {
-        pt3 = pt[1];
-        // now decide which of pt[0] and pt[2] is pt0 - on the left)
-        if (pt[0].x < pt[2].x) {
-            pt0 = pt[0];
-            pt2 = pt[2];
-        } else {
-            pt0 = pt[2];
-            pt2 = pt[0];
-        }
-    }
-
-    /* now write the outputs back to the input array*/
-    pt[0] = pt0;
-    pt[1] = pt1;
-    pt[2] = pt2;
-    pt[3] = pt3;
-
-    // And Check the results
-    // y3 > y0
-    // y2 > y0
-    // y3 > y1
-    // x1 > x0
-    // x2 > x0
-    // x2 > x3
+        tl = pt[0];
+        tr = pt[1];
+    } else {
+        tl = pt[1];
+        tr = pt[0];
+    }
+    if (tl.y >= tr.y) {
+        // Type 1 (or 3 which is equivalent)
+        pt[0] = bl;
+        pt[1] = br;
+        pt[2] = tr;
+        pt[3] = tl;
+    } else {
+        pt[0] = tl;
+        pt[1] = bl;
+        pt[2] = br;
+        pt[3] = tr;
+    }
+
+    // Now check the results match our requirements
+    // pt3 is above pt0
     if (pt[3].y <= pt[0].y) {
         fprintf(stderr, "ERROR calculating diff overlap\n");
@@ -349,9 +333,5 @@
         streaksExit("", PS_EXIT_PROG_ERROR);
     }
-    if (pt[2].y <= pt[0].y) {
-        fprintf(stderr, "ERROR calculating diff overlap\n");
-        fprintf(stderr, "pt[2].y (%f) <= pt[0].y (%f)\n", pt[2].y, pt[0].y);
-        streaksExit("", PS_EXIT_PROG_ERROR);
-    }
+    // pt3 is above pt1
     if (pt[3].y <= pt[1].y) {
         fprintf(stderr, "ERROR calculating diff overlap\n");
@@ -359,4 +339,11 @@
         streaksExit("", PS_EXIT_PROG_ERROR);
     }
+    // pt2 is above pt1
+    if (pt[2].y <= pt[1].y) {
+        fprintf(stderr, "ERROR calculating diff overlap\n");
+        fprintf(stderr, "pt[2].y (%f) <= pt[1].y (%f)\n", pt[2].x, pt[1].x);
+        streaksExit("", PS_EXIT_PROG_ERROR);
+    }
+    // pt1 is to the right of pt0
     if (pt[1].x <= pt[0].x) {
         fprintf(stderr, "ERROR calculating diff overlap\n");
@@ -364,4 +351,5 @@
         streaksExit("", PS_EXIT_PROG_ERROR);
     }
+    // pt2 is to the right of pt0
     if (pt[2].x <= pt[0].x) {
         fprintf(stderr, "ERROR calculating diff overlap\n");
@@ -369,4 +357,5 @@
         streaksExit("", PS_EXIT_PROG_ERROR);
     }
+    // pt2 is to the right of pt3
     if (pt[2].x <= pt[3].x) {
         fprintf(stderr, "ERROR calculating diff overlap\n");
@@ -374,3 +363,9 @@
         streaksExit("", PS_EXIT_PROG_ERROR);
     }
-}
+    // pt2 is below or at the same y as pt3
+    if (pt[2].y > pt[3].y) {
+        fprintf(stderr, "ERROR calculating diff overlap\n");
+        fprintf(stderr, "pt[2].y (%f) > pt[3].y (%f)\n", pt[2].y, pt[3].y);
+        streaksExit("", PS_EXIT_PROG_ERROR);
+    }
+}
