| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <cmath> |
| 24 |
#include <cstdint> |
| 25 |
|
| 26 |
#ifndef M_PI |
| 27 |
#define M_PI 3.14159265358979323846 |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
namespace fheroes2 |
| 32 |
{ |
| 33 |
|
| 34 |
template <typename _Type> |
| 35 |
constexpr bool isEqual( const _Type & value1, const _Type & value2 ) |
| 36 |
{ |
| 37 |
return ( value1 == value2 ); |
| 38 |
} |
| 39 |
|
| 40 |
template <typename _Type> |
| 41 |
struct PointBase2D |
| 42 |
{ |
| 43 |
constexpr PointBase2D() |
| 44 |
: x( 0 ) |
| 45 |
, y( 0 ) |
| 46 |
{} |
| 47 |
|
| 48 |
constexpr PointBase2D( _Type _x, _Type _y ) |
| 49 |
: x( _x ) |
| 50 |
, y( _y ) |
| 51 |
{} |
| 52 |
|
| 53 |
constexpr bool operator==( const PointBase2D & point ) const |
| 54 |
{ |
| 55 |
return isEqual( x, point.x ) && isEqual( y, point.y ); |
| 56 |
} |
| 57 |
|
| 58 |
constexpr bool operator!=( const PointBase2D & point ) const |
| 59 |
{ |
| 60 |
return !( *this == point ); |
| 61 |
} |
| 62 |
|
| 63 |
constexpr PointBase2D & operator+=( const PointBase2D & point ) |
| 64 |
{ |
| 65 |
x += point.x; |
| 66 |
y += point.y; |
| 67 |
return *this; |
| 68 |
} |
| 69 |
|
| 70 |
constexpr PointBase2D & operator-=( const PointBase2D & point ) |
| 71 |
{ |
| 72 |
x -= point.x; |
| 73 |
y -= point.y; |
| 74 |
return *this; |
| 75 |
} |
| 76 |
|
| 77 |
constexpr PointBase2D operator+( const PointBase2D & point ) const |
| 78 |
{ |
| 79 |
return PointBase2D( x + point.x, y + point.y ); |
| 80 |
} |
| 81 |
|
| 82 |
constexpr PointBase2D operator-( const PointBase2D & point ) const |
| 83 |
{ |
| 84 |
return PointBase2D( x - point.x, y - point.y ); |
| 85 |
} |
| 86 |
|
| 87 |
constexpr bool operator<( const PointBase2D & point ) const |
| 88 |
{ |
| 89 |
return x == point.x ? y < point.y : x < point.x; |
| 90 |
} |
| 91 |
|
| 92 |
_Type x; |
| 93 |
_Type y; |
| 94 |
}; |
| 95 |
|
| 96 |
template <typename _Type> |
| 97 |
struct SizeBase2D |
| 98 |
{ |
| 99 |
constexpr SizeBase2D() |
| 100 |
: width( 0 ) |
| 101 |
, height( 0 ) |
| 102 |
{} |
| 103 |
|
| 104 |
constexpr SizeBase2D( _Type _width, _Type _height ) |
| 105 |
: width( _width ) |
| 106 |
, height( _height ) |
| 107 |
{} |
| 108 |
|
| 109 |
constexpr bool operator==( const SizeBase2D & size ) const |
| 110 |
{ |
| 111 |
return isEqual( width, size.width ) && isEqual( height, size.height ); |
| 112 |
} |
| 113 |
|
| 114 |
constexpr bool operator!=( const SizeBase2D & size ) const |
| 115 |
{ |
| 116 |
return !( *this == size ); |
| 117 |
} |
| 118 |
|
| 119 |
_Type width; |
| 120 |
_Type height; |
| 121 |
}; |
| 122 |
|
| 123 |
template <typename _TypePoint, typename _TypeSize> |
| 124 |
struct RectBase2D |
| 125 |
{ |
| 126 |
constexpr RectBase2D() |
| 127 |
: x( 0 ) |
| 128 |
, y( 0 ) |
| 129 |
, width( 0 ) |
| 130 |
, height( 0 ) |
| 131 |
{} |
| 132 |
|
| 133 |
constexpr RectBase2D( _TypePoint _x, _TypePoint _y, _TypeSize _width, _TypeSize _height ) |
| 134 |
: x( _x ) |
| 135 |
, y( _y ) |
| 136 |
, width( _width ) |
| 137 |
, height( _height ) |
| 138 |
{} |
| 139 |
|
| 140 |
constexpr RectBase2D( const PointBase2D<_TypePoint> & point, const SizeBase2D<_TypeSize> & size ) |
| 141 |
: x( point.x ) |
| 142 |
, y( point.y ) |
| 143 |
, width( size.width ) |
| 144 |
, height( size.height ) |
| 145 |
{} |
| 146 |
|
| 147 |
constexpr bool operator==( const RectBase2D & rect ) const |
| 148 |
{ |
| 149 |
return isEqual( x, rect.x ) && isEqual( y, rect.y ) && isEqual( width, rect.width ) && isEqual( height, rect.height ); |
| 150 |
} |
| 151 |
|
| 152 |
constexpr bool operator!=( const RectBase2D & rect ) const |
| 153 |
{ |
| 154 |
return !( *this == rect ); |
| 155 |
} |
| 156 |
|
| 157 |
constexpr RectBase2D & operator+=( const PointBase2D<_TypePoint> & point ) |
| 158 |
{ |
| 159 |
x += point.x; |
| 160 |
y += point.y; |
| 161 |
return *this; |
| 162 |
} |
| 163 |
|
| 164 |
constexpr RectBase2D & operator-=( const PointBase2D<_TypePoint> & point ) |
| 165 |
{ |
| 166 |
x -= point.x; |
| 167 |
y -= point.y; |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
constexpr RectBase2D operator+( const PointBase2D<_TypePoint> & point ) const |
| 172 |
{ |
| 173 |
return RectBase2D( x + point.x, y + point.y, width, height ); |
| 174 |
} |
| 175 |
|
| 176 |
constexpr RectBase2D operator-( const PointBase2D<_TypePoint> & point ) const |
| 177 |
{ |
| 178 |
return RectBase2D( x - point.x, y - point.y, width, height ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
constexpr bool operator&( const PointBase2D<_TypePoint> & point ) const |
| 183 |
{ |
| 184 |
return point.x >= x && point.y >= y && point.x < ( x + width ) && point.y < ( y + height ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
constexpr RectBase2D operator^( const RectBase2D & rect ) const |
| 189 |
{ |
| 190 |
RectBase2D output = rect; |
| 191 |
if ( output.x < x ) { |
| 192 |
const _TypePoint diff = x - output.x; |
| 193 |
output.x = x; |
| 194 |
output.width -= diff; |
| 195 |
} |
| 196 |
if ( output.y < y ) { |
| 197 |
const _TypePoint diff = y - output.y; |
| 198 |
output.y = y; |
| 199 |
output.height -= diff; |
| 200 |
} |
| 201 |
|
| 202 |
if ( output.x > x + width || output.y > y + height ) |
| 203 |
return RectBase2D(); |
| 204 |
|
| 205 |
if ( output.x + output.width > x + width ) { |
| 206 |
const _TypePoint diff = output.x + output.width - ( x + width ); |
| 207 |
output.width -= diff; |
| 208 |
} |
| 209 |
|
| 210 |
if ( output.y + output.height > y + height ) { |
| 211 |
const _TypePoint diff = output.y + output.height - ( y + height ); |
| 212 |
output.height -= diff; |
| 213 |
} |
| 214 |
|
| 215 |
return output; |
| 216 |
} |
| 217 |
|
| 218 |
constexpr PointBase2D<_TypePoint> getPosition() const |
| 219 |
{ |
| 220 |
return PointBase2D<_TypePoint>( x, y ); |
| 221 |
} |
| 222 |
|
| 223 |
_TypePoint x; |
| 224 |
_TypePoint y; |
| 225 |
_TypeSize width; |
| 226 |
_TypeSize height; |
| 227 |
}; |
| 228 |
|
| 229 |
using Point = PointBase2D<int32_t>; |
| 230 |
using Size = SizeBase2D<int32_t>; |
| 231 |
using Rect = RectBase2D<int32_t, int32_t>; |
| 232 |
} |