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 › scaling atan2
Page Index Toggle Pages: 1
scaling atan2 (Read 1379 times)
scaling atan2
May 19th, 2005, 11:40am
 

Any suggestions?

How would I take ang in this case and say normalize it to a number between 1 & 12, or 1 & 19 given 360 degrees in a circle.

I'd like to return a positive and sequential number similar to a clock face

Code:

ang = atan2(mouseY, mouseX);


I know this is fairly rudimentary stuff, but then so am I!

Thanks
Re: scaling atan2
Reply #1 - May 19th, 2005, 11:53am
 
So ang will be an angle in radians from -PI to +PI, and you want to get a number between, say, 0 and 12, right?

First of all, you need to make ang be positive.  So add PI.  It will now be between 0 and TWOPI.

Next, you need to scale ang to be between 0.0 and 1.0.  So divide by TWOPI.

Finally you need to make ang be between 0.0 and 12.0, so multiply by 12.

Finally, if you're just dealing with the hour hand, you would want ang to be an whole number (throw away the fraction part) so you would either call floor or convert to an int.

Hope that's clear, and from that you can write some code Smiley
Re: scaling atan2
Reply #2 - May 19th, 2005, 12:01pm
 
That's the stuff!

Thanks, Tom.
Re: scaling atan2
Reply #3 - May 19th, 2005, 1:22pm
 
oops!

This ends up doing the job but there's a problem with the segmentation. What happens is when you get pt/TWO_PI = 1.0 is an unequal division. something like 000000,1111111,222222,3,000000,111111
                                                                |
                                                                | ... How can I even this out??

Code:

 float pt = ang+PI;
 pt=pt/TWO_PI;
 pt=pt*4;
 int pt1=floor(pt);

Re: scaling atan2
Reply #4 - May 19th, 2005, 3:08pm
 

Fixed it!

just needed a modulo function when truncating the float.

Thanks again, Tom.
Page Index Toggle Pages: 1