We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › mouse pressed on graph
Page Index Toggle Pages: 1
mouse pressed on graph (Read 603 times)
mouse pressed on graph
May 15th, 2010, 1:25pm
 
I would like that I can click on points on a graph. So that if the mouse is on the graph that I can mark a point and draw this fatter.

is there an easy way to do that? or do I have to make it like this:

if mouseX is in the area where the graph is and mouseY has the "right relation" to mouseX, then it's on the graph..

thank you for your ideas..
Re: mouse pressed on graph
Reply #1 - May 15th, 2010, 1:54pm
 
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);
 }

}
Page Index Toggle Pages: 1