fork download
  1. #include <cmath>
  2. #include <iostream>
  3. #include <utility>
  4.  
  5. std::pair<float, float> rotate(std::pair<float, float> origin, std::pair<float, float> start, unsigned int degrees)
  6. {
  7. std::pair<float, float> diff = std::make_pair(start.first - origin.first, start.second - origin.second);
  8. float currentAngle = ::atan2(diff.second, std::abs(diff.first));
  9. float newAngle = currentAngle + (degrees / 180.0 * 3.1415926539);
  10. float radius = std::sqrt(diff.first * diff.first + diff.second * diff.second);
  11. float cosAngle = ::cos(newAngle);
  12. float sinAngle = ::sin(newAngle);
  13. float x = origin.first + radius * cosAngle;
  14. float y = origin.second + radius * sinAngle;
  15. return std::make_pair(x, y);
  16. }
  17.  
  18. int main()
  19. {
  20. std::pair<float, float> origin = std::make_pair(0.0, 0.0);
  21. std::pair<float, float> start = std::make_pair(1.0, 0.0);
  22. const unsigned int degrees = 45;
  23. for (unsigned int i = 0; i < 360; i += degrees)
  24. {
  25. std::pair<float, float> newPos = rotate(origin, start, i);
  26. std::cout << "Rotated to " << i << " degrees: (" << newPos.first << ", " << newPos.second << ")" << std::endl;
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Rotated to 0 degrees: (1, 0)
Rotated to 45 degrees: (0.707107, 0.707107)
Rotated to 90 degrees: (-4.37114e-08, 1)
Rotated to 135 degrees: (-0.707107, 0.707107)
Rotated to 180 degrees: (-1, -8.74228e-08)
Rotated to 225 degrees: (-0.707107, -0.707107)
Rotated to 270 degrees: (1.19249e-08, -1)
Rotated to 315 degrees: (0.707107, -0.707107)