Index: trunk/magic/remove/src/Line.c
===================================================================
--- trunk/magic/remove/src/Line.c	(revision 20308)
+++ trunk/magic/remove/src/Line.c	(revision 20579)
@@ -39,4 +39,37 @@
            (tuple->y >= line->end.y &&
             tuple->y <= line->begin.y);
+}
+
+double DistanceSquared (Line* line, double x, double y)
+{
+    // Define U as the vector from the line segment start to end
+    // Define V as the vector from the line segment start to the tuple
+    // Define W as the vector from the line segment end to the tuple
+
+    double ux, uy, vx, vy, b, px, py;
+    ux = line->end.x - line->begin.x;
+    uy = line->end.y - line->begin.y;
+    
+    vx = x - line->begin.x;
+    vy = y - line->begin.y;
+    
+    double u_u = ux * ux + uy * uy;                         // (u . u)
+    double u_v = ux * vx + uy * vy;                         // (u . v)
+    
+    if (u_v <= 0) return vx * vx + vy * vy;                 // (v . v)
+    if (u_u <= u_v)
+    {
+        double wx =  x - line->end.x;
+        double wy =  y - line->end.y;
+        return wx * wx + wy * wy;                           // (w . w)
+    }
+    
+    // Compute P(b) is the base of the perpendicular dropped from tuple to
+    // the line
+    
+    b = u_v / u_u;
+    px = vx - b * ux;
+    py = vy - b * uy;
+    return px * px + py * py;                               // norm (p)
 }
 
@@ -286,5 +319,5 @@
             for (y = yBegin; y != yEnd; ++y)
             {
-                if ((x * x + y * y) <= halfWidth2)
+                if (DistanceSquared (line, x, y) <= halfWidth2)
                 {
                     pixel = psAlloc (sizeof(PixelPos));
@@ -333,5 +366,5 @@
             for (x = xBegin; x != xEnd; ++x)
             {
-                if ((x * x + y * y) <= halfWidth2)
+                if (DistanceSquared (line, x, y) <= halfWidth2)
                 {
                     pixel = psAlloc (sizeof(PixelPos));
