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 › Calculating distances
Page Index Toggle Pages: 1
Calculating distances (Read 2622 times)
Calculating distances
May 9th, 2010, 8:02am
 
I am trying to calculate the distance of the mouse traveled. This is what I have so far but the distance keeps on resetting. I would like the distance to be continuously added on top of the previous distance.

like a + b, b + c, c + d etc. for an infinite amount of times.

To achieve this do I need to use an array?

Any help will be appreciated

Thanks

--------------------------------------------------------------------

PFont myFont;

int w = screen.width;
int h = screen.height;


void setup() {
 frame.setResizable(true);
 size (w/2, h/2);
 background(255);
 smooth();
 myFont = createFont("AndaleMono", 12);
}

void draw () {
 float d = dist(pmouseX, pmouseY, mouseX, mouseY);
 fill(200);
 noStroke();
 rect(10, 10, 150, 50);
 fill(0);
 textFont(myFont);
 text("Distance = " + d, 10, 20);
 
}
Re: Calculating distances
Reply #1 - May 9th, 2010, 8:21am
 
Initial thought was to make 'd' global and simply add to it each frame - i.e.:

 d += dist(pmouseX, pmouseY, mouseX, mouseY);

(just in case you didn't know) This is just a shortcut for:

 d = d + dist(pmouseX, pmouseY, mouseX, mouseY);

It seems a problem remains however: since pmouseX/Y presumably are 0 at the start you get an arbitrary amount added to the total distance when the sketch first starts and mouse is moved over it...

Just figuring out a workaround...
Re: Calculating distances
Reply #2 - May 9th, 2010, 8:40am
 
The trick is to only add to d after the first frame has completed (i.e. frameCount >0) - which gets around the issue of pmouseX/Y being 0 on the first frame - and only when the mouse is over the sketch.  To achieve that you need an extra boolean variable and to add a couple of methods:

Code:
PFont myFont;

int w = screen.width;
int h = screen.height;

// make d global
float d;
boolean mouseIsOverSketch;

void setup() {
 size (w/2, h/2);
 
 frame.setResizable(true);
 background(255);
 smooth();
 myFont = createFont("AndaleMono", 12);
}

void draw () {
 if(frameCount > 0 && mouseIsOverSketch){
   // add to d when mouse if over the sketch
   // and 1st frame has completed:
   d += dist(pmouseX, pmouseY, mouseX, mouseY);
 }
 
 fill(200);
 noStroke();
 rect(10, 10, 150, 50);
 fill(0);
 textFont(myFont);
 text("Distance = " + d, 10, 20);
}

// Check if the mouse is over the sketch window...
void mouseEntered(MouseEvent mouseEvent) {
 mouseIsOverSketch = true;
}

void mouseExited(MouseEvent mouseEvent) {
 mouseIsOverSketch = false;
}
Re: Calculating distances
Reply #3 - May 9th, 2010, 8:40am
 
EDIT:  Looks like I got beaten to the punch. Wink

I haven't tested it, but I think this should do the trick.  (Just implementing blindfish's answer.)  You could also adapt this to do things like reset d to zero on a mouse click or whatever.
Code:
PFont myFont;

int w = screen.width;
int h = screen.height;
float d;

void setup() {
 frame.setResizable(true);
 size (w/2, h/2);
 background(255);
 smooth();
 myFont = createFont("AndaleMono", 12);
}

void draw () {
if (frameCount>0) {
   d = 0;
 } else {
   d += dist(pmouseX, pmouseY, mouseX, mouseY);
 }
 fill(200);
 noStroke();
 rect(10, 10, 150, 50);
 fill(0);
 textFont(myFont);
 text("Distance = " + d, 10, 20);

}
Re: Calculating distances
Reply #4 - May 9th, 2010, 9:35am
 
Thank you so much Blindfish and Smitty for your help, I have been trying to figure this out for a few days.

I have put your help to use and made this.

Do you know if there is a way to use the whole computer screen as a means to track the mouse and it record in the sketch. As I would like the program to run the background so I can collect the data of mouse movement.

Thanks again

-----------------------------------

PFont myFont;

int w = screen.width;
int h = screen.height;
float d;
boolean mouseIsOverSketch;


void setup() {
 size (w/2, h/2);
 frame.setResizable(true);
 background(255);
 smooth();
 myFont = createFont("AndaleMono", 12);
}

void draw () {
 if(frameCount > 0 && mouseIsOverSketch) {
   d += dist(pmouseX, pmouseY, mouseX, mouseY);
 }
 
 //Information box
 fill(130, 200, 252);
 noStroke();
 rect(10, 10, 175, 40);
 
 //Distance
 fill(0);
 textFont(myFont);
 text("Distance = " + d, 10, 20);
 
 //Screen size
 text("Screen size = " + screen.width + " x " + screen.height, 10, 40);
 
 
 stroke(0);
 line(pmouseX, pmouseY, mouseX, mouseY);
 
}

void mouseEntered(MouseEvent mouseEvent) {
 mouseIsOverSketch = true;
}

void mouseExited(MouseEvent mouseEvent) {
 mouseIsOverSketch = false;
}

Re: Calculating distances
Reply #5 - May 10th, 2010, 1:19am
 
That question gets asked often enough.  E.g. see this thread; though you may also want to read this...
Re: Calculating distances
Reply #6 - May 10th, 2010, 10:49am
 
Thanks for links.

I've managed to find a way to track the mouse in accordance to the size of my screen, but I have lost the ability to get a pmouseX pmouseY outside of the sketch window.

Code:

PFont myFont;
PGraphics pg;
Point mouse;

void setup() {
size(screen.width/2+10, screen.height/2+10);
pg = createGraphics(screen.width/2, screen.height/2, P3D);
myFont = createFont("AndaleMono", 12);
}

void draw() {
mouse = MouseInfo.getPointerInfo().getLocation();
pg.beginDraw();
pg.background(102);
pg.stroke(255);
pg.ellipse(mouse.x/2, mouse.y/2, 10, 10);
pg.endDraw();
image(pg, 5, 5);

//Info Container
fill(130, 200, 252);
noStroke();
rect(10, 10, 190, 60);

fill(0);
textFont(myFont);
text("x is " + mouse.x + " and y is " + mouse.y, 10, 20);

//Screen Size
text("Screen size = " + screen.width + " x " + screen.height, 10, 40);

println("mX is " + mouse.x + " and mY is " + mouse.y);
}
Re: Calculating distances
Reply #7 - May 10th, 2010, 11:33am
 
It's likely that since pmouseX/Y are internal to Processing (IIRC at least) they don't register outside the sketch window.  The solution is simple enough though: create two global variables and at the end of draw set them to the current mouse position using the appropriate code.  You can then use these variables instead of pmouseX/Y in your distance calculations...
Re: Calculating distances
Reply #8 - May 10th, 2010, 3:43pm
 
Thank you soo much for your help and guidance. I am very pleased that this community is very helpful.

It seems to be all working and functional although I am having the problem that I was having in the beginning and it is recording the current mouse position and adding it to the distance.

I am looking at mouseEvent 's http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseListener.html Looking at mouseEntered & Exited but I'm not sure how to define the component to be the screen.width screen.height.

Code:
PFont myFont;

Point mouse;
float d;
int pmX;
int pmY;

void setup() {
size(screen.width/2, screen.height/2);
myFont = createFont("AndaleMono", 12);
background(102);
smooth();
}

void draw() {
mouse = MouseInfo.getPointerInfo().getLocation();
d += dist(pmX*2, pmY*2, mouse.x*2, mouse.y*2);

//Info Container
fill(130, 200, 252);
noStroke();
rect(10, 10, 190, 60);

fill(0);
textFont(myFont);
text("Distance = " + d, 10, 20);

//Position of current x & y mouse
text("x is " + mouse.x + " and y is " + mouse.y, 10, 40);

//Screen Size
text("Screen size = " + screen.width + " x " + screen.height, 10, 60);

stroke(255);
line(pmX/2, pmY/2, mouse.x/2, mouse.y/2);

println("pmX is " + pmX + " and PmY is " + pmY + "mX is " + mouse.x + "mY is " + mouse.y);

pmX = mouse.x;
pmY = mouse.y;
}


Again thanks for the help I really appreciate it.
Re: Calculating distances
Reply #9 - May 11th, 2010, 1:23am
 
For some reason I thought it was necessary at the time, but in actual fact the thing that fixed that previously wasn't the mouseEntered/Exited stuff.  The really important part is making sure the previous x and y position for the mouse is populated before you start calculating distance; otherwise it will use the default value of 0 against the current position of the mouse and add to the distance...

So in the previous example pmouseX/Y was set when frameCount > 0
In this case your variables are populated when frameCount > 1

So if you add a condition and move your line drawing code into that...

Code:
PFont myFont;

Point mouse;
float d;
int pmX;
int pmY;

void setup() {
 size(screen.width/2, screen.height/2);
 myFont = createFont("AndaleMono", 12);
 background(102);
 smooth();
}

void draw() {
 mouse = MouseInfo.getPointerInfo().getLocation();
 if(frameCount > 1){
   d += dist(pmX*2, pmY*2, mouse.x*2, mouse.y*2);
   stroke(255);
   line(pmX/2, pmY/2, mouse.x/2, mouse.y/2);
 }

 
 //Info Container
 fill(130, 200, 252);
 noStroke();
 rect(10, 10, 190, 60);
 
 fill(0);
 textFont(myFont);
 text("Distance = " + d, 10, 20);
 
 //Position of current x & y mouse
 text("x is " + mouse.x + " and y is " + mouse.y, 10, 40);
 
 //Screen Size
 text("Screen size = " + screen.width + " x " + screen.height, 10, 60);
 
 println("pmX is " + pmX + " and PmY is " + pmY + "mX is " + mouse.x + "mY is " + mouse.y);
 
 pmX = mouse.x;
 pmY = mouse.y;
}
Re: Calculating distances
Reply #10 - May 12th, 2010, 5:30am
 
Thank you, didn't realise it would be some thing so simple.

I've changed the design a bit and added a square for every time the mouse is idle.

I've also added the ability to save the image when you are done with the program.

Is there a way to change the dpi from 72 to 300 in the save() function?
I have tried using the PDF export function but it records every frame, slowing down the program and making it impossible to practically open in acrobat or photoshop. I have also tried the PDF export function for recording one frame but it only records the first frame.

Code:
import processing.pdf.*;

PFont myFont;

Point mouse;
float d;
int pmX;
int pmY;
float l;
int h;
int m;
int s;
int time;

void setup() {
size(screen.width/2,screen.height/2 + 60);
myFont = createFont("AndaleMono", 10);
background(255);
smooth();
}

void draw() {
mouse = MouseInfo.getPointerInfo().getLocation();
if(frameCount > 1) {
d += dist(pmX*2, pmY*2, mouse.x*2, mouse.y*2);
stroke(0);
line(pmX/2, pmY/2, mouse.x/2, mouse.y/2);
}

if (mouse.x/2 == pmX/2 && mouse.y/2 == pmY/2) {
rectMode(CENTER);
noStroke();
fill(130, 200, 252);
rect(mouse.x/2, mouse.y/2, 5, 5);
}

//Info Container
fill(130, 200, 252);
rectMode(CORNER);
noStroke();
rect(0, screen.height/2, screen.width/2, 60);

fill(0);
textFont(myFont);
text("Distance (pixles) = " + d, 15, screen.height/2 + 20);

//Screen Size
text("Screen resolution (pixles) = " + screen.width + " x " + screen.height, 15, screen.height/2 + 40);

//Time running
text("Time running (milliseconds) = " + millis(), 300, screen.height/2 + 20);

//Instructions
text("Save image by pressing 's'", 300, screen.height/2 + 40);



println("pmX is " + pmX + " and PmY is " + pmY + "mX is " + mouse.x + "mY is " + mouse.y);

pmX = mouse.x;
pmY = mouse.y;
}

void keyPressed() {
if (key == 's') {
saveFrame("mouse.tif");
}
}
Re: Calculating distances
Reply #11 - May 12th, 2010, 6:12am
 
I don't see how you're going to be able to increase the resolution of what's in the display window: you're not dealing with vector objects after all...  The alternative is to draw your line to an offscreen buffer that's at the right pixel dimensions for the resolution you want - though since that's likely to be fairly large this may impact on performance.

Either way I think you'd start by looking at PGraphics.
Re: Calculating distances
Reply #12 - May 12th, 2010, 6:24am
 
samput1 wrote on May 12th, 2010, 5:30am:
Is there a way to change the dpi from 72 to 300 in the save() function

You are not the first one to ask... DPI is just a number stored along the image file, indicating how to map pixels in file to physical units.
You can use a good image viewer like IrfanView to change this value, it won't alter the image actually.
Re: Calculating distances
Reply #13 - May 29th, 2010, 5:09am
 
Hi,

I would like to get the value of distance from the app while its running and make it visible on a website.

ideally collecting the distance from many people using the app and collating it as one collective value on a website.

ie. total distance from everyone.

Question is where do I start looking, and is it possible?

Thanks for the help.

Page Index Toggle Pages: 1