Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _MY_VECTOR2D_HPP_
- #define _MY_VECTOR2D_HPP_
- // ----------------------------------------------------------------------------
- // Для russianiacup.ru 2019
- // ============================================================================
- #include <cmath>
- #include <string>
- // ============================================================================
- const double MyEps1 = 1e-3;
- // ============================================================================
- // Point2
- // ============================================================================
- class Vect2;
- // ----------------------------------------------------------------------------
- class Point2 : public Vec2Double
- {
- // --------------------------------------------------------------
- public:
- Point2() = delete;
- // ------------------------------------------------
- //Point2() : Vec2Double()
- //{
- // x = 0.0; y = 0.0;
- //}
- // ------------------------------------------------
- Point2(double x, double y) : Vec2Double(x, y)
- {
- }
- // ------------------------------------------------
- Point2(const Vec2Double& v)
- {
- x = v.x;
- y = v.y;
- }
- // ------------------------------------------------
- Point2(const Unit& u)
- {
- x = u.position.x;
- y = u.position.y;
- }
- // ------------------------------------------------
- Point2(const LootBox& b)
- {
- x = b.position.x;
- y = b.position.y;
- }
- // ------------------------------------------------
- Point2(const Bullet& b)
- {
- x = b.position.x;
- y = b.position.y;
- }
- // ------------------------------------------------
- Point2(const Mine& b)
- {
- x = b.position.x;
- y = b.position.y;
- }
- // ==========================================================
- operator Vec2Float() const
- {
- return Vec2Float((float)x, (float)y);
- }
- // ==========================================================
- bool operator==(const Point2& r)
- {
- return (abs(x - r.x) < MyEps1) && (abs(y - r.y) < MyEps1);
- }
- // ------------------------------------------------
- bool operator!=(const Point2& r)
- {
- return (abs(x - r.x) >= MyEps1) || (abs(y - r.y) >= MyEps1);
- }
- // ==========================================================
- Point2& operator-=(const Vect2& rv);
- // ----------------------------------------------------------
- Point2& operator+=(const Vect2& rv);
- // ==========================================================
- std::string ToString()
- {
- return std::to_string(x).erase(5) + ", " + std::to_string(y);
- }
- // ----------------------------------------------------------
- void Draw(const ColorFloat& color = Red) const;
- };
- // ------------------------------------------------------------------
- const Point2 NullPoint = Point2(1e6, 1e6);
- const Point2 ZeroPoint = Point2(0.0, 0.0);
- // ============================================================================
- // Vect2
- // ============================================================================
- class Vect2 : public Point2
- {
- // --------------------------------------------------------------
- public:
- Vect2() = delete;
- // ------------------------------------------------
- //Vect2() : Point2()
- //{
- //}
- // ------------------------------------------------
- explicit Vect2(const Point2 & p) : Point2(p)
- {
- }
- // ------------------------------------------------
- Vect2(const Vect2 & p) : Point2(p)
- {
- }
- // ------------------------------------------------
- Vect2(double x, double y) : Point2(x, y)
- {
- }
- // ------------------------------------------------
- // Неявное преобразование из точки. А специально сделано,
- // чтобы такого преобразования не было.
- // Можно было бы поставить explicit, но не нужно.
- //Vect2(const Vec2Double & v) : Point2(v.x, v.y)
- //{
- //}
- // ==========================================================
- Vect2 & operator+=(const Vect2 & r)
- {
- Point2::operator+=(r);
- return *this;
- }
- // ------------------------------------------------
- Vect2 operator+(const Vect2 & r) const
- {
- Vect2 res(*this);
- res.operator+=(r);
- return *this;
- }
- // ----------------------------------------------------------
- Vect2 & operator-=(const Vect2 & r)
- {
- Point2::operator-=(r);
- return *this;
- }
- // ------------------------------------------------
- Vect2 operator-(const Vect2 & r) const
- {
- Vect2 res(*this);
- res.operator-=(r);
- return *this;
- }
- // ----------------------------------------------------------
- Vect2 & operator *=(double k)
- {
- x *= k; y *= k;
- return *this;
- }
- // ------------------------------------------------
- Vect2 operator*(double k) const
- {
- Vect2 res(*this);
- return res *= k;
- }
- // ----------------------------------------------------------
- Vect2 & operator /=(double k)
- {
- const double eps = MyEps1 / 1000;
- if (abs(k) > eps)
- {
- x /= k; y /= k;
- }
- else
- {
- x /= eps; y /= eps;
- }
- return *this;
- }
- // ------------------------------------------------
- Vect2 operator/(double k) const
- {
- Vect2 res(*this);
- return res /= k;
- }
- // ==========================================================
- double Dot(const Vect2& rv)
- {
- double res = 0;
- return res;
- }
- // ------------------------------------------------
- double Cross(const Vect2& rv)
- {
- double res = 0;
- return res;
- }
- // ==========================================================
- double LenghtSqr() const
- {
- return x * x + y * y;
- }
- // ------------------------------------------------
- double Length() const
- {
- return sqrt(LenghtSqr());
- }
- // ------------------------------------------------
- Vect2& Normalize()
- {
- return operator/=(Length());
- }
- // ------------------------------------------------
- Vect2 GetNormalized()
- {
- Vect2 res(*this);
- return res.Normalize();
- }
- // ----------------------------------------------------------
- void Draw(const ColorFloat& color = Blue)
- {
- Point2::Draw(color);
- }
- };
- // ------------------------------------------------------------------
- const Vect2 NullVect = (Vect2)NullPoint;
- const Vect2 ZeroVect = Vect2(0.0, 0.0);
- // ------------------------------------------------------------------
- inline Point2& Point2::operator+=(const Vect2& rv)
- {
- x += rv.x;
- y += rv.y;
- return *this;
- }
- // ------------------------------------------------------------------
- inline Point2& Point2::operator-=(const Vect2& rv)
- {
- x -= rv.x;
- y -= rv.y;
- return *this;
- }
- // ============================================================================
- inline Vect2 operator-(const Point2& l, const Point2& r)
- {
- Vect2 res((Vect2)l);
- return res -= (Vect2)r;
- }
- // ------------------------------------------------------------------
- inline Point2 operator+(const Point2& l, const Vect2& r)
- {
- Point2 res(l);
- return res += r;
- }
- // ------------------------------------------------------------------
- inline Point2 operator-(const Point2& l, const Vect2& r)
- {
- Point2 res(l);
- return res -= r;
- }
- // ============================================================================
- // Line2
- // ============================================================================
- class Line2 : public Point2
- {
- // --------------------------------------------------------------
- public:
- Point2 P2 = Point2(0,0);
- // --------------------------------------------------------------
- public:
- Line2() = delete;
- // ------------------------------------------------
- Line2(const Point2& p1, const Point2& p2) : Point2(p1), P2(p2)
- {
- }
- // ------------------------------------------------
- void Draw(const ColorFloat& color = White) const;
- };
- // ----------------------------------------------------------------------------
- const Line2 NullLine(NullPoint, NullPoint);
- const Line2 ZeroLine(ZeroPoint, ZeroPoint);
- // ============================================================================
- // Rect2
- // ============================================================================
- // Позиция - середина нижней стороны.
- class Rect2 : public Point2
- {
- // --------------------------------------------------------------
- public:
- double Width = 0;
- double Height = 0;
- Point2 Center = NullPoint;
- Point2 Topleft = NullPoint;
- Point2 Bottomright = NullPoint;
- Line2 Left = NullLine;
- Line2 Top = NullLine;
- Line2 Right = NullLine;
- Line2 Bottom = NullLine;
- // --------------------------------------------------------------
- public:
- Rect2() = delete;
- // ------------------------------------------------
- void SetupPoints()
- {
- Center = *this + Vect2(0, Height / 2);
- Topleft = *this + Vect2(-Width / 2, Height);
- Bottomright = *this + Vect2(Width / 2, 0);
- }
- // ------------------------------------------------
- void SetupLines()
- {
- }
- // ------------------------------------------------
- Rect2(const Point2& p, const Point2& size) : Point2(p)
- {
- Width = size.x;
- Height = size.y;
- SetupPoints();
- SetupLines();
- }
- // ------------------------------------------------
- Rect2(const Unit& unit) : Point2(unit)
- {
- Width = unit.size.x;
- Height = unit.size.y;
- SetupPoints();
- SetupLines();
- }
- };
- // ----------------------------------------------------------------------------
- const Rect2 NullRect(NullPoint, NullPoint);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement