Reputation: 5571
I have two points, a
and b
. I need to calculate the angle between them, so I treat them like vectors. However vector a
will always be defined as [0 0 0]. Reading over the MATLAB Newsreader, "Angle between two vectors", three solutions are provided:
x1 = 0;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 1;
z2 = 0;
a = [x1,y1,z1]; b= [x2,y2,z2];
theta = rad2deg(atan2(norm(cross(a,b)),dot(a,b)))
theta = rad2deg(acos(dot(a,b)))
theta = rad2deg(atan2(x1*y2-x2*y1,x1*x2+y1*y2))
However as acos
has accuracy problems as theta nears zero, yet out of the three equations, only acos
provides the correct solution.
Should I continue to use acos
or is there a better solution?
Upvotes: 1
Views: 12433
Reputation: 5571
The mistake is setting a = [0 0 0]
. The point of interest is centered at the origin, and to calculate the angle with respect to vector b
, you need to specify the direction the point is traveling. This can by done by setting a
is a unit vector.
If the point is traveling in the "x" direction, then x1=1
x1 = 1;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 1;
z2 = 0;
a = [x1,y1,z1]; b= [x2,y2,z2];
theta = rad2deg(atan2(norm(cross(a,b)),dot(a,b)))
theta = rad2deg(acos(dot(a,b)))
theta = rad2deg(atan2(x1*y2-x2*y1,x1*x2+y1*y2))
theta =
90
theta =
90
theta =
90
Problem Solved, forget to use the unit vector :P
Upvotes: -1
Reputation: 125854
A vector has magnitude and direction, while a
and b
are just coordinate points in space. When you treat a
and b
as vectors, you are implicitly defining [0 0 0]
as the origin point for the two vectors. However, since point a
is at [0 0 0]
, then it will be a vector with zero length.
If a vector has zero length, which direction does it point in? The answer is nowhere. It doesn't point in any direction, and thus you can't find the angle between it and another vector.
I think maybe you've defined your problem poorly. Does your coordinate system have an origin other than [0 0 0]
? Are you actually trying to find the angle between the line formed by a
and b
and the x-y plane?
Upvotes: 4