#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# find angle between 2 points

edited April 2015

Hi, as in the image: I have 2 points. the red one in the center is fixed. the green one is rotating around the red one. How can I determine the angle 'a' (blue) between them in the range 0° - 360° ?

http://www2.zippyshare.com/v/WLwOIo3o/file.html

thank you!

• ??? If you're drawing it at a certain angle, surely you must know the angle!

``````float a;

void setup(){
size(220,220);
smooth();
ellipseMode(CENTER);
}

void draw(){
background(0);
a = map(millis()%10000,0,10000,0,TWO_PI);
translate(110,110);
stroke(0,255,255);
line(-110,0,110,0);
line(0,-110,0,110);
noFill();
arc(0,0,100,100,-a,0);
fill(255,0,0);
noStroke();
ellipse(0,0,10,10);
fill(0,255,0);
ellipse(50*cos(a),50*-sin(a),10,10);
fill(255);
text("" + int(degrees(a)), 90*cos(a), 90*-sin(a));
}
``````
• Of course, if you don't know where the point is...

``````float a;
PVector x = new PVector(1,0,0);

void setup(){
size(220,220);
smooth();
ellipseMode(CENTER);
}

void draw(){
background(0);
a = map(millis()%10000,0,10000,0,TWO_PI);
translate(110,110);
stroke(0,255,255);
line(-110,0,110,0);
line(0,-110,0,110);
noFill();
arc(0,0,100,100,-a,0);
fill(255,0,0);
noStroke();
ellipse(0,0,10,10);
fill(0,255,0);
ellipse(50*cos(a),50*-sin(a),10,10);
fill(255);
text("" + int(degrees(a)), 90*cos(a), 90*-sin(a));
PVector p = new PVector( mouseX-110, mouseY-110, 0);
float b = PVector.angleBetween(p, x);
fill(0,0,255);
ellipse(mouseX-110,mouseY-110,10,10);
fill(255);
float t = degrees(b);
if( mouseY < 110 ) t = 360 - t;
text("" + int(360-t), 15, -15);
}
``````

atan2() will get you the two angles (angle from horizon in radians, origin at the red ball). then take one away from the other to get the angle between the two.

see wikipedia.

• thank you, I checked atan2 but didn't really understood it..subtracting each other works!

• I was needing an angle between function and looked up this old discussion.

I had a very hard time understanding it but this is what I came up with, in case anyone needs it.

I hope my code is ok.

The crucial point was

• to take the delta vector `d` from 2 vectors `a` and mouse position (those I wanted the angle between) and do atan2 on d to get an angle and
• to convert this angle from rad to degree (I had to weak TfGuy44s way of doing it).

Best, Chrisir ;-)

``````// https : // forum.processing.org/two/discussion/10474/find-angle-between-2-points

PVector a; // the fixed red point

void setup() {
size(1200, 600);
a = new PVector(190, height-70); // was before :  width/2, height/2
}

void draw() {
background(0);

// draw a simple cross at a
crossAtPV(a);
ellipsePV(a); // show a in red

// get angle
float angle=angleBetweenPV_PV(a, new PVector(mouseX, mouseY));

// show the found angle
displayInDegree(angle);
triangleMy(angle);
}

// ----------------------------------------------------------------

float angleBetweenPV_PV(PVector a, PVector mousePV) {

// calc angle : the core of the sketch

PVector d = new PVector();

// calc angle
pushMatrix();
translate(a.x, a.y);
// delta
d.x = mousePV.x - a.x;
d.y = mousePV.y - a.y;
// angle
float angle1 = atan2(d.y, d.x);
popMatrix();

return angle1;
}

void displayInDegree(float ang) {

// expects rad, shows in degrees

fill(255);
float t=degrees(ang);

if (mouseY<a.y) {
t=360-t;
t=360-t;
t=abs(t);
} else {
t=360-t;
}

text(t, 21, height-21);
}

void triangleMy(float ang) {

pushMatrix();

translate(a.x, a.y);

rotate(ang);

fill(255); // white shield
triangle(60, 0,
80, -30,
80, 30);

popMatrix();
}

void ellipsePV(PVector pv) {
fill(255, 0, 0); // red
ellipse(pv.x, pv.y, 10, 10);
}

void crossAtPV(PVector pv) {
stroke(255);
line(pv.x, 0, pv.x, height);
line(0, pv.y, width, pv.y);
}
//
``````