fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using std::cout;
  5. using std::endl;
  6.  
  7. struct Vector2D
  8. {
  9. float x, y;
  10. Vector2D() : x(0), y(0) {}
  11. Vector2D(float x, float y) : x(x), y(y) {}
  12. Vector2D operator -(const Vector2D& v) const
  13. {
  14. return (Vector2D(x - v.x, y - v.y));
  15. }
  16. Vector2D operator -(void) const
  17. {
  18. return (Vector2D(-x, -y));
  19. }
  20. Vector2D operator /(float f) const
  21. {
  22. return (Vector2D(x / f, y / f));
  23. }
  24. };
  25.  
  26. // constants
  27. const float chunkSize = 512;
  28. const Vector2D minimapCenter(627, 135);
  29.  
  30. float distance(const Vector2D& a, const Vector2D& b)
  31. {
  32. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  33. }
  34.  
  35. void rotate(Vector2D& p, float theta)
  36. {
  37. p.x = cos(theta) * p.x - sin(theta) * p.y;
  38. p.y = sin(theta) * p.x + cos(theta) * p.y;
  39. }
  40.  
  41. float getTheta(const Vector2D& v)
  42. {
  43. return atan2(v.y,v.x) - atan2(-1 , 0); // b(0, -1)
  44. }
  45.  
  46. Vector2D getRelativePosition(const Vector2D& vert0, const Vector2D& vert1)
  47. {
  48. Vector2D p = vert0 - minimapCenter;
  49. rotate(p, getTheta(vert0 - vert1));
  50. return -p / (distance(vert0, vert1) / chunkSize);
  51. }
  52.  
  53. int main()
  54. {
  55. // input
  56. Vector2D vert0(309.88147, -144.74777); // top left
  57. Vector2D vert1(305.99057, 389.2381); // bottom left
  58.  
  59. Vector2D p = getRelativePosition(vert0, vert1);
  60. cout << "Relative position = (" << p.x << ", " << p.y << ")" << endl;
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
Relative position = (302.091, 270.417)