| 1 | constexpr inline ImageRef::ImageRef() |
| 2 | :x(0),y(0) |
| 3 | { |
| 4 | } |
| 5 | |
| 6 | constexpr inline ImageRef::ImageRef(int xp, int yp) |
| 7 | :x(xp),y(yp) |
| 8 | {} |
| 9 | |
| 10 | inline ImageRef::ImageRef(std::istream& is) |
| 11 | { |
| 12 | is.read((char*)&x,sizeof(int)); |
| 13 | is.read((char*)&y,sizeof(int)); |
| 14 | } |
| 15 | |
| 16 | ////////////////////////////////////////////////////////////////////////// |
| 17 | // the following cryptic pieces of rubbish are because inline functions // |
| 18 | // must have their one and only return function as the last call // |
| 19 | // so this code makes use of the fact that expressions to the right // |
| 20 | // of || are only evaluated when the left hand side is false // |
| 21 | ////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | inline bool ImageRef::next(const ImageRef& max) // move on to the next value |
| 24 | { |
| 25 | return(++x < max.x || (x=0, ++y < max.y) || (y=0, false)); |
| 26 | } |
| 27 | |
| 28 | inline bool ImageRef::next(const ImageRef& min, const ImageRef& max) |
| 29 | { |
| 30 | return (++x < max.x || (x=min.x, ++y < max.y) || (y=min.y, false)); |
| 31 | } |
| 32 | |
| 33 | inline bool ImageRef::prev(const ImageRef& max) // move back to the previous value |
| 34 | { |
| 35 | return(--x > -1 || (x=max.x-1, --y > -1) || (y=max.y-1, false)); |
| 36 | } |
| 37 | |
| 38 | inline bool ImageRef::prev(const ImageRef& min, const ImageRef& max) |
| 39 | { |
| 40 | return (--x > min.x-1 || (x=max.x-1, --y > min.y-1) || (y=max.y-1, false)); |
| 41 | } |
| 42 | |
| 43 | inline void ImageRef::home() |
| 44 | { |
| 45 | x=y=0; |
| 46 | } |
| 47 | |
| 48 | inline void ImageRef::end(const ImageRef& size) |
| 49 | { |
| 50 | x=size.x-1; |
| 51 | y=size.y-1; |
| 52 | } |
| 53 | |
| 54 | constexpr inline ImageRef& ImageRef::operator=(const ImageRef& ref) |
| 55 | { |
| 56 | x=ref.x; |
| 57 | y=ref.y; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | constexpr inline bool ImageRef::operator ==(const ImageRef& ref) const |
| 62 | { |
| 63 | return (x==ref.x && y==ref.y); |
| 64 | } |
| 65 | |
| 66 | constexpr inline bool ImageRef::operator !=(const ImageRef& ref) const |
| 67 | { |
| 68 | return (x!=ref.x || y!=ref.y); |
| 69 | } |
| 70 | |
| 71 | constexpr inline ImageRef ImageRef::operator-() const |
| 72 | { |
| 73 | ImageRef v(-x, -y); |
| 74 | return v; |
| 75 | } |
| 76 | |
| 77 | constexpr inline ImageRef& ImageRef::operator*=(const double scale) |
| 78 | { |
| 79 | x=(int)(x*scale); |
| 80 | y=(int)(y*scale); |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | constexpr inline ImageRef& ImageRef::operator/=(const double scale) |
| 85 | { |
| 86 | x=(int)(x/scale); |
| 87 | y=(int)(y/scale); |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | constexpr inline ImageRef& ImageRef::operator+=(const ImageRef rhs) |
| 92 | { |
| 93 | x+=rhs.x; |
| 94 | y+=rhs.y; |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | constexpr inline ImageRef& ImageRef::operator-=(const ImageRef rhs) |
| 99 | { |
| 100 | x-=rhs.x; |
| 101 | y-=rhs.y; |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | constexpr inline ImageRef ImageRef::operator*(const double scale) const |
| 106 | { |
| 107 | ImageRef v((int)(x*scale),(int)(y*scale)); |
| 108 | return v; |
| 109 | } |
| 110 | |
| 111 | constexpr inline ImageRef ImageRef::operator/(const double scale) const |
| 112 | { |
| 113 | ImageRef v((int)(x/scale),(int)(y/scale)); |
| 114 | return v; |
| 115 | } |
| 116 | |
| 117 | constexpr inline ImageRef ImageRef::operator+(const ImageRef rhs) const |
| 118 | { |
| 119 | ImageRef v(x+rhs.x, y+rhs.y); |
| 120 | return v; |
| 121 | } |
| 122 | |
| 123 | constexpr inline ImageRef ImageRef::operator-(const ImageRef rhs) const |
| 124 | { |
| 125 | ImageRef v(x-rhs.x, y-rhs.y); |
| 126 | return v; |
| 127 | } |
| 128 | |
| 129 | constexpr inline ImageRef& ImageRef::operator<<=(int i) |
| 130 | { |
| 131 | x = x << i; |
| 132 | y=y << i; |
| 133 | return *this; |
| 134 | } |
| 135 | |
| 136 | constexpr inline ImageRef& ImageRef::operator>>=(int i) |
| 137 | { |
| 138 | x = x >> i; |
| 139 | y=y >> i; |
| 140 | return *this; |
| 141 | } |
| 142 | |
| 143 | constexpr inline ImageRef ImageRef::shiftl(int i) const |
| 144 | { |
| 145 | ImageRef result; |
| 146 | result.x = x << i; |
| 147 | result.y=y << i; |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | constexpr inline ImageRef ImageRef::shiftr(int i) const |
| 152 | { |
| 153 | ImageRef result; |
| 154 | result.x = x >> i; |
| 155 | result.y=y >> i; |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | constexpr inline ImageRef ImageRef::operator<<(int i) const |
| 160 | { |
| 161 | return shiftl(i); |
| 162 | } |
| 163 | |
| 164 | constexpr inline ImageRef ImageRef::operator>>(int i) const |
| 165 | { |
| 166 | return shiftr(i); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | constexpr inline ImageRef operator*(const int scale, const ImageRef& ref) |
| 171 | { |
| 172 | return ImageRef(ref.x*scale, ref.y*scale); |
| 173 | } |
| 174 | |
| 175 | constexpr inline bool ImageRef::operator<(const ImageRef & other) const |
| 176 | { |
| 177 | return y < other.y || ( y == other.y && x < other.x); |
| 178 | } |
| 179 | |
| 180 | constexpr inline bool ImageRef::operator>(const ImageRef & other) const |
| 181 | { |
| 182 | return y > other.y || ( y == other.y && x > other.x); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | constexpr inline int& ImageRef::operator[](int i) |
| 187 | { |
| 188 | if(i==0) |
| 189 | return x; |
| 190 | else if(i==1) |
| 191 | return y; |
| 192 | else |
| 193 | throw Exceptions::BadSubscript(); |
| 194 | } |
| 195 | |
| 196 | constexpr inline int ImageRef::operator[](int i) const |
| 197 | { |
| 198 | if(i==0) |
| 199 | return x; |
| 200 | else if(i==1) |
| 201 | return y; |
| 202 | else |
| 203 | throw Exceptions::BadSubscript(); |
| 204 | } |
| 205 | |
| 206 | constexpr inline unsigned int ImageRef::mag_squared() const |
| 207 | { |
| 208 | typedef unsigned int uint; |
| 209 | return uint(x*x) + uint(y*y); |
| 210 | } |
| 211 | |
| 212 | constexpr inline int ImageRef::area() const |
| 213 | { |
| 214 | return x * y; |
| 215 | } |
| 216 | |
| 217 | constexpr inline ImageRef ImageRef::dot_times(const ImageRef &ref) const |
| 218 | { |
| 219 | return ImageRef(x * ref.x, y * ref.y); |
| 220 | } |