5

I would like to "write" on to a matrix using the mouse. That is, I would like to use the integer value of Locator coordinates (p) to index a matrix. Then, if the mouse button is down, write a 1 in that location. That approach doesn't seem to work since, when attempting to do something like M[[Sequence @@ Round [p]]], the coordinate p is actually a dynamic quantity, which can't be converted to two integer indices by Sequence @@ Round.

Is there any way to do this? or, Is there an equivalent workaround that gets the same result. The simplest example would be using the mouse to input a freehand black and white drawing as pixels on a matrix, which could later be subject to image processing algorithms.

user2238171
  • 162
  • 7

1 Answers1

5
Manipulate[x = ConstantArray[0, 9 {1, 1}];
 Row[{EventHandler[Dynamic[tds = Reverse[Transpose[x]];
     MatrixPlot[tds, PlotRangePadding -> 0, Mesh -> All, 
      ImageSize -> {300, 300}, 
      ColorRules -> {1 -> Black, 
        0 -> None}]], {"MouseClicked" :> (pos = 
        Ceiling[MousePosition["Graphics"]];
       x = ReplacePart[x, pos -> 1 - x[[Sequence @@ pos]]];)}], 
   Dynamic@Magnify[MatrixForm[tds], 2]}], {pos, 
  ControlType -> None}, {x, ControlType -> None}, {tds, 
  ControlType -> None}, AppearanceElements -> None, FrameMargins -> 0]

enter image description here

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355