Changeset 856 for cpp/common


Ignore:
Timestamp:
03/28/19 22:13:25 (5 years ago)
Author:
Maciej Komosinski
Message:

Support for more operations in 2D

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/2d.h

    r848 r856  
    2727        template <typename Q> XY operator*=(Q q) { x *= q; y *= q; return *this; }
    2828        template <typename Q> XY operator/=(Q q) { x /= q; y /= q; return *this; }
    29         template <typename Q> XY operator/(Q q) { return XY(x / q, y / q); }
     29        template <typename Q> XY operator/(Q q) const { return XY(x / q, y / q); }
    3030        template <typename Q> XY operator*(Q q) const { return XY(q*x, q*y); }
    3131        void set(T _x, T _y) { x = _x; y = _y; }
     
    8888        template <typename Q> const XYRect& operator=(const Q& other) { p = other.p; size = other.size; return *this; }
    8989
     90        T right() const {return p.x+size.x;}
     91        T bottom() const {return p.y+size.y;}
     92        T top() const {return p.y;}
     93        T left() const {return p.x;}
     94        XY<T> center() const {return p+size/2;}
     95        const XY<T>& topLeft() const {return p;}
     96        XY<T> bottomRight() const {return p+size;}
     97        XY<T> topRight() const {return XY<T>(p.x+size.x,p.y);}
     98        XY<T> bottomLeft() const {return XY<T>(p.x,p.y+size.y);}
     99       
     100        T area() const { return size.x*size.y; }
     101
    90102        bool intersects(const XYRect& r) const
    91103        {
     
    106118        }
    107119
     120        bool contains(const XYRect& r) const
     121        {
     122                return contains(r.p) && contains(r.p+r.size);
     123        }
     124       
    108125        void add(const XY<T>& n)
    109126        {
     
    165182        }
    166183
     184        T distanceTo(const XYRect<T>& r) const
     185        {
     186        bool r_above = (r.bottom() <= top());
     187        bool r_below = (r.top() >= bottom());
     188        bool r_left = (r.right() <= left());
     189        bool r_right = (r.left() >= right());
     190
     191        if (r_above)
     192                {
     193                if (r_left) return r.bottomRight().distanceTo(topLeft());
     194                else if (r_right) return r.bottomLeft().distanceTo(topRight());
     195                else return top()-r.bottom();
     196                }
     197        else if (r_below)
     198                {
     199                if (r_left) return r.topRight().distanceTo(bottomLeft());
     200                else if (r_right) return r.topLeft().distanceTo(bottomRight());
     201                else return r.top()-bottom();
     202                }
     203        else if (r_left)
     204                {
     205                return left()-r.right();
     206                }
     207        else if (r_right)
     208                {
     209                return r.left()-right();
     210                }
     211        else
     212                return 0; //intersection
     213        }
     214
    167215        static const XYRect& zero() { static XYRect t(0, 0, 0, 0); return t; }
    168216        static const XYRect& one() { static XYRect t(0, 0, 1, 1); return t; }
Note: See TracChangeset for help on using the changeset viewer.