Geometry problems become easier when you write a coordinate rule before a loop. A clockwise rotation sends a source cell to a specific destination. A rectangle intersection is the overlap of two independent coordinate ranges. A straight line is a normalized direction, including vertical lines and vectors pointing in opposite directions.
Prefer integer comparisons when the inputs are integers. Dividing slopes introduces rounding and vertical-line exceptions. Reducing a direction by its greatest common divisor gives an exact key. For a fence segment, that same divisor tells you how many primitive lattice steps fit between its endpoints.
Matrix traversal needs its own boundary discipline. A spiral can end with one row, one column, or one cell. Shrink one side after visiting it and check the remaining bounds before crossing the opposite side. Finally, test negative coordinates and degenerate shapes, where intuitive sketches often hide an equality case.
source (r,c) in a 3 by 3 matrix maps to (c,2-r).
The upper-left cell (0,0) moves to (0,2).
The lower-left cell (2,0) moves to (0,0).
Thus the first output row is the first source column read bottom to top.
Translate coordinate rules into exact integer calculations and matrix traversals.