Find angle between two vectors along a given direction (anti-clockwise or clockwise)

By Xiaodong Liang

Issue

Question: How to find the angle between two vectors along a particular direction, i.e clockwise or anti-clockwise using the Inventor API?

Solution

The angle between any two vectors (angle being defined as the union of the two vectors) as returned by the "Vector.AngleTo" method is always less than 180 deg irrespective of the directions of the vectors. The angle returned is the included angle between the vectors and hence, is always less than 180 deg.

This angle is the smaller of "theta" and "360-theta" (where "theta" is the angle that you would consider when calculating the dot product or the cross product between the vectors), this applies irrespective of how your coordinate system is defined.

If you want your angles to be returned in a particular "sense" (anti-clockwise or clockwise) then you could define this "sense" as a direction vector. With this direction defined, you could compare the cross product of the vectors (say Vector1 and Vector2) between which you want to measure the angle to this direction vector, if they are the same then the angle is Vector1.AngleTo(Vector2), but, if opposite in direction, then the angle is (2*pi) – Vector1.AngleTo(Vector2).

As an example, let us consider that all vectors lie on the XY plane. If you are looking at the XY plane in such a manner that the positive X direction points to the right and the positive Y direction points towards the top, then the Z direction would be pointing out of the plane towards you. Given this direction of viewing the XY plane, and if you want to measure the angles between all vectors that might be drawn on the XY planes in the anti-clockwise direction only, then the steps are:
1) Define the "sense" or direction vector corresponding to the anti-clockwise measurement; in this case it would be the Z vector.
2) In order to find the angle between any two vectors, e.g. Vector1 and Vector2, find the cross product of the two vectors, i.e. Vector1 x Vector2, if the direction of the cross product vector is the same as the direction vector (in this case the Z direction), then the angle between them in the anti-clockwise direction is Vector1.AngleTo(Vector2) but, if the direction of the cross-product is opposite that of the direction vector, then the angle between the vectors is 2*pi – Vector1.AngleTo(Vector2).

Public Sub VectorAngle()                ' assume we have had inventor application        Dim Tg As TransientGeometry        Tg = _InvApplication.TransientGeometry             'if you have many vectors on a plane and         'want to measure angles in the same sense:        'e.g anticlockwise             'use two vectors to set up the sense        Dim v1 As Vector        v1 = Tg.CreateVector(1, 0, 0)             Dim v2 As Vector        v2 = Tg.CreateVector(0, 1, 0)             Dim dir As Vector        dir = v1.CrossProduct(v2)        'use above or reverse it:        'dir.X = -(dir.X)        'dir.Y = -(dir.Y)        'dir.Z = -(dir.Z)             ' now use the sense to measure angles         ' between any other vectors on the plane        Dim v3 As Vector        v3 = Tg.CreateVector(-1, -1, 0)             Dim oCrossProd As Vector        oCrossProd = v1.CrossProduct(v3)             Dim angRad As Double             If oCrossProd.DotProduct(dir) < 0 Then            angRad = 2 * Math.PI - v1.AngleTo(v3)        Else            angRad = v1.AngleTo(v3)        End If             Dim angDeg As Double = angRad * 180 / Math.PI             MsgBox("Angle between vectors = " + angDeg.ToString())         End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading