Changeset 132 for trunk/nv/position.hh
- Timestamp:
- 06/25/13 18:43:24 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/position.hh
r121 r132 32 32 position lr; 33 33 /** 34 * Creates a new rectangle assigned to {0, 0, 0, 0}.34 * Creates a new rectangle assigned to {0, 0, 0, 0}. 35 35 */ 36 36 rectangle() : ul(), lr() {} 37 37 38 38 /** 39 * Creates a new rectangle given a position.40 * 41 * @param p The position to assign the rectangle to.39 * Creates a new rectangle given a position. 40 * 41 * @param p The position to assign the rectangle to. 42 42 */ 43 43 rectangle( position p ) : ul(p), lr(p) {} 44 44 45 45 /** 46 * Creates a new rectangle given an upper-left and lower-right position.47 * 48 * @param aul The position of the upper-left corner of the rectangle.49 * @param alr The position of the lower-right corner of the rectangle.46 * Creates a new rectangle given an upper-left and lower-right position. 47 * 48 * @param aul The position of the upper-left corner of the rectangle. 49 * @param alr The position of the lower-right corner of the rectangle. 50 50 */ 51 51 rectangle( position aul, position alr ) : ul(aul), lr(alr) {} 52 52 53 53 /** 54 * Creates a new rectangle given an upper-left position, width, and height.55 * 56 * @param aul The position of the upper-left corner of the rectangle.57 * @param width The width of the rectangle.58 * @param height The height of the rectangle.54 * Creates a new rectangle given an upper-left position, width, and height. 55 * 56 * @param aul The position of the upper-left corner of the rectangle. 57 * @param width The width of the rectangle. 58 * @param height The height of the rectangle. 59 59 */ 60 60 rectangle( position aul, value_type width, value_type height ) : ul(aul), lr(aul + position(width,height)) {} … … 66 66 67 67 /** 68 * Explicit Copy constructor68 * Explicit Copy assignment operator 69 69 */ 70 70 rectangle& operator= (const rectangle& r) { ul = r.ul; lr = r.lr; return *this; } 71 71 72 72 /** 73 * Sets the dimensions of the rectangle without moving the upper-left of the rectangle.74 * 75 * @param d The new dimensions of the rectangle.73 * Sets the dimensions of the rectangle without moving the upper-left of the rectangle. 74 * 75 * @param d The new dimensions of the rectangle. 76 76 */ 77 77 rectangle& dim( dimension d ) { lr = ul + d; return *this; } 78 78 79 79 /** 80 * Moves the rectangle to a new position while maintaining its size.81 * 82 * @param p The new position of the rectangle's upper-left corner.80 * Moves the rectangle to a new position while maintaining its size. 81 * 82 * @param p The new position of the rectangle's upper-left corner. 83 83 */ 84 84 rectangle& pos( position p ) { lr = p + (lr - ul); lr = p; return *this; } 85 85 86 86 /** 87 * Sets the dimensions of the rectangle without moving the upper-left of the rectangle.88 * 89 * @param d The new dimensions of the rectangle.87 * Sets the dimensions of the rectangle without moving the upper-left of the rectangle. 88 * 89 * @param d The new dimensions of the rectangle. 90 90 */ 91 91 void set_dimension( dimension d ) { lr = ul + d; } 92 92 93 93 /** 94 * Moves the rectangle to a new position while maintaining its size.95 * 96 * @param p The new position of the rectangle's upper-left corner.94 * Moves the rectangle to a new position while maintaining its size. 95 * 96 * @param p The new position of the rectangle's upper-left corner. 97 97 */ 98 98 void set_position( position p ) { lr = p + (lr - ul); ul = p; } … … 101 101 position ll() const { return position( ul.x, lr.y ); } 102 102 /** 103 * Gets the dimensions of the rectangle. Synonym for get_size.104 * 105 * @returns The dimensions of the rectangle.106 * @see get_size103 * Gets the dimensions of the rectangle. Synonym for get_size. 104 * 105 * @returns The dimensions of the rectangle. 106 * @see get_size 107 107 */ 108 108 dimension get_dimension() const { return lr - ul; } 109 109 110 110 /** 111 * Gets the position of the upper-left corner of the rectangle.112 * 113 * @returns The position of the rectangle's upper-left corner.111 * Gets the position of the upper-left corner of the rectangle. 112 * 113 * @returns The position of the rectangle's upper-left corner. 114 114 */ 115 115 position get_position() const { return ul; } 116 116 117 117 /** 118 * Gets the dimensions of the rectangle. Synonym for get_dimension.119 * 120 * @returns The dimensions of the rectangle.121 * @see get_dimension118 * Gets the dimensions of the rectangle. Synonym for get_dimension. 119 * 120 * @returns The dimensions of the rectangle. 121 * @see get_dimension 122 122 */ 123 123 dimension get_size() const { return lr - ul; } 124 124 125 125 /** 126 * Gets the center of the rectangle.127 * 128 * @returns The center of the rectangle.126 * Gets the center of the rectangle. 127 * 128 * @returns The center of the rectangle. 129 129 */ 130 130 position get_center() const { return ( lr + ul ) / 2; } 131 131 132 132 /** 133 * Gets the width of the rectangle.134 * 135 * @returns The width of the rectangle.133 * Gets the width of the rectangle. 134 * 135 * @returns The width of the rectangle. 136 136 */ 137 137 value_type get_width() const { return lr.x - ul.x; } 138 138 139 139 /** 140 * Gets the height of the rectangle.141 * 142 * @returns The height of the rectangle.140 * Gets the height of the rectangle. 141 * 142 * @returns The height of the rectangle. 143 143 */ 144 144 value_type get_height() const { return lr.y - ul.y; } 145 145 146 146 /** 147 * Gets the area of the rectangle.148 * 149 * @returns The area of the rectangle.147 * Gets the area of the rectangle. 148 * 149 * @returns The area of the rectangle. 150 150 */ 151 151 value_type get_area() const { return (lr.y - ul.y) * (lr.x - ul.x); } 152 152 153 153 /** 154 * Checks to see if the rectangle is backwards.155 * 156 * @returns True if the rectangle's upper-left is above and left (or equal to) the rectangle's lower-right, false if it is not.154 * Checks to see if the rectangle is backwards. 155 * 156 * @returns True if the rectangle's upper-left is above and left (or equal to) the rectangle's lower-right, false if it is not. 157 157 */ 158 158 bool is_valid() const { return lr.x >= ul.x && lr.y >= ul.y; } 159 159 160 160 /** 161 * Enlarges the rectangle by a given amount. Each side is adjusted by the given amount, e.g. expanding by 1 increases the width and height by 2 each.162 * 163 * @param value The amount to adjust each sides by.161 * Enlarges the rectangle by a given amount. Each side is adjusted by the given amount, e.g. expanding by 1 increases the width and height by 2 each. 162 * 163 * @param value The amount to adjust each sides by. 164 164 */ 165 165 void expand( value_type value ) { position p(value,value); ul -= p; lr += p; } 166 166 167 167 /** 168 * Reduces the rectangle by a given amount. Each side is adjusted by the given amount, e.g. shrinking by 1 decreases the width and height by 2 each.169 * 170 * @param value The amount to adjust each side by.168 * Reduces the rectangle by a given amount. Each side is adjusted by the given amount, e.g. shrinking by 1 decreases the width and height by 2 each. 169 * 170 * @param value The amount to adjust each side by. 171 171 */ 172 172 void shrink( value_type value ) { position p(value,value); ul += p; lr -= p; } 173 173 174 174 /** 175 * Gets a rectangle that is an expanded version of this rectangle.176 * 177 * @param value The amount to adjust each side by.178 * @returns An expanded rectangle.179 * @see expand175 * Gets a rectangle that is an expanded version of this rectangle. 176 * 177 * @param value The amount to adjust each side by. 178 * @returns An expanded rectangle. 179 * @see expand 180 180 */ 181 181 rectangle expanded( int value ) { position p(value,value); return rectangle(ul-p, lr+p); } 182 182 183 183 /** 184 * Gets a rectangle that is a shrunk version of this rectangle.185 * 186 * @param value The amount to adjust each side by.187 * @returns A shrunk rectangle.188 * @see shrink184 * Gets a rectangle that is a shrunk version of this rectangle. 185 * 186 * @param value The amount to adjust each side by. 187 * @returns A shrunk rectangle. 188 * @see shrink 189 189 */ 190 190 rectangle shrinked( int value ) { position p(value,value); return rectangle(ul+p, lr-p); } 191 191 192 192 /** 193 * Shifts the rectangle by a given amount.194 * 195 * @param pos The amount to shift the rectangle by.193 * Shifts the rectangle by a given amount. 194 * 195 * @param pos The amount to shift the rectangle by. 196 196 */ 197 197 rectangle& operator+=( const position& pos ) { ul += pos; lr += pos; return (*this); } 198 198 199 199 /** 200 * Returns a rectangle shifted by a given amount.201 * 202 * @param pos The amount to shift by.203 * @returns The shifted rectangle.200 * Returns a rectangle shifted by a given amount. 201 * 202 * @param pos The amount to shift by. 203 * @returns The shifted rectangle. 204 204 */ 205 205 rectangle operator+( const position& pos ) const { rectangle r(*this); return r += pos; } 206 206 207 207 /** 208 * Shifts the rectangle by a given amount.209 * 210 * @param pos The amount to shift the rectangle by.208 * Shifts the rectangle by a given amount. 209 * 210 * @param pos The amount to shift the rectangle by. 211 211 */ 212 212 rectangle& operator-=( const position& pos ) { ul -= pos; lr -= pos; return (*this); } 213 213 214 214 /** 215 * Returns a rectangle shifted by a given amount.216 * 217 * @oaram pos The amount to shift by.218 * @returns The shifted rectangle.215 * Returns a rectangle shifted by a given amount. 216 * 217 * @param pos The amount to shift by. 218 * @returns The shifted rectangle. 219 219 */ 220 220 rectangle operator-( const position& pos ) const { rectangle r(*this); return r -= pos; } 221 221 222 222 /** 223 * Compares two rectangles to see if they are the same.224 * 225 * @param r The rectangle to compare to.226 * @returns True if the rectangles have the same positions and dimensions, false otherwise.223 * Compares two rectangles to see if they are the same. 224 * 225 * @param r The rectangle to compare to. 226 * @returns True if the rectangles have the same positions and dimensions, false otherwise. 227 227 */ 228 228 bool operator==( const rectangle& r ) const { return r.ul == ul && r.lr == lr; } 229 229 230 230 /** 231 * Compares two rectangles to see if they are different.232 * 233 * @param r The rectangle to compare to.234 * @returns True if the rectangles have different positions or dimensions, false otherwise.231 * Compares two rectangles to see if they are different. 232 * 233 * @param r The rectangle to compare to. 234 * @returns True if the rectangles have different positions or dimensions, false otherwise. 235 235 */ 236 236 bool operator!=( const rectangle& r ) const { return r.ul != ul || r.lr != lr; } 237 237 238 238 /** 239 * Checks if a position is within the bounds of this rectangle.240 * 241 * @param r The position to check.242 * @returns True if the position is inside or on the edge of the rectangle, false otherwise.239 * Checks if a position is within the bounds of this rectangle. 240 * 241 * @param r The position to check. 242 * @returns True if the position is inside or on the edge of the rectangle, false otherwise. 243 243 */ 244 244 bool contains( const position& r ) const{ return lr.y >= r.y && ul.y <= r.y && lr.x >= r.x && ul.x <= r.x; } 245 245 246 246 /** 247 * Checks if a rectangle is within the bounds of this rectangle.248 * 249 * @param r The rectangle to check.250 * @returns True if the entire rectangle to check is inside or on the edge of this rectangle, false otherwise.247 * Checks if a rectangle is within the bounds of this rectangle. 248 * 249 * @param r The rectangle to check. 250 * @returns True if the entire rectangle to check is inside or on the edge of this rectangle, false otherwise. 251 251 */ 252 252 bool contains( const rectangle& r ) const { return contains( r.ul ) && contains( r.lr ); } 253 253 254 254 /** 255 * Checks if another rectangle overlaps this one.256 * 257 * @param r The rectangle to check.258 * @returns True if any part of the rectangle to check overlaps this rectangle, false otherwise.255 * Checks if another rectangle overlaps this one. 256 * 257 * @param r The rectangle to check. 258 * @returns True if any part of the rectangle to check overlaps this rectangle, false otherwise. 259 259 */ 260 260 bool collides( const rectangle& r ) const { return lr.y > r.ul.y && ul.y < r.lr.y && lr.x > r.ul.x && ul.x < r.lr.x; } 261 261 262 262 /** 263 * Limits the region of the rectangle to the inidicated rectangle.264 * 265 * @param r The rectangle that this rectangle is confined to.266 * @returns True if the rectangle was changed, false otherwise.263 * Limits the region of the rectangle to the inidicated rectangle. 264 * 265 * @param r The rectangle that this rectangle is confined to. 266 * @returns True if the rectangle was changed, false otherwise. 267 267 */ 268 268 bool clamp_to( const rectangle& r ) … … 276 276 277 277 /** 278 * Limits the size of the rectangle to the given rectangle's size.279 * 280 * @param r The rectangle representing the maximum dimensions this rectangle can be.281 * @returns True if the rectangle needed to be resized, false otherwise.278 * Limits the size of the rectangle to the given rectangle's size. 279 * 280 * @param r The rectangle representing the maximum dimensions this rectangle can be. 281 * @returns True if the rectangle needed to be resized, false otherwise. 282 282 */ 283 283 bool constrain_to( const rectangle& r ) … … 290 290 291 291 /** 292 * Fixes an invalid rectangle.292 * Fixes an invalid rectangle. 293 293 */ 294 294 void repair() … … 299 299 300 300 /** 301 * Expands the rectangle to just include the given point.302 * 303 * @param p The point to include in the rectangle.301 * Expands the rectangle to just include the given point. 302 * 303 * @param p The point to include in the rectangle. 304 304 */ 305 305 void include_point( position p )
Note: See TracChangeset
for help on using the changeset viewer.