Code

noop: CodingStyle: const placement
[inkscape.git] / src / sp-item-transform.cpp
index 2ad7758a46262720130862ae94502e9f73cb1ae0..93c3f5b7b410a15c83204283d93b9907cb34f57f 100644 (file)
@@ -97,9 +97,9 @@ get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool
     NR::Matrix scale = NR::Matrix (NR::scale (1, 1)); // scale component
     NR::Matrix unbudge = NR::Matrix (NR::translate (0, 0)); // move component to compensate for the drift caused by stroke width change
 
-    gdouble w0 = bbox.extent(NR::X);
+    gdouble w0 = bbox.extent(NR::X); // will return a value >= 0, as required further down the road
     gdouble h0 = bbox.extent(NR::Y);
-    gdouble w1 = x1 - x0;
+    gdouble w1 = x1 - x0; // can have any sign
     gdouble h1 = y1 - y0;
     gdouble r0 = strokewidth;
 
@@ -114,43 +114,42 @@ get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool
         return (p2o * direct * o2n); // can't solve the equation: one of the dimensions is equal to stroke width, so return the straightforward scaler
     }
 
-    // Flip when the width or height changes sign
-    int flip_x = ((w1 < 0) == (w0 < 0)) ? 1 : -1;
-    int flip_y = ((h1 < 0) == (h0 < 0)) ? 1 : -1;
+    int flip_x = (w1 > 0) ? 1 : -1;
+    int flip_y = (h1 > 0) ? 1 : -1;
     
-    // w1 and h1 can be negative, but if so then e.g. w1-r0 won't make sense
-    // therefore we should use fabs() all over the place
-    gdouble ratio_x = (fabs(w1) - fabs(r0)) / (fabs(w0) - fabs(r0));
-    gdouble ratio_y = (fabs(h1) - fabs(r0)) / (fabs(h0) - fabs(r0));    
+    // w1 and h1 will be negative when mirroring, but if so then e.g. w1-r0 won't make sense
+    // Therefore we will use the absolute values from this point on
+    w1 = fabs(w1);
+    h1 = fabs(h1);
+    r0 = fabs(r0);
+    // w0 and h0 will always be positive due to the definition extent()
+
+    gdouble ratio_x = (w1 - r0) / (w0 - r0);
+    gdouble ratio_y = (h1 - r0) / (h0 - r0);
     
-    NR::Matrix direct_constant_r = NR::Matrix (NR::scale(flip_x * ratio_x, flip_y*ratio_y));
+    NR::Matrix direct_constant_r = NR::Matrix (NR::scale(flip_x * ratio_x, flip_y * ratio_y));
 
     if (transform_stroke && r0 != 0 && r0 != NR_HUGE) { // there's stroke, and we need to scale it
         // These coefficients are obtained from the assumption that scaling applies to the
         // non-stroked "shape proper" and that stroke scale is scaled by the expansion of that
-        // matrix
-        // In fact, we're trying to solve this equation:
+        // matrix. We're trying to solve this equation:
         // r1 = r0 * sqrt (((w1-r0)/(w0-r0))*((h1-r1)/(h0-r0)))
-        // To make sense of this, the operant of the sqrt() should
-        // be positive, hence all the fabs() below
-        // (w1 and h1 will be negative when mirroring, w0 and h0 will probably never be negative)
-        gdouble A = -fabs(w0*h0) + fabs(r0)*(fabs(w0) + fabs(h0));
-        gdouble B = -(fabs(w1) + fabs(h1)) * r0*r0;
-        gdouble C = fabs(w1 * h1 * r0*r0);
+        // The operant of the sqrt() must be positive, which is ensured by the fabs() a few lines above
+        gdouble A = -w0*h0 + r0*(w0 + h0);
+        gdouble B = -(w1 + h1) * r0*r0;
+        gdouble C = w1 * h1 * r0*r0;
         if (B*B - 4*A*C > 0) {
-            gdouble r1 = (-B - sqrt (B*B - 4*A*C))/(2*A);
+            gdouble r1 = fabs((-B - sqrt(B*B - 4*A*C))/(2*A));
             //gdouble r2 = (-B + sqrt (B*B - 4*A*C))/(2*A);
             //std::cout << "r0" << r0 << " r1" << r1 << " r2" << r2 << "\n";
             //
-            // I think r1 will always be positive if r0 is (mathematical proof?)
-            // but if w1 becomes negative, then the scale will be wrong if we just do  
+            // If w1 < 0 then the scale will be wrong if we just do
             // gdouble scale_x = (w1 - r1)/(w0 - r0);
-            // gdouble scale_y = (h1 - r1)/(h0 - r0);
-            // So let's do it like this: Calculate the absolute scale
-            gdouble scale_x = (fabs(w1) - fabs(r1))/(fabs(w0) - fabs(r0));
-            gdouble scale_y = (fabs(h1) - fabs(r1))/(fabs(h0) - fabs(r0));
-            scale *= NR::scale(flip_x*scale_x, flip_y*scale_y);
-            unbudge *= NR::translate (-flip_x * 0.5 * (fabs(r0) * scale_x - fabs(r1)), -flip_y * 0.5 * (fabs(r0) * scale_y - fabs(r1)));
+            // Here we also need the absolute values of w0, w1, h0, h1, and r1
+            gdouble scale_x = (w1 - r1)/(w0 - r0);
+            gdouble scale_y = (h1 - r1)/(h0 - r0);
+            scale *= NR::scale(flip_x * scale_x, flip_y * scale_y);
+            unbudge *= NR::translate (-flip_x * 0.5 * (r0 * scale_x - r1), -flip_y * 0.5 * (r0 * scale_y - r1));
         } else {
             scale *= direct;
         }
@@ -159,7 +158,7 @@ get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool
             scale *= direct;
         } else {// nonscaling strokewidth
             scale *= direct_constant_r;
-            unbudge *= NR::translate (flip_x * 0.5 * fabs(r0) * (1 - ratio_x), flip_y * 0.5 * fabs(r0) * (1 - ratio_y));
+            unbudge *= NR::translate (flip_x * 0.5 * r0 * (1 - ratio_x), flip_y * 0.5 * r0 * (1 - ratio_y));
         }
     }