
Edward E. answered 01/28/22
Senior Software Engineer Specializing in .NET and C#
Given a line A-B, you should have coordinate pairs Ax,Ay and Bx,By. You should then be able to calculate the angle with respect to the X axis by taking the ArcTan of the slope calculated by By-Ay / Bx-Ax. The ArcTan gives the angle in radians which then need to be converted to degrees by multiplying by 180/pi. Some current C# code would be similar to the following.
---- Program.cs ----
using AngleOfLine;
var radians = AngleCalculator.CalculateAngleOfLine(0,0,3,3);
var degrees = AngleCalculator.RadiansToDegrees(radians);
Console.WriteLine($"Angle in radians:{radians}");
Console.WriteLine($"Angle in degrees:{degrees}");
---- AngleCalculator.cs ----
namespace AngleOfLine{
public static class AngleCalculator{
public static double CalculateAngleOfLine(int LineAx, int LineAy, int LineBx, int LineBy){
var AngleInRadians = Math.Atan(GetSlopeOfLine(LineAx, LineAx, LineBx, LineBy));
return AngleInRadians;
}
public static double GetSlopeOfLine(int LineAx, int LineAy, int LineBx, int LineBy){
return (LineBy - LineAy)/(LineBx-LineAx);
}
public static double RadiansToDegrees(double radians){
return (180 / Math.PI) * radians;
}
}
}