简体   繁体   English

在绘图过程中使用定位器坐标为矩阵建立索引

[英]Using Locator coordinates to index a matrix during drawing

I would like to "draw" into a matrix in Mathematica. 我想“画”成Mathematica中的矩阵。 That is, I would like to use the mouse to (say, using EventHandler) change the value at a matrix index to 1 from, say, 0. The simplest approach would seem to be to use Locator coordinates, p, take the Rounded value of those coordinates, and assign the matrix location at that value with 1. This does not appear to work because the form "Sequence @@ Round[p]" in M[[Sequence @@ Round[p]]]=1 does not result in a proper index because p is itself a Dynamic quantity (as can be seen using FullForm). 也就是说,我想使用鼠标(例如,使用EventHandler)将矩阵索引的值从0更改为1。最简单的方法似乎是使用Locator坐标p取取Rounded值坐标,并在该值处分配矩阵位置1。这似乎不起作用,因为M [[Sequence @@ Round [p]]] = 1中的形式“ Sequence @@ Round [p]”不起作用由于p本身是一个动态量(使用FullForm可以看到),因此得出正确的索引。

It would appear to be extremely useful to do something like this, but I can find no references to this, or any other method that takes a Locator coordinate output and uses it as an index to an array. 做这样的事情似乎非常有用,但是我找不到对此的引用,也没有找到采用Locator坐标输出并将其用作数组索引的任何其他方法的引用。

Any ideas greatly appreciated. 任何想法表示赞赏。

My experience is that locators are great for what they are, but attempts to generalize them don't work out very well. 我的经验是,定位器非常适合它们的用途,但是试图对其进行概括化效果并不理想。 Fortunately, what you want to do is still pretty straightforward. 幸运的是,您想要做的仍然很简单。

clickFunction[point,moreargs]:=Module[{}, see below ];

DynamicModule[{},

  ClickPane[
    Dynamic[Show[ what you want to click on ]],
    clickFunction[#,moreargs]
  ]
]

When the user clicks in the pane, clickFunction is called with the coordinates of the mouse pointer and possibly other arguments. 当用户在窗格中单击时,将使用鼠标指针的坐标以及可能的其他参数来调用clickFunction。 You can place Dynamic[] around individual arguments in the click function call (with addition of the symbols inside the first argument of DynamicModule, to make them Dynamic). 您可以在click函数调用中的各个参数周围放置Dynamic [](在DynamicModule的第一个参数内添加符号,使它们成为动态的)。 The clickFunction can also return values, using the form clickFunction还可以使用以下形式返回值:

 ({v1,v2,v3,..}=clickFunction[#,moreargs])&

The symbols in the list of return values can be dynamic, but should not have the Dynamic wrapper. 返回值列表中的符号可以是动态的,但不应具有动态包装器。

clickFunction needs to figure out what was clicked on. clickFunction需要找出被点击的内容。 For each area on your click pane (assuming it is rectangular),use its lower left and upper right corners to create x and y inequalities which return true or false when the point argument is in or out of the rectangle. 对于单击窗格上的每个区域(假设它是矩形的),使用其左下角和右上角创建x和y不等式,当point参数位于矩形内或矩形外时,它们返回true或false。 You can keep these in a list that you traverse with an appropriate function to detect what is clicked. 您可以使用适当的功能将它们保留在遍历的列表中,以检测单击的内容。

Don't shy away from Dynamic[]. 不要回避Dynamic []。 I tried to do things with Manipulate[] (and Locators) for far too long, thinking Dynamic[] was a big learning curve. 我尝试用Manipulate [](和定位器)做事太久了,以为Dynamic []是一个很大的学习曲线。 It's not. 不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM