Найти угол наклона по двум точкам - C#
Формулировка задачи:
Здравствуйте )
Столкнулся с проблемой: имеются две точки с координатами (x1,y1) (x2,y2), нужно найти направление от т.1 к т.2. Направление - с шагом 45 градусов присваивается значение string W,NW,N,NE,E,SE,S,SW. Поможете? )
Этот код чего-то не работает. Target и Position - Vector2, координаты - стандартные экранные, левый верхний угол - 0,0.
Имеет ли значения ситуации, когда Target и Position в разных четвертях друг относительно друга?
public void DirectionCalculation()
{
double degrees;
double radians;
string CurrentDirection;
radians = Math.Atan((Target.Y - Position.Y)/(Target.X - Position.X));
degrees = radians * (180 / Math.PI);
if (degrees >= 0 && degrees < 45)
{ CurrentDirection="W";}
else if (degrees >= 45 && degrees < 90)
{ CurrentDirection="NW";}
else if (degrees >= 90 && degrees < 135)
{ CurrentDirection="N";}
else if (degrees >= 135 && degrees < 180)
{ CurrentDirection="NE";}
else if (degrees >= 180 && degrees < 225)
{ CurrentDirection="E";}
else if (degrees >= 225 && degrees < 270)
{ CurrentDirection="SE";}
else if (degrees >= 270 && degrees < 315)
{ CurrentDirection="S";}
else if (degrees >= 315 && degrees < 360)
{ CurrentDirection = "SW"; }
}Решение задачи: «Найти угол наклона по двум точкам»
textual
Листинг программы
degrees = ((Math.Atan2 (Target.Y - Position.Y,Target.X - Position.X) +2*Math.PI)*180/ Math.PI)%360;