Changeset 909


Ignore:
Timestamp:
02/04/20 19:36:58 (4 years ago)
Author:
Maciej Komosinski
Message:

Introduced XY::extensionContaining()

File:
1 edited

Legend:

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

    r905 r909  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1616        XY() {}
    1717        XY(T _x, T _y) :x(_x), y(_y) {}
    18         template <typename Q> XY(const Q& other) : x(other.x), y(other.y) {}
    19         template <typename Q> const XY& operator=(const Q& other) { x = other.x; y = other.y; return *this; }
    20         template <typename Q> const XY operator()(const Q& other) { return XY(other.x, other.y); }
    21         XY operator+(const XY&p) const { return XY(x + p.x, y + p.y); }
    22         XY operator-(const XY&p) const { return XY(x - p.x, y - p.y); }
    23         XY operator+=(const XY&p) { x += p.x; y += p.y; return *this; }
    24         XY operator-=(const XY&p) { x -= p.x; y -= p.y; return *this; }
     18        template <typename Q> XY(const Q &other) : x(other.x), y(other.y) {}
     19        template <typename Q> const XY &operator=(const Q &other) { x = other.x; y = other.y; return *this; }
     20        template <typename Q> const XY operator()(const Q &other) { return XY(other.x, other.y); }
     21        XY operator+(const XY &p) const { return XY(x + p.x, y + p.y); }
     22        XY operator-(const XY &p) const { return XY(x - p.x, y - p.y); }
     23        XY operator+=(const XY &p) { x += p.x; y += p.y; return *this; }
     24        XY operator-=(const XY &p) { x -= p.x; y -= p.y; return *this; }
    2525        XY operator-() const { return XY(-x, -y); }
    2626        // allows float operations on ints
     
    2828        template <typename Q> XY operator/=(Q q) { x /= q; y /= q; return *this; }
    2929        template <typename Q> XY operator/(Q q) const { return XY(x / q, y / q); }
    30         template <typename Q> XY operator*(Q q) const { return XY(q*x, q*y); }
    31         XY operator*=(const XY& q) { x *= q.x; y *= q.y; return *this; }
    32         XY operator/=(const XY& q) { x /= q.x; y /= q.y; return *this; }
    33         XY operator*(const XY& q) const { return XY(x*q.x, y*q.y); }
    34         XY operator/(const XY& q) const { return XY(x/q.x, y/q.y); }
     30        template <typename Q> XY operator*(Q q) const { return XY(q * x, q * y); }
     31        XY operator*=(const XY &q) { x *= q.x; y *= q.y; return *this; }
     32        XY operator/=(const XY &q) { x /= q.x; y /= q.y; return *this; }
     33        XY operator*(const XY &q) const { return XY(x * q.x, y * q.y); }
     34        XY operator/(const XY &q) const { return XY(x / q.x, y / q.y); }
    3535        void set(T _x, T _y) { x = _x; y = _y; }
    3636        void add(T _x, T _y) { x += _x; y += _y; }
    3737        void sub(T _x, T _y) { x -= _x; y -= _y; }
    38         bool operator==(const XY& p) const { return (fabs(double(x - p.x)) < 1e-20) && (fabs(double(y - p.y)) < 1e-20); }
    39         bool operator!=(const XY& p) const { return !operator==(p); }
    40         T distanceTo(const XY& p) const { return sqrt(double((p.x - x)*(p.x - x) + (p.y - y)*(p.y - y))); }
    41         T magnitude() const { return sqrt(x*x + y * y); }
    42         T length() const { return sqrt(x*x + y * y); }
     38        bool operator==(const XY &p) const { return (fabs(double(x - p.x)) < 1e-20) && (fabs(double(y - p.y)) < 1e-20); }
     39        bool operator!=(const XY &p) const { return !operator==(p); }
     40        T distanceTo(const XY &p) const { return sqrt(double((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y))); }
     41        T magnitude() const { return sqrt(x * x + y * y); }
     42        T length() const { return sqrt(x * x + y * y); }
    4343        T lengthSq() const { return x * x + y * y; }
    44         T dotProduct(const XY& v) const { return x * v.x + y * v.y; }
    45         T crossProduct(const XY& v) const { return x * v.y - y * v.x; }
     44        T dotProduct(const XY &v) const { return x * v.x + y * v.y; }
     45        T crossProduct(const XY &v) const { return x * v.y - y * v.x; }
    4646        void normalize() { operator/=(length()); } // length becomes 1
    47         static XY average(const XY& v1, const XY& v2) { return XY((v1.x + v2.x)*0.5, (v1.y + v2.y)*0.5); }
     47        static XY average(const XY &v1, const XY &v2) { return XY((v1.x + v2.x) * 0.5, (v1.y + v2.y) * 0.5); }
    4848        double getDirection() const { return atan2(y, x); }
    49         static XY interpolate(const XY& v1, const XY& v2, double t) { return universal_lerp(v1,v2,t); }
     49        static XY interpolate(const XY &v1, const XY &v2, double t) { return universal_lerp(v1, v2, t); }
    5050        XY toInt() const { return XY(int(x), int(y)); }
    5151        XY transpose() const { return XY(y, x); }
    52         static const XY& zero() { static XY t(0, 0); return t; }
    53         static const XY& one() { static XY t(1, 1); return t; }
     52        static const XY &zero() { static XY t(0, 0); return t; }
     53        static const XY &one() { static XY t(1, 1); return t; }
    5454};
    5555
    5656//specialized: int equality not using fabs()
    57 template<> inline bool XY<int>::operator==(const XY<int>& p) const { return (x == p.x) && (y == p.y); }
    58 
    59 template <typename T> XY<T> xymin(const XY<T>& a, const XY<T>& b) { return XY<T>(min(a.x, b.x), min(a.y, b.y)); }
    60 template <typename T> XY<T> xymax(const XY<T>& a, const XY<T>& b) { return XY<T>(max(a.x, b.x), max(a.y, b.y)); }
     57template<> inline bool XY<int>::operator==(const XY<int> &p) const { return (x == p.x) && (y == p.y); }
     58
     59template <typename T> XY<T> xymin(const XY<T> &a, const XY<T> &b) { return XY<T>(min(a.x, b.x), min(a.y, b.y)); }
     60template <typename T> XY<T> xymax(const XY<T> &a, const XY<T> &b) { return XY<T>(max(a.x, b.x), max(a.y, b.y)); }
    6161
    6262template <typename T>
     
    8282        XY<T> p, size;
    8383        XYRect() {}
    84         XYRect(const XY<T>& p1, const XY<T>& s) :p(p1), size(s) {}
    85         template <typename Q> XYRect(const Q& other) : p(other.p), size(other.size) {}
     84        XYRect(const XY<T> &p1, const XY<T> &s) :p(p1), size(s) {}
     85        template <typename Q> XYRect(const Q &other) : p(other.p), size(other.size) {}
    8686        XYRect(T _x, T _y, T _w, T _h) :p(_x, _y), size(_w, _h) {}
    87         static XYRect<T> centeredAt(const XY<T>& p, XY<T> s) { return XYRect<T>(p - s * 0.5, s); }
     87        static XYRect<T> centeredAt(const XY<T> &p, XY<T> s) { return XYRect<T>(p - s * 0.5, s); }
    8888
    8989        bool isEmpty() const { return (size.x < 0) || (size.y < 0); }
    9090        XYRect toInt() const { return XYRect(int(p.x), int(p.y), int(p.x + size.x) - int(p.x), int(p.y + size.y) - int(p.y)); }
    91         bool operator==(const XYRect& r) const { return (p == r.p) && (size == r.size); }
    92         template <typename Q> const XYRect& operator=(const Q& other) { p = other.p; size = other.size; return *this; }
     91        bool operator==(const XYRect &r) const { return (p == r.p) && (size == r.size); }
     92        template <typename Q> const XYRect &operator=(const Q &other) { p = other.p; size = other.size; return *this; }
    9393
    9494        T right() const { return p.x + size.x; }
     
    9797        T left() const { return p.x; }
    9898        XY<T> center() const { return p + size / 2; }
    99         const XY<T>& topLeft() const { return p; }
     99        const XY<T> &topLeft() const { return p; }
    100100        XY<T> bottomRight() const { return p + size; }
    101101        XY<T> topRight() const { return XY<T>(p.x + size.x, p.y); }
    102102        XY<T> bottomLeft() const { return XY<T>(p.x, p.y + size.y); }
    103103
    104         T area() const { return size.x*size.y; }
    105 
    106         bool intersects(const XYRect& r) const
     104        T area() const { return size.x * size.y; }
     105
     106        bool intersects(const XYRect &r) const
    107107        {
    108108                if (r.p.x >= (p.x + size.x)) return false;
     
    113113        }
    114114
    115         bool contains(const XY<T>& n) const
     115        bool contains(const XY<T> &n) const
    116116        {
    117117                if (n.x < p.x) return false;
     
    122122        }
    123123
    124         bool contains(const XYRect& r) const
     124        bool contains(const XYRect &r) const
    125125        {
    126126                return contains(r.p) && contains(r.p + r.size);
    127127        }
    128128
    129         void add(const XY<T>& n)
     129        void add(const XY<T> &n)
    130130        {
    131131                if (n.x < p.x) { size.x += p.x - n.x; p.x = n.x; }
     
    135135        }
    136136
    137         XYRect extendBy(const XY<T>& border_size) const
     137        XYRect extendBy(const XY<T> &border_size) const
    138138        {
    139139                return XYRect(p - border_size, size + border_size * 2);
    140140        }
    141141
    142         XYRect shrinkBy(const XY<T>& border_size) const
     142        XYRect shrinkBy(const XY<T> &border_size) const
    143143        {
    144144                return XYRect(p + border_size, size - border_size * 2);
    145145        }
    146146
    147         XYRect extendBy(const XYMargin<T>& m) const
     147        XYRect extendBy(const XYMargin<T> &m) const
    148148        {
    149149                return XYRect(p.x - m.left, p.y - m.top, size.x + m.horizontal(), size.y + m.vertical());
    150150        }
    151151
    152         XYRect shrinkBy(const XYMargin<T>& m) const
     152        XYRect shrinkBy(const XYMargin<T> &m) const
    153153        {
    154154                return XYRect(p.x + m.left, p.y + m.top, size.x - m.horizontal(), size.y - m.vertical());
     
    164164        {
    165165                XYRect r;
    166                 r.size=size;
    167                 if (size.x < size.y*aspect)
    168                         r.size.y = r.size.x/aspect;
     166                r.size = size;
     167                if (size.x < size.y * aspect)
     168                        r.size.y = r.size.x / aspect;
    169169                else
    170                         r.size.x = r.size.y*aspect;
    171                 r.p=p+(size-r.size)*0.5;
    172                 return r;               
    173         }
    174        
    175         XYRect intersection(const XYRect& r) const
     170                        r.size.x = r.size.y * aspect;
     171                r.p = p + (size - r.size) * 0.5;
     172                return r;
     173        }
     174
     175        XYRect intersection(const XYRect &r) const
    176176        {
    177177                XYRect i;
     
    185185        }
    186186
    187         XYRect extensionContaining(const XYRect& r) const
     187        XYRect extensionContaining(const XY<T> &p) const
     188        {
     189                XY<T> p1 = xymin(topLeft(), p);
     190                XY<T> p2 = xymax(bottomRight(), p);
     191                return XYRect(p1, p2 - p1);
     192        }
     193
     194        XYRect extensionContaining(const XYRect &r) const
    188195        {
    189196                XY<T> p1 = xymin(topLeft(), r.topLeft());
     
    192199        }
    193200
    194         XYRect translation(const XY<T>& t) const
     201        XYRect translation(const XY<T> &t) const
    195202        {
    196203                return XYRect(p + t, size);
    197204        }
    198205
    199         T distanceTo(const XY<T>& n) const
     206        T distanceTo(const XY<T> &n) const
    200207        {
    201208                XY<T> tp = n;
     
    205212        }
    206213
    207         T distanceTo(const XYRect<T>& r) const
     214        T distanceTo(const XYRect<T> &r) const
    208215        {
    209216                bool r_above = (r.bottom() <= top());
     
    236243        }
    237244
    238         static const XYRect& zero() { static XYRect t(0, 0, 0, 0); return t; }
    239         static const XYRect& one() { static XYRect t(0, 0, 1, 1); return t; }
     245        static const XYRect &zero() { static XYRect t(0, 0, 0, 0); return t; }
     246        static const XYRect &one() { static XYRect t(0, 0, 1, 1); return t; }
    240247};
    241248
Note: See TracChangeset for help on using the changeset viewer.