4

Edit: This image shows what I am working on, I know the coordinates of each of the points, and they have a separate list of names for each one.

I'm working on a small function which can be used so that when the user hovers over certain coordinates in a LocatorPane, these coordinates can be checked against a separate list of coordinates, and there can be different graphical outputs on a separate graphic, depending on what coordinates the user has hovered over.

ttp = LocatorPane[Dynamic[userEditedProbeCoordinates], tmp, 
Appearance -> (probeDissFunc[WorstcaseDistressWTT16, probeList])]

coordinates = Dynamic[MousePosition["Graphics"]]

This get's me the coordinates from the user.

If[coordinates - 893, 368 < {\[PlusMinus]10, \[PlusMinus]10}, yay, boo]
If[coordinates == 893, 368, hooray, stop]
If[coordinates \[Subset] userEditedProbeCoordinates, woop, nooooo]

Here are 3 of my attempts to make something happen. None of them were successful.

The list of coordinates looks like this:

{{893, 368}, {863, 327}, {885, 512}, {800, 502}, {766, 360}, {710, 
374}}

Thanks in advance

2 Answers2

4

You can also do this without LocatorPane. Let's start by creating test data:

SeedRandom[25];
pts = RandomReal[200, {10, 2}];
labels = RandomInteger[1000, 10];
map = Deploy@Graphics[{
     Disk[#, 10] & /@ pts,
     PointSize[Large], Red, Point[pts]
     }, PlotRange -> {{0, 200}, {0, 200}}];

Mathematica graphics

It is important to specify the plot range explicitly, as we will use this in the next step. I use Deploy so the graphics can't be selected, which is appropriate for a display. The black disk represents the area of radius ten around the point, represented by a red dot.

nf = Part[labels, Nearest[pts -> Automatic, #, {1, 10}]] &;
EventHandler[map, {
  "MouseMoved" :> (hovering = nf[200 MousePosition["EventHandlerScaled"]])
  }]

In the previous step we initialized a list labels. Element 1 is the label for disk 1, element 2 for disk 2 and so on. Here we define a function nf that, given a coordinate, returns the label of the closest point within a radius of 10. Finally we use an event handler to update a variable hovering. MousePosition["EventHandlerScaled"] gives a position {x,y} where x and y are in the range (0,1) inside the graphic. Multiplying this by the plot range we get the actual coordinate inside the graphic.

Demo

As the animation shows the label of the currently hovered point can now be retrieved by Dynamic[hovering]. This value can be displayed anywhere it's needed, in the graphics or elsewhere.

C. E.
  • 70,533
  • 6
  • 140
  • 264
2

As an example

img = Graphics[{Blue, Disk[{0, 0}, 1], Red, Disk[{0, 0}, 0.5]}, ImageSize -> 300];

tag[p_] := Piecewise[{{"Red", Norm[p] < 0.5}, {"Blue", 0.5 < Norm[p] < 1},
           {"White", Norm[p] > 1}, {"Boundary", Norm[p] == 0.5 || Norm[p] == 1}}]

csign[{x_, y_}] := StringForm["[``,``]", Sign[x], Sign[y]]

Manipulate[Show[img,PlotLabel -> StringForm["`` ``", tag[p], csign[p]]],
           {{p, {0, 0}}, Locator}]

enter image description here

With multiple locators

n = 6;
col = {Black, Red, Blue, Green, Orange, Yellow};
txt = {"Black", "Red", "Blue", "Green", "Orange", "Yellow", "Empty"};
rad = {0, 0.2, 0.3, 0.5, 0.7, 0.8, 10};
img = Graphics[Table[{col[[i]], Disk[{0, 0}, rad[[i]]]}, {i, n, 1, -1}]];

tag[p_] := Piecewise[ Table[{txt[[i+1]], rad[[i]]<Norm[p]<rad[[i+1]]}, {i, n}]]

coord = RandomReal[{-1, 1}, {n, 2}];

Manipulate[Show[img, ImagePadding -> 20,  Epilog -> {
 Text[tag[p1], p1 + {0, 0.1}], Text[tag[p2], p2 + {0, 0.1}],
 Text[tag[p3], p3 + {0, 0.1}], Text[tag[p4], p4 + {0, 0.1}]}],
 {{p1, coord[[1]]}, Locator}, {{p2, coord[[2]]}, Locator},
 {{p3, coord[[3]]}, Locator}, {{p4, coord[[4]]}, Locator}]

enter image description here

Sumit
  • 15,912
  • 2
  • 31
  • 73
  • Sumit, Thanks for the example, do you know how I can convert this to coordinate based Plotlabels? So if the cursor is in within + or - 10 of a certain coordinate, some separate information can appear on a separate graphic next to it? – UndercoverPie Jun 14 '16 at 12:48
  • 1
    you can define a new function for that. I modified my answer with an example. – Sumit Jun 14 '16 at 13:08
  • Thanks so much, but I am using a LocatorPane rather than a graphic, how can I incorporate this in to my already displayed LocatorPane? Sorry I am really new to mathematica, and need some simple way of checking the cursor's location against a list of coordinates, to see if it is within + or - 10 of it radially. I have added in a picture of my LocatorPane to my post. – UndercoverPie Jun 14 '16 at 13:31
  • 1
    I modified my answer for multiple locator. Hope that will help. You can choose the initial coordinates for the locator from your list of coordinates. – Sumit Jun 15 '16 at 07:24
  • Thanks that's really useful. Can you help me with one last step though? My code is now tag[p_]:=Piecewise[{{Part[probeList,1],Norm[p -{5,5}-Part[userEditedProbeCoordinates,1]] < 10}}] Manipulate[Show[rttp,PlotLabel->tag[p] ],{{p,{0,0}},Locator}] , but I need to repeat part of it for values from 1 to the Length of one of the lists. So in normal code speak this would be tag[p_]:=Piecewise[{{Part[probeList,n],Norm[p -{5,5}-Part[userEditedProbeCoordinates,n]] < 10}, for n from 1 to Length[probeList]}], but I don't know how to do this in Mathematica. Is this possible? – UndercoverPie Jun 15 '16 at 08:45
  • have a look at the modified answer. – Sumit Jun 15 '16 at 09:39
  • This is great, thanks Sumit. Is there a way to extract the PlotLabel (or Epilog) As a separate variable which I can use? So instead of Green or Orange just being the PlotLabel etc, I can use an If function later (i.e. If[(new variable from PlotLabel) = "Orange", "This is Orange"] – UndercoverPie Jun 15 '16 at 10:46
  • yes. Just define a new variable. say Manipulate[s = p1; Show[img,... – Sumit Jun 15 '16 at 11:20