# Angle Between Two 3D Points

This topic is 3349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to find the angle between two 3D points, using the code below. However, the angle between 0,0,0 and 5,5,5 comes out to be 90deg while the angle between 0,0,0 and 5,0,0 comes out to be 45deg. Seems like the results are flipped around so I don't know if I'm doing this right or not. Are there problems with the function? float FindAngle(Data* pointA, Data* pointB) { double pi = 3.14159265358979323846; float angle = 0; float deltaY = pointB->y * pointA->y; float deltaX = pointB->x * pointA->x; float deltaZ = pointB->z * pointA->z; //Calculate the angle angle = (acos(deltaY+deltaX+deltaZ)*(180/pi)); return angle; }

##### Share on other sites
You need to divide by the vector lengths, unless you have normalized vectors.
float lenA = sqrt(pointA->x*pointA->x + pointA->y*....);float lenB = ..;delta = ..float value = (deltaX + ..) / (lenA * lenB);angle = acos(value)...;

##### Share on other sites
The thing is that I don't have vectors,I only have points.

##### Share on other sites
Same thing, use that method, it works.

##### Share on other sites
I did ask you said, but for the result of angle, I keep getting -1.#IND000 and I'm not sure what that means.

Heres the new function:

float FindAngle(Data* pointA, Data* pointB)
{

double pi = 3.14159265358979323846;

float lenA = sqrt(pointA->x*pointA->x + pointA->y*pointA->y + pointA->z*pointA->z);
float lenB = sqrt(pointB->x*pointB->x + pointB->y*pointB->y + pointB->z*pointB->z);

float deltaY = pointB->y * pointA->y;
float deltaX = pointB->x * pointA->x;
float deltaZ = pointB->z * pointA->z;

float value = ((deltaY + deltaX + deltaZ)/(lenA * lenB));
float angle = (acos(value)*(180/pi));

return angle;
}

##### Share on other sites
What angle exactly do you want to find?
When you have one point at 0,0,0 there is no angle to the other point.. there's just a line. You get that value (infinity) because of division by zero, since the length of (0,0,0) is 0. Perhaps you want to find the angle from the xz-plane to the line between the two points or something?

##### Share on other sites
Well in runtime its going to be variable, but for now I'm trying to find the angle between 0,0,0 and 5,5,5.

##### Share on other sites
There is no angle between 0,0,0 and 5,5,5. Those are two points, and there's a line between them. Draw a picture of the angle you want. =)

##### Share on other sites
Sorry, that was an incorrect explanation on my part.

Imagine a person facing the north. This function is meant to calculate how much they would have to turn to face east in degrees. I want to do the same basic thing in 3D.

##### Share on other sites
Then North would look something like (x,0,0), and East would look something like (0,0,-x), where x is however large a number you want (1 if you're dealing with unit vectors).

EDIT: I'm taking vectors as (x,y,z) where y is vertical - nfi if that's correct here, I don't do 3D. >_>

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633695
• Total Posts
3013374
×