# Problem with MousePosition after rotating a graphic

I've been doing a dynamic matrix plot using MousePosition but I need to rotate the plot 45°. but when I do that the MousePosition lose coordinates.

Any idea of how to do the rotation keeping MousePosition working?.

This is the code I´ve been using:

rowNumber[rowdim_] :=
If[# === None, #,
If[MatchQ[#, _?(1 <= # <= rowdim &)], #, None] &[
1 + rowdim - Last@Ceiling[#]]] &@MousePosition["Graphics"];
colNumber[coldim_] :=
If[# === None, #,
If[MatchQ[#, _?(1 <= # <= coldim &)], #, None] &[
First@Ceiling[#]]] &@MousePosition["Graphics"];

DynamicModule[{x = {}}, EventHandler[
Dynamic@MatrixPlot[
SparseArray[
Append[Map[# -> If[EvenQ@Count[x, #], 1, 0] &, x], {i_, j_} ->
1], {7, 7}],
Mesh -> All],
{"MouseClicked" :>
(x =
DeleteCases[
Append[x, {rowNumber[7],colNumber[7]}],
{a_, b_} /; a == None || b == None])}]]


• Your question appears to be incomplete. I see no code that does any rotating of the plot. Jan 13, 2019 at 0:00
• The OPs code draws a grid that you can click on. Each place you click, the color changes. But now you Rotate the image by 45 degrees (wrap around the MatrixPlot, for instance)... if you do so, then when you click on the boxes, the mouse is offset from location where the color changes. Jan 13, 2019 at 0:10
• Yes, i put the version without rotate[,45°]. Yeah i know it is an offset, but the problem is that there are parts of the rotated matrix plot that MousePosition don't recognize and give {None,None} so i can't fix that with just by finding the offset and aplying that offset to coordinates. Jan 13, 2019 at 7:24

You can apply RotationTransform[θ] to the graphics primitives produced by MatrixPlot and RotationTransform[-θ] to theMousePosition coordinates:

θ = π/4;
rowNumber[rowdim_] := If[# === None, #, If[MatchQ[#, _?(1 <= # <= rowdim &)], #, None] &[
1 + rowdim - Last@Ceiling[RotationTransform[-θ]@#]]] &[MousePosition["Graphics"]];
colNumber[coldim_] := If[# === None, #, If[MatchQ[#, _?(1 <= # <= coldim &)], #, None] &[
First@Ceiling[RotationTransform[-θ]@#]]] &@ MousePosition["Graphics"];
DynamicModule[{x = {}},
EventHandler[Dynamic@MapAt[GeometricTransformation[#, RotationTransform[θ]] &,
MatrixPlot[SparseArray[Append[Map[# -> If[EvenQ@Count[x, #], 1, 0] &, x],
{i_, j_} -> 1], {7, 7}], Mesh -> All, Frame -> False,
ImageSize -> 1 -> 50], {1}],
{"MouseClicked" :> (x = DeleteCases[Append[x, {rowNumber[7], colNumber[7]}], {a_, b_} /;
a == None || b == None])}]]


If you need the frame and frame ticks, you can overlay a rotated empty plot with the same image size setting:

DynamicModule[{x = {}}, EventHandler[Dynamic@Overlay[{Rotate[#, θ] &@
MatrixPlot[ConstantArray[0, {7, 7}], ImageSize -> 1 -> 50],
MapAt[GeometricTransformation[#, RotationTransform[θ]] &,
MatrixPlot[SparseArray[Append[Map[# -> If[EvenQ@Count[x, #], 1, 0] &, x],
{i_, j_} -> 1], {7, 7}], Mesh -> All, Frame -> False, ImageSize -> 1 -> 50], {1}]},
All, 2, Alignment -> Center],
{"MouseClicked" :> (x = DeleteCases[Append[x, {rowNumber[7], colNumber[7]}],
{a_, b_} /; a == None || b == None])}]]


Use θ = π/6 to get