Depends on what kind of graph you have and what you wanna do exactly.
Your idea is one way to go.
Another would be to use something like
ellipse(mouseX,graphY,5,5); to have an ellipse 'slide over' the graph and work from there. For example at that location create or increase the radius of an ellipse.
Another would be to use the
dist() function to compare the distance between a point on the graph and mouseX,mouseY. This would be useful if you already have a series of points which make the graph.
Here is a simple code example of both. The red dot is the first suggestion, the green dot the second suggestion.
Code:void setup() {
size(500,500);
smooth();
noStroke();
}
void draw() {
background(255);
translate(0,height/2);
// the graph made out of series of points
fill(0);
for (int i=0; i <width; i+=10) {
ellipse(i,sin(radians(i))*50,5,5);
}
// the red dot always follows the graph and mouseX
fill(200,0,0);
ellipse(mouseX,sin(radians(mouseX))*50,15,15);
// the green dot appears when the mouse is within distance
if(dist(mouseX,sin(radians(mouseX))*50+height/2,mouseX,mouseY)
< 20) {
fill(0,200,0);
ellipse(mouseX,(sin(radians(mouseX))*50),25,25);
}
}