Changeset 866 for cpp/common


Ignore:
Timestamp:
04/07/19 03:16:14 (5 years ago)
Author:
Maciej Komosinski
Message:

XY and XYRect for floating point values

File:
1 edited

Legend:

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

    r856 r866  
    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        
     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
    100100        T area() const { return size.x*size.y; }
    101101
     
    120120        bool contains(const XYRect& r) const
    121121        {
    122                 return contains(r.p) && contains(r.p+r.size);
    123         }
    124        
     122                return contains(r.p) && contains(r.p + r.size);
     123        }
     124
    125125        void add(const XY<T>& n)
    126126        {
     
    184184        T distanceTo(const XYRect<T>& r) const
    185185        {
    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
     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
    213213        }
    214214
     
    220220typedef XYRect<int> IntRect;
    221221
     222typedef XY<float> FloatXY;
     223typedef XYRect<float> FloatRect;
     224
    222225#endif
Note: See TracChangeset for help on using the changeset viewer.