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 question (Read 1185 times)
Atan2 question
Aug 19th, 2005, 8:42pm
 
Hello,

I have a question to 'atan2' in processing.
Regarding the code below, I want to assure myself that the way I write it is correct...
I am a bit irritated that I have to invert the value of atan2...

...since this chart says I dont, but propably its because I am calculating the xy position to a negative value, no?
...
...
Code:

void setup()
{
size(500,500);
}

void draw()
{
background(255);

float xmiddle = width/2;
float ymiddle = height/2;

float angle = -(atan2(mouseY - ymiddle, mouseX - xmiddle) - PI/2);

float d = dist(xmiddle, ymiddle, mouseX, mouseY);

float xp = sin(angle) * (d) + width/2;
float yp = cos(angle) * (d) + height/2;

beginShape(LINES);
vertex(width/2, height/2);
vertex(xp, yp);
endShape();
}
Re: Atan2 question
Reply #1 - Aug 20th, 2005, 12:15pm
 
I think :

float angle = atan2(xmiddle-mouseX,ymiddle-mouseY);
float xp   = sin(angle-PI)  * (d)    + width/2;
float yp   = cos(angle-PI)   * (d)    + height/2;

Code:


void setup()
{
size(500,500);
}

void draw()
{
background(255);

float xmiddle = width/2;
float ymiddle = height/2;

float angle = atan2(xmiddle-mouseX,ymiddle-mouseY);

float d = dist(xmiddle, ymiddle, mouseX, mouseY);

float xp = sin(angle-PI) * (d) + width/2;
float yp = cos(angle-PI) * (d) + height/2;

beginShape(LINES);
vertex(width/2, height/2);
vertex(xp, yp);
endShape();
}

Re: Atan2 question
Reply #2 - Aug 20th, 2005, 11:52pm
 
Opps!

Code:

Die Funktion atan2...
x = r*cos(atan2(y,x)) < COS
y = r*sin(atan2(y,x)) < SIN


Code:


void setup()
{
size(500,500);
}

void draw()
{
background(255);

float xmiddle = width/2;
float ymiddle = height/2;

float angle = atan2(mouseY - ymiddle, mouseX - xmiddle);

float d = dist(xmiddle, ymiddle, mouseX, mouseY);

float xp = cos(angle) * (d) + width/2; // < COS
float yp = sin(angle) * (d) + height/2; // < SIN

beginShape(LINES);
vertex(width/2, height/2);
vertex(xp, yp);
endShape();
}


Re: Atan2 question
Reply #3 - Aug 22nd, 2005, 6:25pm
 
OMG - Im all red for the shame. Thanks for pointing my nose in my mistake!
Page Index Toggle Pages: 1