fork(1) download
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. struct Obj
  5. {
  6. double x, y, dir;
  7. Obj(double x, double y, double dir) :x{ x }, y{ y }, dir{ dir } {}
  8. double GetHeadingAngle(Obj const &other)
  9. {
  10. double heading = std::atan2(other.x - this->x, other.y - this->y) * 180 / 3.1415926535897 - this->dir;
  11. if (heading < -180) heading += 360;
  12. if (heading > 180) heading -= 360;
  13. return heading;
  14. }
  15. };
  16.  
  17.  
  18. int main()
  19. {
  20. Obj A(10, 10, 90);
  21.  
  22. Obj B(15, 5, 0);
  23. std::cout << A.GetHeadingAngle(B) << std::endl; // 45
  24.  
  25. Obj C(10, 15, 0);
  26. std::cout << A.GetHeadingAngle(C) << std::endl; // -90
  27.  
  28. Obj D(5, 5, 0);
  29. std::cout << A.GetHeadingAngle(D) << std::endl; // 135
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 4572KB
stdin
Standard input is empty
stdout
45
-90
135