How to obtain points of a graphic, in a dynamic way

Hello everyone,

My doubt is the next: I would like to obtain points of a graphic placing the mouse in the graphic’s points. For example, if the graphic is y=2*x, sitting the mouse in the coordinate x=1, in the graphic, obtain in other variable y=2, to show it and use this result later.

Thank you very much for your time.

You could use Locator controls in a Manipulate to do this.

For example:

function = Sin;

Manipulate[
 coordinate = p;
 Show[
  Plot[function[x], {x, 0, 2 Pi}],
  Graphics[Line[{{p[[1]], -100}, {p[[1]], 100}}]]
  ]
 , {{p, {3 Pi/4, 0}}, Locator, Appearance -> None}]

Dynamic[function[coordinate[[1]]]]

Thank you for your response.

Could be possible that the locator, being visible, follows the function’s direction? And are there any possibility that all occurs placing the mouse only, without click?

This was the simplest solution that struck me. You might be able to make it work without clicking, but you’d be unable to copy the resulting number, for example. Investigate the options in the manipulate and locator documentation.

Dynamic[{coordinate[[1]], function[coordinate[[1]]]}]
to get x coordinate, too

Thank you David for your response.
In the code, there´s a part I don´t understand (for my ignorance). Why in these lines “Graphics[Line[{{p[[1]], -100}, {p[[1]], 100}}]]]” and “Dynamic[function[coordinate[[1]]]]” is the number one inside p[1] and coordinate[[1]]? For example, why not p[2] and coordinate [[2]], or simply p[ ] and coordinate[[ ]]?

The variable p is a length 2 array containing the x and y coordinates of the point you clicked on. The 1s here specify taking the first element, and thus the x coordinate.

If you remove the Appearance → None code, you can see the locator, providing an extra dimension of input that we are not making use of.

Okey, now I understand it.

Thank you very much David.

Thank you Okkes.