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 › width/2 + mouseX = 0
Page Index Toggle Pages: 1
width/2 + mouseX = 0 (Read 537 times)
width/2 + mouseX = 0
Feb 11th, 2010, 7:13am
 
hi all.

i'm looking for a line of code representing the 0 in the middle of the window. So if i move the mouse to the left, the println of the mouseX is negative. So, for example, in a window with 400px of width the values range from -200 to 200.

i try to do with translate, but, no ways. any ideas?

thanks
Re: width/2 + mouseX = 0
Reply #1 - Feb 11th, 2010, 7:54am
 
You go it in the title
Code:
void setup() {
 size(400, 400);
}

void draw() {
 float xpos = mouseX - width * 0.5;
 float ypos = mouseY - height * 0.5;
 println(xpos + " | " + ypos);
 
 line(width * 0.5, 0, width * 0.5, height);
 line(0, height * 0.5, width, height * 0.5);
}


Hope that helps
rS
Re: width/2 + mouseX = 0
Reply #2 - Feb 11th, 2010, 8:35am
 
Awesome! It works like i want.

Thanks
Re: width/2 + mouseX = 0
Reply #3 - Feb 11th, 2010, 9:28am
 
Also have a look at map(), which will give you a more general way to control what the range of values is you want.

eg: float x = map(mouseX, 0, width - 1, MIN_X, MAX_X);

That is, a mouseX value of 0 is mapped to MIN_X, a value of (width - 1), which is the right-most pixel location, is mapped to MAX_X, and values in between are scaled smoothly between MIN_X and MAX_X. Incidentally, values outside of the range specified by the 2nd and 3rd parameters will also be mapped linearly, but will appear outside the range specified by the last two parameters.

-spxl
Page Index Toggle Pages: 1