1
$\begingroup$

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?.

These 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])}]]
$\endgroup$
  • 1
    $\begingroup$ Your question appears to be incomplete. I see no code that does any rotating of the plot. $\endgroup$ – m_goldberg Jan 13 at 0:00
  • $\begingroup$ 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. $\endgroup$ – bill s Jan 13 at 0:10
  • $\begingroup$ 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. $\endgroup$ – Jaime moreno zuleta Jan 13 at 7:24
2
$\begingroup$

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])}]]

enter image description here

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])}]]

enter image description here

Use θ = π/6 to get

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.