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 › Angle between points
Page Index Toggle Pages: 1
Angle between points (Read 1607 times)
Angle between points
Nov 23rd, 2009, 1:18pm
 
Processing wizards,

I've hit a stumbling block.  My intuition tells me that it is probably a simple calculation, but I'm stuck.

I would like to calculate the angle or radians between two 2d points. I've tried using angleBetween with PVectors and using atan2, but neither seem to be working for me.

What I'd like to be able to do is, given 2 points (one known in advance and one random) be able to place a 3rd point directly behind the random point and be able to draw a straight line through this 3rd point and the the first point that also hits the random point.  Make sense?

Thanks in advance for anyone who might be able to help!

Furly
Re: Angle between points
Reply #1 - Nov 23rd, 2009, 2:18pm
 
atan2 should do the trick - did you make sure you passed the y coordinate first

Why not post what you tried
Re: Angle between points
Reply #2 - Nov 23rd, 2009, 3:06pm
 
here's the code i created to try out some various options


PVector origin;
PVector newPoint;
PVector angleP;
void setup(){
 size(800,800);
 origin = new PVector(width/2, height/2);
}

void draw(){
 background(0);
 noFill();
 stroke(255);
 ellipse(width/2,height/2,300,300);
 fill(255);
 ellipse(origin.x,origin.y,5,5);//center point to calculate angle from
 float deg = 220; //try to return this number
 fill(255,0,0);
 newPoint =  drawPoint(deg,150); //new 'random' point
 ellipse(newPoint.x,newPoint.y, 10,10);
 float angle = PVector.angleBetween(newPoint,origin);
 //pushMatrix();
 //translate(width/2, height/2);
 float a = atan2(origin.y - newPoint.y,origin.x - newPoint.x );
//popMatrix();
 println("intial degrees: " + deg + "as radians: " + radians(deg));
 println("angleBetween as radians: " + angle + " as degrees: " + degrees(angle));
 float aRad = radians(a);
 println( "atan2: " + a + " atan2 converted to radians: " + aRad);
 noLoop();
}

PVector drawPoint(float $deg, int $radius){
 float angle = radians($deg);
 float x = width/2 + (cos(angle) * $radius);
 float y = height/2 + (sin(angle) * $radius);
 PVector p;
 p =  new PVector(x,y);
 return p;
}
Re: Angle between points
Reply #3 - Nov 23rd, 2009, 3:13pm
 
Why do you 'convert' the value returned by atan2 to Radians?  From the Reference:

Quote:
atan2
...
Calculates the angle (in radians)


Wink
Page Index Toggle Pages: 1