Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The angle between two 3D vectors with a result range 0 - 360

Tags:

c#

math

3d

I'm looking for a way to calculate the angle between three points considered as two vectors (see below):

using System.Windows.Media.Media3D;

public static float AngleBetweenThreePoints(Point3D[] points)
{
    var v1 = points[1] - points[0];
    var v2 = points[2] - points[1];

    var cross = Vector3D.CrossProduct(v1, v2);
    var dot = Vector3D.DotProduct(v1, v2);

    var angle = Math.PI - Math.Atan2(cross.Length, dot);
    return (float) angle;
}

If you give this the following points:

var points = new[]
{
    new Point3D(90, 100, 300),
    new Point3D(100, 200, 300),
    new Point3D(100, 300, 300)
};

or the following:

var points = new[]
{
    new Point3D(110, 100, 300),
    new Point3D(100, 200, 300),
    new Point3D(100, 300, 300)
};

You get the same result. I can see the cross product in the function returns (0, 0, 10000) in the first case, and (0, 0, -10000) in the second but this information gets lost with cross.Length which could never return a -ve result.

What I'm looking for is a result range 0 - 360 not limited to 0 - 180. How do I achieve that?

like image 264
imekon Avatar asked Oct 20 '25 19:10

imekon


2 Answers

The answer is to provide a reference UP vector:

public static float AngleBetweenThreePoints(Point3D[] points, Vector3D up)
{
    var v1 = points[1] - points[0];
    var v2 = points[2] - points[1];

    var cross = Vector3D.CrossProduct(v1, v2);
    var dot = Vector3D.DotProduct(v1, v2);

    var angle = Math.Atan2(cross.Length, dot);

    var test = Vector3D.DotProduct(up, cross);
    if (test < 0.0) angle = -angle;
    return (float) angle;
}

This came from here: https://stackoverflow.com/a/5190354/181622

like image 166
imekon Avatar answered Oct 23 '25 08:10

imekon


Are you looking for this ?

θ_radian = arccos ( (​P⋅Q) / ​(∣P∣∣Q∣)​ ​​) with vectors P and Q

θ_radian = θ_degree * π / 180

EDIT 0-360 range

angle = angle * 360 / (2*Math.PI);
if (angle < 0) angle = angle + 360;
like image 42
Ythio Csi Avatar answered Oct 23 '25 08:10

Ythio Csi