Advertisement
Ardt2

Vector 2d double template

Dec 4th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.60 KB | None | 0 0
  1. #ifndef _MY_VECTOR2D_HPP_
  2. #define _MY_VECTOR2D_HPP_
  3.  
  4. // ----------------------------------------------------------------------------
  5. // Для russianiacup.ru 2019
  6.  
  7. // ============================================================================
  8. #include <cmath>
  9. #include <string>
  10.  
  11.  
  12. // ============================================================================
  13. const double MyEps1 = 1e-3;
  14.  
  15.  
  16. // ============================================================================
  17. // Point2
  18. // ============================================================================
  19. class Vect2;
  20.  
  21. // ----------------------------------------------------------------------------
  22. class Point2 : public Vec2Double
  23. {
  24.     // --------------------------------------------------------------
  25.     public:
  26.         Point2() = delete;
  27.  
  28.         // ------------------------------------------------
  29.         //Point2() : Vec2Double()
  30.         //{
  31.         //  x = 0.0; y = 0.0;
  32.         //}
  33.  
  34.         // ------------------------------------------------
  35.         Point2(double x, double y) : Vec2Double(x, y)
  36.         {
  37.        
  38.         }
  39.  
  40.         // ------------------------------------------------
  41.         Point2(const Vec2Double& v)
  42.         {
  43.             x = v.x;
  44.             y = v.y;
  45.         }
  46.  
  47.         // ------------------------------------------------
  48.         Point2(const Unit& u)
  49.         {
  50.             x = u.position.x;
  51.             y = u.position.y;
  52.         }
  53.  
  54.         // ------------------------------------------------
  55.         Point2(const LootBox& b)
  56.         {
  57.             x = b.position.x;
  58.             y = b.position.y;
  59.         }
  60.  
  61.         // ------------------------------------------------
  62.         Point2(const Bullet& b)
  63.         {
  64.             x = b.position.x;
  65.             y = b.position.y;
  66.         }
  67.  
  68.         // ------------------------------------------------
  69.         Point2(const Mine& b)
  70.         {
  71.             x = b.position.x;
  72.             y = b.position.y;
  73.         }
  74.  
  75.  
  76.         // ==========================================================
  77.         operator Vec2Float() const
  78.         {
  79.             return Vec2Float((float)x, (float)y);
  80.         }
  81.  
  82.  
  83.         // ==========================================================
  84.         bool operator==(const Point2& r)
  85.         {
  86.             return (abs(x - r.x) < MyEps1) && (abs(y - r.y) < MyEps1);
  87.         }
  88.  
  89.         // ------------------------------------------------
  90.         bool operator!=(const Point2& r)
  91.         {
  92.             return (abs(x - r.x) >= MyEps1) || (abs(y - r.y) >= MyEps1);
  93.         }
  94.  
  95.  
  96.         // ==========================================================
  97.         Point2& operator-=(const Vect2& rv);
  98.  
  99.         // ----------------------------------------------------------
  100.         Point2& operator+=(const Vect2& rv);
  101.  
  102.  
  103.         // ==========================================================
  104.         std::string ToString()
  105.         {
  106.             return std::to_string(x).erase(5) + ", " + std::to_string(y);
  107.         }
  108.  
  109.         // ----------------------------------------------------------
  110.         void Draw(const ColorFloat& color = Red) const;
  111.  
  112.  
  113. };
  114.  
  115. // ------------------------------------------------------------------
  116. const Point2 NullPoint = Point2(1e6, 1e6);
  117. const Point2 ZeroPoint = Point2(0.0, 0.0);
  118.  
  119.  
  120. // ============================================================================
  121. // Vect2
  122. // ============================================================================
  123. class Vect2 : public Point2
  124. {
  125.     // --------------------------------------------------------------
  126.     public:
  127.         Vect2() = delete;
  128.  
  129.         // ------------------------------------------------
  130.         //Vect2() : Point2()
  131.         //{
  132.         //}
  133.  
  134.         // ------------------------------------------------
  135.         explicit Vect2(const Point2 & p) : Point2(p)
  136.         {
  137.         }
  138.  
  139.         // ------------------------------------------------
  140.         Vect2(const Vect2 & p) : Point2(p)
  141.         {
  142.         }
  143.  
  144.         // ------------------------------------------------
  145.         Vect2(double x, double y) : Point2(x, y)
  146.         {
  147.         }
  148.  
  149.         // ------------------------------------------------
  150.         // Неявное преобразование из точки. А специально сделано,
  151.         // чтобы такого преобразования не было.
  152.         // Можно было бы поставить explicit, но не нужно.
  153.         //Vect2(const Vec2Double & v) : Point2(v.x, v.y)
  154.         //{
  155.         //}
  156.  
  157.  
  158.         // ==========================================================
  159.         Vect2 & operator+=(const Vect2 & r)
  160.         {
  161.             Point2::operator+=(r);
  162.  
  163.             return *this;
  164.         }
  165.  
  166.         // ------------------------------------------------
  167.         Vect2 operator+(const Vect2 & r) const
  168.         {
  169.             Vect2 res(*this);
  170.             res.operator+=(r);
  171.  
  172.             return *this;
  173.         }
  174.  
  175.         // ----------------------------------------------------------
  176.         Vect2 & operator-=(const Vect2 & r)
  177.         {
  178.             Point2::operator-=(r);
  179.  
  180.             return *this;
  181.         }
  182.  
  183.         // ------------------------------------------------
  184.         Vect2 operator-(const Vect2 & r) const
  185.         {
  186.             Vect2 res(*this);
  187.             res.operator-=(r);
  188.  
  189.             return *this;
  190.         }
  191.  
  192.         // ----------------------------------------------------------
  193.         Vect2 & operator *=(double k)
  194.         {
  195.             x *= k; y *= k;
  196.  
  197.             return *this;
  198.         }
  199.  
  200.         // ------------------------------------------------
  201.         Vect2 operator*(double k) const
  202.         {
  203.             Vect2 res(*this);
  204.  
  205.             return res *= k;
  206.         }
  207.  
  208.         // ----------------------------------------------------------
  209.         Vect2 & operator /=(double k)
  210.         {
  211.             const double eps = MyEps1 / 1000;
  212.             if (abs(k) > eps)
  213.             {
  214.                 x /= k; y /= k;
  215.             }
  216.             else
  217.             {
  218.                 x /= eps; y /= eps;
  219.             }
  220.  
  221.             return *this;
  222.         }
  223.  
  224.         // ------------------------------------------------
  225.         Vect2 operator/(double k) const
  226.         {
  227.             Vect2 res(*this);
  228.  
  229.             return res /= k;
  230.         }
  231.  
  232.         // ==========================================================
  233.         double Dot(const Vect2& rv)
  234.         {
  235.             double res = 0;
  236.  
  237.             return res;
  238.         }
  239.  
  240.         // ------------------------------------------------
  241.         double Cross(const Vect2& rv)
  242.         {
  243.             double res = 0;
  244.  
  245.             return res;
  246.         }
  247.  
  248.         // ==========================================================
  249.         double LenghtSqr() const
  250.         {
  251.             return x * x + y * y;
  252.         }
  253.  
  254.         // ------------------------------------------------
  255.         double Length() const
  256.         {
  257.             return sqrt(LenghtSqr());
  258.         }
  259.  
  260.         // ------------------------------------------------
  261.         Vect2& Normalize()
  262.         {
  263.             return operator/=(Length());
  264.         }
  265.  
  266.         // ------------------------------------------------
  267.         Vect2 GetNormalized()
  268.         {
  269.             Vect2 res(*this);
  270.  
  271.             return res.Normalize();
  272.         }
  273.  
  274.         // ----------------------------------------------------------
  275.         void Draw(const ColorFloat& color = Blue)
  276.         {
  277.             Point2::Draw(color);
  278.         }
  279. };
  280.  
  281. // ------------------------------------------------------------------
  282. const Vect2 NullVect = (Vect2)NullPoint;
  283. const Vect2 ZeroVect = Vect2(0.0, 0.0);
  284.  
  285.  
  286. // ------------------------------------------------------------------
  287. inline Point2& Point2::operator+=(const Vect2& rv)
  288. {
  289.     x += rv.x;
  290.     y += rv.y;
  291.  
  292.     return *this;
  293. }
  294.  
  295. // ------------------------------------------------------------------
  296. inline Point2& Point2::operator-=(const Vect2& rv)
  297. {
  298.  
  299.     x -= rv.x;
  300.     y -= rv.y;
  301.  
  302.     return *this;
  303. }
  304.  
  305.  
  306. // ============================================================================
  307. inline Vect2 operator-(const Point2& l, const Point2& r)
  308. {
  309.     Vect2 res((Vect2)l);
  310.     return res -= (Vect2)r;
  311. }
  312.  
  313. // ------------------------------------------------------------------
  314. inline Point2 operator+(const Point2& l, const Vect2& r)
  315. {
  316.     Point2 res(l);
  317.     return res += r;
  318. }
  319.  
  320. // ------------------------------------------------------------------
  321. inline Point2 operator-(const Point2& l, const Vect2& r)
  322. {
  323.     Point2 res(l);
  324.     return res -= r;
  325. }
  326.  
  327.  
  328. // ============================================================================
  329. // Line2
  330. // ============================================================================
  331. class Line2 : public Point2
  332. {
  333.     // --------------------------------------------------------------
  334.     public:
  335.         Point2 P2 = Point2(0,0);
  336.  
  337.     // --------------------------------------------------------------
  338.     public:
  339.         Line2() = delete;
  340.  
  341.         // ------------------------------------------------
  342.         Line2(const Point2& p1, const Point2& p2) : Point2(p1), P2(p2)
  343.         {
  344.         }
  345.  
  346.  
  347.  
  348.         // ------------------------------------------------
  349.         void Draw(const ColorFloat& color = White) const;
  350.  
  351. };
  352.  
  353. // ----------------------------------------------------------------------------
  354. const Line2 NullLine(NullPoint, NullPoint);
  355. const Line2 ZeroLine(ZeroPoint, ZeroPoint);
  356.  
  357.  
  358. // ============================================================================
  359. // Rect2
  360. // ============================================================================
  361. // Позиция - середина нижней стороны.
  362. class Rect2 : public Point2
  363. {
  364.     // --------------------------------------------------------------
  365.     public:
  366.         double Width = 0;
  367.         double Height = 0;
  368.  
  369.         Point2 Center = NullPoint;
  370.         Point2 Topleft = NullPoint;
  371.         Point2 Bottomright = NullPoint;
  372.  
  373.         Line2 Left = NullLine;
  374.         Line2 Top = NullLine;
  375.         Line2 Right = NullLine;
  376.         Line2 Bottom = NullLine;
  377.  
  378.  
  379.     // --------------------------------------------------------------
  380.     public:
  381.         Rect2() = delete;
  382.  
  383.         // ------------------------------------------------
  384.         void SetupPoints()
  385.         {
  386.             Center = *this + Vect2(0, Height / 2);
  387.             Topleft = *this + Vect2(-Width / 2, Height);
  388.             Bottomright = *this + Vect2(Width / 2, 0);
  389.         }
  390.  
  391.         // ------------------------------------------------
  392.         void SetupLines()
  393.         {
  394.  
  395.         }
  396.  
  397.         // ------------------------------------------------
  398.         Rect2(const Point2& p, const Point2& size) : Point2(p)
  399.         {
  400.             Width = size.x;
  401.             Height = size.y;
  402.  
  403.             SetupPoints();
  404.             SetupLines();
  405.         }
  406.  
  407.         // ------------------------------------------------
  408.         Rect2(const Unit& unit) : Point2(unit)
  409.         {
  410.             Width = unit.size.x;
  411.             Height = unit.size.y;
  412.  
  413.             SetupPoints();
  414.             SetupLines();
  415.         }
  416.  
  417.  
  418. };
  419.  
  420. // ----------------------------------------------------------------------------
  421. const Rect2 NullRect(NullPoint, NullPoint);
  422.  
  423.  
  424.  
  425. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement