diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index c72b09bcbf..a783341f0e 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -304,7 +304,31 @@ flip-x? (and (neg? dot-x) (not (mth/close? dot-x 0 (* 1e-10 (gpt/length xv1) (gpt/length xv2))))) flip-y? (and (neg? dot-y) - (not (mth/close? dot-y 0 (* 1e-10 (gpt/length yv1) (gpt/length yv2)))))] + (not (mth/close? dot-y 0 (* 1e-10 (gpt/length yv1) (gpt/length yv2))))) + + ;; The edges' orientation reverses exactly under a reflection, which + ;; then flips one axis. Without area the edges cannot tell. + cross1 (- (* (:x xv1) (:y yv1)) (* (:y xv1) (:x yv1))) + cross2 (- (* (:x xv2) (:y yv2)) (* (:y xv2) (:x yv2))) + area? (not (or (mth/close? cross1 0 (* 1e-10 (gpt/length xv1) (gpt/length yv1))) + (mth/close? cross2 0 (* 1e-10 (gpt/length xv2) (gpt/length yv2))))) + mirror? (neg? (* cross1 cross2)) + + [flip-x? flip-y?] + (cond + (not area?) [flip-x? flip-y?] + (not mirror?) (if (and flip-x? flip-y?) [true true] [false false]) + (not= flip-x? flip-y?) [flip-x? flip-y?] + ;; A mirror axis at 45 degrees to the edges turns both by a right + ;; angle. Flip the axis that keeps the final turn along the new x + ;; edge: flipping y keeps the x edge's direction, flipping x + ;; reverses it. + :else + (let [turn (mth/radians (- (d/nilv (dm/get-in shape [:modifiers :rotation]) 0) + (d/nilv (:rotation shape) 0))) + sign (if (:flip-x shape) -1 1) + along (* sign (+ (* (mth/cos turn) (:x xv2)) (* (mth/sin turn) (:y xv2))))] + [(neg? along) (not (neg? along))]))] (cond-> shape flip-x? diff --git a/common/test/common_tests/geom_shapes_test.cljc b/common/test/common_tests/geom_shapes_test.cljc index f99b0f527d..5eb5ace5a6 100644 --- a/common/test/common_tests/geom_shapes_test.cljc +++ b/common/test/common_tests/geom_shapes_test.cljc @@ -301,3 +301,17 @@ (t/is (not (:flip-x after))) (t/is (not (:flip-y after))) (t/is (mth/close? 180 (:rotation after)))))) + +(t/deftest a-reflection-keeps-flags-and-rotation-consistent-with-the-transform + ;; At odd multiples of 45 degrees a reflection turns both edges by a + ;; right angle, so neither edge reverses. + (doseq [angle [30 45 135 225 315] + axis [(gpt/point -1 1) (gpt/point 1 -1)]] + (let [shape (create-test-shape :rect {:width 120 :height 140}) + rotated (gsh/transform-shape shape (ctm/rotation-modifiers shape (gsh/shape->center shape) angle)) + after (gsh/transform-shape rotated (ctm/resize-modifiers axis (gsh/shape->center rotated))) + scale (gpt/point (if (:flip-x after) -1 1) (if (:flip-y after) -1 1)) + implied (gmt/multiply (gmt/rotate-matrix (:rotation after)) (gmt/scale-matrix scale))] + (t/is (not= (boolean (:flip-x after)) (boolean (:flip-y after))) (str angle " " axis)) + (t/is (every? #(mth/close? (get implied %) (get (:transform after) %) 1e-9) [:a :b :c :d]) + (str angle " " axis)))))