English 中文(简体)
How do you calculate the reflex angle given two vectors in 3D space?
原标题:

I want to calculate the angle between two vectors a and b. Lets assume these are at the origin. This can be done with

theta = arccos(a . b / |a| * |b|)

However arccos gives you the angle in [0, pi], i.e. it will never give you an angle greater than 180 degrees, which is what I want. So how do you find out when the vectors have gone past the 180 degree mark? In 2D I would simply let the sign of the y-component on one of the vectors determine what quadrant the vector is in. But what is the easiest way to do it in 3D?

EDIT: I wanted to keep the question general but here we go. I m programming this in c and the code I use to get the angle is theta = acos(dot(a, b)/mag(a)*mag(b)) so how would you programmatically determine the orientation?

最佳回答

I came up with the following solution that takes advantage of the direction change of the cross product of the two vectors:

  1. Make a vector n = a X b and normalize it. This vector is normal to the plane spanned by a and b.

  2. Whenever a new angle is calculated compare it with the old normal. In the comparison, treat the old and the current normals as points and compute the distance between them. If this distance is 2 the normal (i.e. the cross product a X b has flipped).

You might want to have a threshold for the distance as the distance after a flip might be shorter than 2, depending on how the vectors a and b are oriented and how often you update the angle.

问题回答

This works in 2D because you have a plane defined in which you define the rotation.

If you want to do this in 3D, there is no such implicit 2D plane. You could transform your 3D coordinates to a 2D plane going through all three points, and do your calculation inside this plane.

But, there are of course two possible orientations for the plane, and that will affect which angles will be > 180 or smaller.

One solution that you could use:
What you effectively need to do is create a plane that one of the vectors is coplanar to.

Getting the cross product of both vectors will create a plane, then is you get the normal of this plane, you can get the angle between this and the vector you need to get the signed angle for, and you can use the angle to determine the sign.
If the angle is greater than 90 degrees, then it is below the created plane; less than 90 degrees, and it is above.
Depending on cost of calculations, the dot product can be used at this stage instead of the angle.

Just make sure that you always calculate the normals by the same order of vectors.

This is useable more easily if you re using the XYZ axes, and that s what you re comparing against, since you already have the vectors needed for the plane.

There are possbly more efficient solutions, but this is one I came up with.

Edit: clarification of created vectors a X b = p. This is perpendicular to both a and b. Then, do either: a X p or b X p to create another vector that is the normal to the plane created by the 2 vectors. Choice of vector depends on which you re trying to find the angle for.

Strictly speaking, two 3D vectors always have two angles between them - one below or equal to 180, the other over or equal to 180. Arccos gives you one of them, you can get the other by subtracting from 360. Think of it that way: imagine two lines intersect. You have 4 angles there - 2 of one value, 2 of another. What s the angle between the lines? No single answer. Same here. Without some kind of extra criteria, you can not, in theory, tell which of the two angle values should be taken into account.

EDIT: So what you really need is an arbitrary example of fixing an orientation. Here s one: we look from the positive Z direction. If the plane between the two vectors contains the Z axis, we look from the positive Y direction. If the plane is YZ, we look from the positive X direction. I ll think how to express this in coordinate form, then edit again.





相关问题
Bounding ellipse

I have been given an assignement for a graphics module, one part of which is to calculate the minimum bounding ellipse of a set of arbitrary shapes. The ellipse doesn t have to be axis aligned. This ...

Line segment in a triangle

How can we check if a line segment falls partially or fully inside a triangle? Cheers.

Line Segments from a point

I have a point p, and 2 line segments in a 2D plane. Point p is a location of view from where camera is looking towards the line segments. I want to check if line segment 1 is partially or fully ...

Creating a surface of triangles from a set of 2D points

I have a set of points and I need to convert the set to (non-overlapping) triangles (or a big polygon if equivalent)... The application: I have a list of locations (latitude,longitude) from a country,...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Delete holes in a Polygon

I have a polygon determined by an Array of Points. This polygon is crossing itself making some holes in the polygon itself. My questions is: How can I omit this holes and just get the exterior ...

Finding cycle of 3 nodes ( or triangles) in a graph

I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative ...

热门标签