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 & HelpSyntax Questions › Testing if A is within X pixels of B
Page Index Toggle Pages: 1
Testing if A is within X pixels of B (Read 214 times)
Testing if A is within X pixels of B
Jan 10th, 2009, 6:45pm
 
Hi all, I am trying to work out a way to test if the current mouse position is within X pixels of the previous position, i suspect it has something to do with radians, but am not entirely sure.. any help on code structure or rough ideas would be greatly appreciated!
Re: Testing if A is within X pixels of B
Reply #1 - Jan 10th, 2009, 7:09pm
 
would this help?

Code:


void setup(){
 size(800,800);
 smooth();
 frameRate(24);
}

void draw(){
 background(240);
 //noFill();
 float r = dist(mouseX, mouseY, pmouseX, pmouseY);
 ellipse(width/3,height/3,r,r);
 
 // just for fun
//(similar to http://processing.org/reference/dist_.html)
 for(int i = 0; i < 10; i++){
   for(int j = 0; j < 10; j++){
     float x = width/2+ i*20;
     float y = height/2+ j*20;
     r = dist(mouseX, mouseY, x, y);
   ellipse(x, y,r,r);
   }
 }
}


Re: Testing if A is within X pixels of B
Reply #2 - Jan 10th, 2009, 8:00pm
 
it's quite simple:

Code:
if(dist(mouseX,mouseY,pmouseX,pmouseY)<10)
{
//do something.
}


pmouseX/Y are the previous frame's mouse position. If you want the mouse position at a different point in time, save it in some variables, and replace the pmouseX/Y with the relevant variables.
Re: Testing if A is within X pixels of B
Reply #3 - Jan 10th, 2009, 10:02pm
 
nice one, that's me sorted, thanks folks
Page Index Toggle Pages: 1