locked
angle between 3 points RRS feed

  • Question

  • Hi,
    I would like to know if there's any .NET tool\method that helps finding angle between 3 points?

    I have written a method which calculates the angle between 3 nodes but the problem is:
    it returns only 0-180 angle so i cant know the direction.

    I need to add something that will return (-180 - 180) or (0 - 360) degrees.

    here's the method:

    /// <summary>

    /// Calculate the angle (in degrees) between 3 points

    /// </summary>

    /// <param name="a">end point</param>

    /// <param name="b">center point</param>

    /// <param name="c">end point</param>

    /// <returns>angle (0 to 180)</returns>

    public static double GetAngle(IPoint a, IPoint b, IPoint c)

    {

    double result;

    // calculating the 3 distances

    double ab = GetDistance(a, b);

    double bc = GetDistance(b, c);

    double ac = GetDistance(a, c);

    double cosB = Math.Pow(ac, 2) - Math.Pow(ab, 2) - Math.Pow(bc, 2);

    cosB = cosB / (2 * ab * bc);

    result = 180 - (Math.Acos(cosB) * 180 / Math.PI);

    return result;

    }

    Tuesday, December 30, 2008 8:15 AM

Answers

  • If I understand what you are looking for I would do something like this....

    Evaluate the Y position of your 2 end points in relation to the Y position center point, if both end points are greater than or equal to use the equation you have.  If one is greater than center and one less than center find the angle from the greater than point to horizontal(center) and then the horizontal(center) to less than and add them together.  If both Y positions are below the center find the angle and subtract from 360.

    ...there may be an easier way using geometry but that was never my strong suit.

    -Jeff
    • Proposed as answer by JeffWask Tuesday, December 30, 2008 5:21 PM
    • Marked as answer by Guo Surfer Monday, January 5, 2009 5:48 AM
    Tuesday, December 30, 2008 5:21 PM

All replies

  • If I understand what you are looking for I would do something like this....

    Evaluate the Y position of your 2 end points in relation to the Y position center point, if both end points are greater than or equal to use the equation you have.  If one is greater than center and one less than center find the angle from the greater than point to horizontal(center) and then the horizontal(center) to less than and add them together.  If both Y positions are below the center find the angle and subtract from 360.

    ...there may be an easier way using geometry but that was never my strong suit.

    -Jeff
    • Proposed as answer by JeffWask Tuesday, December 30, 2008 5:21 PM
    • Marked as answer by Guo Surfer Monday, January 5, 2009 5:48 AM
    Tuesday, December 30, 2008 5:21 PM
  •  This is linear algebra. It is very simple. You have three points (x',y') (x'',y'') and (x''',y'''). Which angle do you want to know precisely? Three points define a triangle. There are three angles between them, not one. How can you talk about one angle? So, which angle do you want? I will write you the code or will explain how to do it. Before you compute the angles make sure that they are not positioned on one line. You will have to compute the determiant for that. If the latter is zero (degenerate case), there will be no angles.
    AlexB
    Tuesday, December 30, 2008 6:45 PM