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 › Storing locations
Pages: 1 2 
Storing locations (Read 2258 times)
Storing locations
Jan 13th, 2007, 4:30pm
 
I have a really quick problem to solve and I would be grateful if anyone could help me out. I am trying to store the X and Y locations of the mouse pointer into an array but all my attempts have failed so far. Anyone have any ideas on how to go about this?
Re: Storing locations
Reply #1 - Jan 13th, 2007, 4:43pm
 
There's a couple of ways of doing it, one is to define a class that has an X and Y value and having an array of them, or use two arrays, one for X and one for Y.

e.g.
Code:
int[] mX;
int[] mY;

// when you want to add the latest mouse positions:
mX=append(mX,mouseX);
mY=append(mY,mouseY);

//to get the latest mouse positions from the arrays:
int currentX=mX[mX.length-1];
int currentY=mY[mY.length-1];
Re: Storing locations
Reply #2 - Jan 13th, 2007, 5:02pm
 
Thanks John... it seems to still be having trouble with

mX=append(mX, MouseX);
mY=append(mY, MouseY);

Something about nullPointerException?
Re: Storing locations
Reply #3 - Jan 13th, 2007, 5:26pm
 
I think you just need to instantiate your arrays.
2 ways to do it:

// using empty initialization list
int[] mX = {};
int[] mY = {};

or

instantiation statement, (using initial 0 length)
int[] mX = new int[0];
int[] mY = new int[0];


// when you want to add the latest mouse positions:
mX=append(mX,mouseX);
mY=append(mY,mouseY);

//to get the latest mouse positions from the arrays:
int currentX=mX[mX.length-1];
int currentY=mY[mY.length-1];
Re: Storing locations
Reply #4 - Jan 13th, 2007, 5:38pm
 
Yay it works - Thank you!
Re: Storing locations
Reply #5 - Jan 13th, 2007, 6:09pm
 
Would there be away to compare locations? Like comparing where X is in one frame and then in the next and then working out the distance?
Re: Storing locations
Reply #6 - Jan 13th, 2007, 7:56pm
 
Yep, check out the reference page for dist() http://processing.org/reference/dist_.html

Then you just need something like:
Code:

float d;

if(mX.length>1) // can't compare if there's only one value)
{
int maxVal=mX.length-1;
d=dist(mX[maxVal],mY[maxVal],mX[maxVal-1],mY[maxVal-1]);
}
Re: Storing locations
Reply #7 - Jan 17th, 2007, 1:35pm
 
Thanks for your help! One more quick question how do I get it so it stores about 4 values within the array?
Re: Storing locations
Reply #8 - Jan 17th, 2007, 2:12pm
 
If you only want the 4 most recent entries, I think the following should work:

Code:

//after adding the latest value
if(mx.length>4)
{
mX=subset(mX,1,4);
}
Re: Storing locations
Reply #9 - Jan 17th, 2007, 2:37pm
 
The code goes to run and then pops up with:

"ArrayIndexOutOfBoundsException: 4"
Re: Storing locations
Reply #10 - Jan 17th, 2007, 3:30pm
 
You definately need the if part, but I typoed the name, it's (assuming you copied my example before) mX.length, not mx.length
Re: Storing locations
Reply #11 - Jan 17th, 2007, 4:18pm
 
I corrected the name when placing it into my code but I think it is having problems (mX,1,4); as it keeps on saying that 4 four is outofbounds.
Re: Storing locations
Reply #12 - Jan 17th, 2007, 4:34pm
 
Thanks for the help again! The code was conflicting with some other stuff I was doing but it works great now thanks.
Re: Storing locations
Reply #13 - Jan 17th, 2007, 4:49pm
 
sorry one more question. How would I get all 4 numbers in the array to print out? Would I use the println function?
Re: Storing locations
Reply #14 - Jan 17th, 2007, 5:03pm
 
You can easily do:
Code:
for(int i=0;i<mX.length;i++)
{
println("mX["+i+"]="+mX[i]);
}


Of course you'll need to do the same for mY.
Pages: 1 2