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 › Dead simple question
Page Index Toggle Pages: 1
Dead simple question (Read 362 times)
Dead simple question
Feb 26th, 2009, 10:56pm
 
This is driving me nuts.  I'm trying to get a value from 0.0 to 1.0, based on how far the mouse is from the left of the screen.

Code:
void setup() {
size(300, 300);
}

void draw() {
float relativeMouseX = mouseX / width;
println(relativeMouseX + " " + mouseX + " " + width);
}


Why does mouseX divided by width always give me 0.0?
Re: Dead simple question
Reply #1 - Feb 26th, 2009, 11:19pm
 
because either mouseX or width (or both) is an integer so it's returning an integer. try casting them (both?) to floats before dividing.
Re: Dead simple question
Reply #2 - Feb 26th, 2009, 11:44pm
 
you only need to cast the denominator...

float relativeMouseX = mouseX / (float)width;
Re: Dead simple question
Reply #3 - Feb 27th, 2009, 12:09am
 
Yes!  Thank you so much, koogs.  I knew it was something painfully simple.
Page Index Toggle Pages: 1