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 › two floats input, one float output
Page Index Toggle Pages: 1
two floats input, one float output (Read 1042 times)
two floats input, one float output
Feb 12th, 2010, 9:44am
 
hi all.

there is some way to with the mouseX and mouseY, give me only one float?

I tryed with atan2, but the output give me stange numbers(f.ex: 0.001;0.41616, 1.6542)...i need to transform this numbers in real numbers depending on the size of the window.

any idea?

thanks
Re: two floats input, one float output
Reply #1 - Feb 12th, 2010, 10:14am
 
First of all, mouseX and mouseY are ints, not floats.

I suspect what you want is
int result = mouseX + ( width * mouseY );

If not, post your code for what you're trying to do.
Re: two floats input, one float output
Reply #2 - Feb 12th, 2010, 10:15am
 
what should that one float be ?
i mean mouseX and Y are coordinates. but what do you want ? the distance from 0,0 ? the agle ?
Re: two floats input, one float output
Reply #3 - Feb 12th, 2010, 11:14am
 
yeah, Cedric's right,
if your in two dimensions, you need two values, three dim... 3 values.
there is no relation between x value and y value... these are independant.
Thats the same for the arrays...
Re: two floats input, one float output
Reply #4 - Feb 12th, 2010, 12:20pm
 
Quote:
there is some way to with the mouseX and mouseY, give me only one float?


As the others have said, explain why you want one float and it might give us a clue on how to answer.  Roll Eyes

Anyway the following will calculate the angle (in degrees expressed as a float) of the line from the top left corner of the window to the current mouse position relative to the horizontal.

Code:
degrees(atan2(mouseY, mouseX)); 



Re: two floats input, one float output
Reply #5 - Feb 13th, 2010, 6:53am
 
Cedric wrote on Feb 12th, 2010, 10:15am:
what should that one float be
i mean mouseX and Y are coordinates. but what do you want the distance from 0,0 the agle


ya cedric!

i want the distance from 0,0(origin)! i think atan2 gives me the angle, but i want the distance
Re: two floats input, one float output
Reply #6 - Feb 13th, 2010, 7:21am
 
The distance of the mouse from the origin (i.e. top left corner is given by

Code:
sqrt(mouseX*mouseX + mouseY * mouseY) 



Smiley
Re: two floats input, one float output
Reply #7 - Feb 13th, 2010, 8:14am
 
Or
dist(0, 0, mouseX, mouseY);
Grin
Re: two floats input, one float output
Reply #8 - Feb 13th, 2010, 8:26am
 
TfGuy44 wrote on Feb 13th, 2010, 8:14am:
Or
dist(0, 0, mouseX, mouseY);
Grin


dist() is convenient but I can't imagine it's any quicker than Quark's suggestion (which is also a handy reminder that the calculation of distance is simply an application of pythagorus's theorum).  Also if you're using this for collision detection (i.e you don't need to report the actual distance, just act upon it) then using the longhand method suggests a commonly used optimisation for collision detection:

Code:
int distSquared = mouseX*mouseX + mouseY*mouseY;
int thresholdDistanceSquared = thresholdDistance*thresholdDistance;

if(distSquared < thresholdDistanceSquared) {
 // collision...
}


sqrt() is apparently slower in comparison to adding the extra multiplication (which itself isn't necessary if the value can be hard-coded; though avoid magic-numbers)...

Re: two floats input, one float output
Reply #9 - Feb 13th, 2010, 8:34am
 
Well yeah. I was just suggesting to use dist() because when I see:

"dist( 0, 0, mouseX, mouseY );"

I think:

Smiley Ah! This gets the distance between (0,0) and the mouse's position! Cool

But when I see:

"sqrt( mouseX * mouseX + mouseY * mouseY );"

I think:

Shocked Huh? What does that lin- Oh, it gets the distance between (0,0) and the mouse's position. Right. Why didn't I just write "dist( 0, 0, mouseX, mouseY );"? Huh
Re: two floats input, one float output
Reply #10 - Feb 15th, 2010, 8:55am
 
thanks all.

dist() resolv my problem.

always learning
Page Index Toggle Pages: 1