Showing us the code returning the wrong result would reveal us where the problem is and allow us to give you more specific advice.
Since you want the angle relative to the center, you must subtract the center coordinates from your points.
Math.Atan2
yields radians. Convert them to degrees with degrees = radians * 180 / pi
.
Your zero angle is not as usual on the x-axis, but on the y-axis. Add 90 degrees to make the correction.
Using a vector type makes things easier. Here I will be using the System.Numerics.Vector2
struct.
As Patrick McDonald pointed out, Atan2 might yield negative results in some cases. By adding 450 degrees (360 + our 90 degrees correction) to the result and taking this modulo 360 degrees, you always get a value between 0 and 360.
public static float GetAngle(Vector2 point, Vector2 center)
{
Vector2 relPoint = point - center;
return (ToDegrees(MathF.Atan2(relPoint.Y, relPoint.X)) + 450f) % 360f;
}
public static float ToDegrees(float radians) => radians * 180f / MathF.PI;
The test
var a = new Vector2(7, 3);
var b = new Vector2(20, 7);
var c = new Vector2(7, 10);
var d = new Vector2(3, 7);
var e = new Vector2(6.9f, 3); // Test for more than 270 deg.
var f = new Vector2(7.1f, 3); // Test for small angle.
var center = new Vector2(7, 7);
PrintAngle(a); // ==> 0
PrintAngle(b); // ==> 90
PrintAngle(c); // ==> 180
PrintAngle(d); // ==> 270
PrintAngle(e); // ==> 358.5679
PrintAngle(f); // ==> 1.432098
void PrintAngle(Vector2 point)
{
Console.WriteLine(GetAngle(point, center));
}