From 81fc3008b36a681fe9301c81820440ed678f0792 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sat, 31 Oct 2026 08:05:18 +0100 Subject: [PATCH] :bug: Flip one axis when a reflection turns both edges a right angle A transform finds a reflection by comparing each edge with its image: a reversed edge toggles that axis's flip and negates the rotation. A mirror at 45 degrees to the edges turns both edges by a right angle, so neither reverses. Flipping a shape rotated by an odd multiple of 45 degrees left its flags and rotation unflipped while its transform was mirrored, or set both flags, which describe a turn. Tell a reflection by the reversed orientation of the edges. A reflection flips exactly one axis; when neither edge reverses, flip the one that keeps the final rotation along the new x edge. Without area, keep comparing edges alone. A fresh JVM swap run (seed 20261101) found this: a rotated, switched board was flipped, and its copies later failed I4 on rotation. The regression fails at 45, 135, 225 and 315 degrees on both axes before the fix and passes after it. The full common JVM suite passes at seed 712: 2067 tests / 33827 assertions. Compare lower-quantile milliseconds with the saved baseline; keep that baseline unchanged. The machine/JVM warning makes ratios rough. No world is flagged. World Baseline Now Ratio Main into 10 copies 1.790 0.910 0.51 Main into 100 copies 15.415 9.264 0.60 Main into 1000 copies 153.086 91.084 0.59 Three nesting levels 35.215 18.132 0.51 Dependency chain 68.931 35.513 0.52 Library update 83.083 43.320 0.52 Many plain shapes 4.837 2.291 0.47 Variant switch 32.052 20.011 0.62 AI-assisted-by: claude-opus-5-5 --- .../app/common/geom/shapes/transforms.cljc | 26 ++++++++++++++++++- .../test/common_tests/geom_shapes_test.cljc | 14 ++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) 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)))))