Yaniv Madav

Yaniv Madav

  • NA
  • 1
  • 0

determine angle and direction between 3 points

Dec 30 2008 2:51 AM

Hi,

I have written a method which calculates the angle between 3 nodes.

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.

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;

}