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.
Page Index Toggle Pages: 1
atan2 (Read 364 times)
atan2
Feb 26th, 2009, 10:07pm
 
Hi,
We know that atan2 function calculate the angle of the mouse in the range of {-PI,PI} but in my knob i need to get the angle from -5*PI/4 to PI/4 or relative angles.
Here is my code:

float radius = 100;
float angle = -5*PI/4;
float x = 200, y = 200;
void setup() {
 size(400,400);
 ellipseMode(CENTER);
 smooth();
}
void draw() {
background(100);
fill(255);
ellipse(x,y,radius,radius);
fill(0,0,255);
 arc(x,y,radius,radius,-5*PI/4,angle);
fill(0);
 arc(x,y,radius,radius,PI/4,3*PI/4);
fill(50);
 strokeWeight(5);
 ellipse(x,y,radius/2,radius/2);
}
void mouseDragged() {
if(mouseX >= x-radius/2 && mouseX <= x+radius/2 && mouseY >= y-radius/2 && mouseY <= y+radius/2) {
angle = atan2(mouseY-y,mouseX-x);
}
}
void mouseReleased() {
if(mouseX >= x-radius/2 && mouseX <= x+radius/2 && mouseY >= y-radius/2 && mouseY <= y+radius/2) {
angle = atan2(mouseY-y,mouseX-x);
}
}
Thanks for helping me,
Odin
Re: atan2
Reply #1 - Feb 26th, 2009, 11:35pm
 
add the following under your angle = atan2() calls (both of them)

Code:

if (angle > (PI * .75) && angle < PI) {
angle = angle - TWO_PI;
}


it basically remaps that last PI/4 bit that you couldn't access before from the values it gives to the values you want. (i must confess it was largely guesswork. and i converted it all to degrees and then back again. that'll get me thrown out of trig school...)
Re: atan2
Reply #2 - Feb 26th, 2009, 11:46pm
 
Thanks you very much! This trig was making me crazy Tongue

Oh, an other thing can anyone explain how to make the knob block once he arrive at PI/4 angle?
Re: atan2
Reply #3 - Feb 28th, 2009, 5:04pm
 
er. isn't PI / 4 your highest value? then isn't it simply

if (angle > PI / 4) {
 angle = PI / 4;
}

(do this after the other code from the last answer as those values are > PI / 4 before modification)

the thing i worry about is that with a single knob on screen (a nice knob, but only one) i was getting 100% processor usage on my laptop. i guess setting a framerate would help (at the cost of responsiveness)
Page Index Toggle Pages: 1