| 1 |
/* File: geometry_rectangle.h; Copyright and License: see below */ |
| 2 |
|
| 3 |
#ifndef GEOMETRY_RECTANGLE_H |
| 4 |
#define GEOMETRY_RECTANGLE_H |
| 5 |
|
| 6 |
/* public file for the doxygen documentation: */ |
| 7 |
/*! |
| 8 |
* \file |
| 9 |
* \brief Stores coordinates of a rectangle |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "geometry/geometry_point.h" |
| 13 |
#include "geometry/geometry_dimensions.h" |
| 14 |
#include <stdbool.h> |
| 15 |
|
| 16 |
/*! |
| 17 |
* \brief attributes of a rectangle |
| 18 |
*/ |
| 19 |
struct geometry_rectangle_struct { |
| 20 |
double left; |
| 21 |
double top; |
| 22 |
double width; |
| 23 |
double height; |
| 24 |
}; |
| 25 |
|
| 26 |
typedef struct geometry_rectangle_struct geometry_rectangle_t; |
| 27 |
|
| 28 |
/*! |
| 29 |
* \brief initializes the geometry_rectangle_t struct |
| 30 |
* |
| 31 |
* \param this_ pointer to own object attributes |
| 32 |
* \param left left coordinate of the rectangle |
| 33 |
* \param top top coordinate of the rectangle |
| 34 |
* \param width width of the rectangle |
| 35 |
* \param height height of the rectangle |
| 36 |
*/ |
| 37 |
static inline void geometry_rectangle_init ( geometry_rectangle_t *this_, double left, double top, double width, double height ); |
| 38 |
|
| 39 |
/*! |
| 40 |
* \brief re-initializes the geometry_rectangle_t struct |
| 41 |
* |
| 42 |
* \param this_ pointer to own object attributes |
| 43 |
* \param left left coordinate of the rectangle |
| 44 |
* \param top top coordinate of the rectangle |
| 45 |
* \param width width of the rectangle |
| 46 |
* \param height height of the rectangle |
| 47 |
*/ |
| 48 |
static inline void geometry_rectangle_reinit ( geometry_rectangle_t *this_, double left, double top, double width, double height ); |
| 49 |
|
| 50 |
/*! |
| 51 |
* \brief copies original to this uninitialized geometry_rectangle_t struct |
| 52 |
* |
| 53 |
* \param this_ pointer to own object attributes |
| 54 |
* \param original pointer to object attributes that shall be copied |
| 55 |
*/ |
| 56 |
static inline void geometry_rectangle_copy ( geometry_rectangle_t *this_, const geometry_rectangle_t *original ); |
| 57 |
|
| 58 |
/*! |
| 59 |
* \brief moves original to this uninitialized geometry_rectangle_t struct |
| 60 |
* |
| 61 |
* \param this_ pointer to own object attributes |
| 62 |
* \param that pointer to object attributes that shall be moved |
| 63 |
*/ |
| 64 |
static inline void geometry_rectangle_move ( geometry_rectangle_t *this_, geometry_rectangle_t *that ); |
| 65 |
|
| 66 |
/*! |
| 67 |
* \brief replaces the already initialized geometry_rectangle_t struct by other data |
| 68 |
* |
| 69 |
* \param this_ pointer to own object attributes |
| 70 |
* \param original pointer to object attributes that shall be copied |
| 71 |
*/ |
| 72 |
static inline void geometry_rectangle_replace ( geometry_rectangle_t *this_, const geometry_rectangle_t *original ); |
| 73 |
|
| 74 |
/*! |
| 75 |
* \brief replaces the already initialized geometry_rectangle_t struct by other data to be moved there |
| 76 |
* |
| 77 |
* \param this_ pointer to own object attributes |
| 78 |
* \param that pointer to object attributes that shall be copied |
| 79 |
*/ |
| 80 |
static inline void geometry_rectangle_replacemove ( geometry_rectangle_t *this_, geometry_rectangle_t *that ); |
| 81 |
|
| 82 |
/*! |
| 83 |
* \brief initializes the geometry_rectangle_t struct to an empty rect at position 0,0 |
| 84 |
* |
| 85 |
* \param this_ pointer to own object attributes |
| 86 |
*/ |
| 87 |
static inline void geometry_rectangle_init_empty ( geometry_rectangle_t *this_ ); |
| 88 |
|
| 89 |
/*! |
| 90 |
* \brief re-initializes the geometry_rectangle_t struct to an empty rect at position 0,0 |
| 91 |
* |
| 92 |
* \param this_ pointer to own object attributes |
| 93 |
*/ |
| 94 |
static inline void geometry_rectangle_reinit_empty ( geometry_rectangle_t *this_ ); |
| 95 |
|
| 96 |
/*! |
| 97 |
* \brief initializes the geometry_rectangle_t struct by the intersect of a and b |
| 98 |
* |
| 99 |
* It is valid if parameters this_ and/or rect_a and/or rect_b are identical (same pointer). |
| 100 |
* |
| 101 |
* \param this_ pointer to own object attributes |
| 102 |
* \param rect_a rectangle a. Must not be NULL |
| 103 |
* \param rect_b rectangle b. Must not be NULL |
| 104 |
* \return 0 in case of success, -1 if the rectangles a and b do not overlap (touching is intersecting) |
| 105 |
*/ |
| 106 |
static inline int geometry_rectangle_init_by_intersect ( geometry_rectangle_t *this_, |
| 107 |
const geometry_rectangle_t *rect_a, |
| 108 |
const geometry_rectangle_t *rect_b |
| 109 |
); |
| 110 |
|
| 111 |
/*! |
| 112 |
* \brief initializes the geometry_rectangle_t struct by the minimum bounding box of a and b (similar to a convex hull) |
| 113 |
* |
| 114 |
* It is valid if parameters this_ and/or rect_a and/or rect_b are identical (same pointer). |
| 115 |
* |
| 116 |
* \param this_ pointer to own object attributes |
| 117 |
* \param rect_a rectangle a. Must not be NULL |
| 118 |
* \param rect_b rectangle b. Must not be NULL |
| 119 |
* \return 0 in case of success (always) |
| 120 |
*/ |
| 121 |
static inline int geometry_rectangle_init_by_bounds ( geometry_rectangle_t *this_, |
| 122 |
const geometry_rectangle_t *rect_a, |
| 123 |
const geometry_rectangle_t *rect_b |
| 124 |
); |
| 125 |
|
| 126 |
/*! |
| 127 |
* \brief initializes the geometry_rectangle_t struct by providing two corner coordinates |
| 128 |
* |
| 129 |
* \param this_ pointer to own object attributes |
| 130 |
* \param x1 x coordinate of a corner |
| 131 |
* \param y1 y coordinate of a corner |
| 132 |
* \param x2 x coordinate of a corner |
| 133 |
* \param y2 y coordinate of a corner |
| 134 |
*/ |
| 135 |
static inline void geometry_rectangle_init_by_corners ( geometry_rectangle_t *this_, double x1, double y1, double x2, double y2 ); |
| 136 |
|
| 137 |
/*! |
| 138 |
* \brief initializes the geometry_rectangle_t struct by the difference of a minus b. |
| 139 |
* |
| 140 |
* If the difference is not a rectangle, the algorithm chooses the maximum contained rectange. |
| 141 |
* |
| 142 |
* It is valid if parameters this_ and/or rect_a and/or rect_b are identical (same pointer). |
| 143 |
* |
| 144 |
* \param this_ pointer to own object attributes |
| 145 |
* \param rect_a rectangle a. Must not be NULL |
| 146 |
* \param rect_b rectangle b. Must not be NULL |
| 147 |
* \return 0 in case of success (always) |
| 148 |
*/ |
| 149 |
int geometry_rectangle_init_by_difference_max ( geometry_rectangle_t *this_, |
| 150 |
const geometry_rectangle_t *rect_a, |
| 151 |
const geometry_rectangle_t *rect_b |
| 152 |
); |
| 153 |
|
| 154 |
/*! |
| 155 |
* \brief initializes the geometry_rectangle_t struct by the difference of moon minus shadow. |
| 156 |
* |
| 157 |
* If the difference is not a rectangle, the algorithm chooses the rectange closest to pivot_point. |
| 158 |
* If the pivot-point is at the shadow side of the moon, the resulting rectangle may be empty. |
| 159 |
* |
| 160 |
* It is valid to call with identical parameters this_ and/or moon and/or shadow (same pointer). |
| 161 |
* |
| 162 |
* \param this_ pointer to own object attributes |
| 163 |
* \param moon the full rectangle from which to subtract shadow. Must not be NULL |
| 164 |
* \param shadow shadow rectangle which to subtract from the full moon. Must not be NULL |
| 165 |
* \param pivot_point point that determines the selection of the result in case the difference is not a rectangle. |
| 166 |
* Must not be NULL |
| 167 |
* \return 0 in case of success (always) |
| 168 |
*/ |
| 169 |
int geometry_rectangle_init_by_difference_at_pivot ( geometry_rectangle_t *this_, |
| 170 |
const geometry_rectangle_t *moon, |
| 171 |
const geometry_rectangle_t *shadow, |
| 172 |
const geometry_point_t *pivot_point |
| 173 |
); |
| 174 |
|
| 175 |
/*! |
| 176 |
* \brief destroys the geometry_rectangle_t struct |
| 177 |
* |
| 178 |
* \param this_ pointer to own object attributes |
| 179 |
*/ |
| 180 |
static inline void geometry_rectangle_destroy ( geometry_rectangle_t *this_ ); |
| 181 |
|
| 182 |
/*! |
| 183 |
* \brief gets the left coordinate of geometry_rectangle_t |
| 184 |
* |
| 185 |
* \param this_ pointer to own object attributes |
| 186 |
*/ |
| 187 |
static inline double geometry_rectangle_get_left ( const geometry_rectangle_t *this_ ); |
| 188 |
|
| 189 |
/*! |
| 190 |
* \brief gets the top coordinate of geometry_rectangle_t |
| 191 |
* |
| 192 |
* \param this_ pointer to own object attributes |
| 193 |
*/ |
| 194 |
static inline double geometry_rectangle_get_top ( const geometry_rectangle_t *this_ ); |
| 195 |
|
| 196 |
/*! |
| 197 |
* \brief gets the right coordinate of geometry_rectangle_t |
| 198 |
* |
| 199 |
* \param this_ pointer to own object attributes |
| 200 |
*/ |
| 201 |
static inline double geometry_rectangle_get_right ( const geometry_rectangle_t *this_ ); |
| 202 |
|
| 203 |
/*! |
| 204 |
* \brief gets the bottom coordinate of geometry_rectangle_t |
| 205 |
* |
| 206 |
* \param this_ pointer to own object attributes |
| 207 |
*/ |
| 208 |
static inline double geometry_rectangle_get_bottom ( const geometry_rectangle_t *this_ ); |
| 209 |
|
| 210 |
/*! |
| 211 |
* \brief gets the width of geometry_rectangle_t |
| 212 |
* |
| 213 |
* \param this_ pointer to own object attributes |
| 214 |
*/ |
| 215 |
static inline double geometry_rectangle_get_width ( const geometry_rectangle_t *this_ ); |
| 216 |
|
| 217 |
/*! |
| 218 |
* \brief gets the height of geometry_rectangle_t |
| 219 |
* |
| 220 |
* \param this_ pointer to own object attributes |
| 221 |
*/ |
| 222 |
static inline double geometry_rectangle_get_height ( const geometry_rectangle_t *this_ ); |
| 223 |
|
| 224 |
/*! |
| 225 |
* \brief gets the x-center of geometry_rectangle_t |
| 226 |
* |
| 227 |
* \param this_ pointer to own object attributes |
| 228 |
*/ |
| 229 |
static inline double geometry_rectangle_get_center_x ( const geometry_rectangle_t *this_ ); |
| 230 |
|
| 231 |
/*! |
| 232 |
* \brief gets the y-center of geometry_rectangle_t |
| 233 |
* |
| 234 |
* \param this_ pointer to own object attributes |
| 235 |
*/ |
| 236 |
static inline double geometry_rectangle_get_center_y ( const geometry_rectangle_t *this_ ); |
| 237 |
|
| 238 |
/*! |
| 239 |
* \brief gets the center of geometry_rectangle_t |
| 240 |
* |
| 241 |
* \param this_ pointer to own object attributes |
| 242 |
*/ |
| 243 |
static inline geometry_point_t geometry_rectangle_get_center ( const geometry_rectangle_t *this_ ); |
| 244 |
|
| 245 |
/*! |
| 246 |
* \brief gets the dimensions of geometry_rectangle_t |
| 247 |
* |
| 248 |
* \param this_ pointer to own object attributes |
| 249 |
* \return dimensions (width and height) of the rectangle |
| 250 |
*/ |
| 251 |
static inline geometry_dimensions_t geometry_rectangle_get_dimensions ( const geometry_rectangle_t *this_ ); |
| 252 |
|
| 253 |
/*! |
| 254 |
* \brief gets the area-value of geometry_rectangle_t |
| 255 |
* |
| 256 |
* \param this_ pointer to own object attributes |
| 257 |
* \return space within the rectangle. |
| 258 |
*/ |
| 259 |
static inline double geometry_rectangle_get_area ( const geometry_rectangle_t *this_ ); |
| 260 |
|
| 261 |
/*! |
| 262 |
* \brief determines if a given coordinate is within geometry_rectangle_t |
| 263 |
* |
| 264 |
* \param this_ pointer to own object attributes |
| 265 |
* \param x x coordinate |
| 266 |
* \param y y coordinate |
| 267 |
* \return true if the location is within the rectangle. |
| 268 |
*/ |
| 269 |
static inline bool geometry_rectangle_contains ( const geometry_rectangle_t *this_, double x, double y ); |
| 270 |
|
| 271 |
/*! |
| 272 |
* \brief determines if a given point is within geometry_rectangle_t |
| 273 |
* |
| 274 |
* \param this_ pointer to own object attributes |
| 275 |
* \param point point to check |
| 276 |
* \return true if the point is within the rectangle. |
| 277 |
*/ |
| 278 |
static inline bool geometry_rectangle_contains_point ( const geometry_rectangle_t *this_, const geometry_point_t *point ); |
| 279 |
|
| 280 |
/*! |
| 281 |
* \brief determines if the given rectangle has a valid (positive) size |
| 282 |
* |
| 283 |
* \param this_ pointer to own object attributes |
| 284 |
* \return false if the rectangle has a positive size (width and height). |
| 285 |
*/ |
| 286 |
static inline bool geometry_rectangle_is_empty ( const geometry_rectangle_t *this_ ); |
| 287 |
|
| 288 |
/*! |
| 289 |
* \brief determines if the given rectangle has neither valid (positive) width nor height |
| 290 |
* |
| 291 |
* \param this_ pointer to own object attributes |
| 292 |
* \return true if the rectangle has zero width and zero height. |
| 293 |
*/ |
| 294 |
static inline bool geometry_rectangle_is_point ( const geometry_rectangle_t *this_ ); |
| 295 |
|
| 296 |
/*! |
| 297 |
* \brief determines if the given rectangle intersects the other (not just touching) |
| 298 |
* |
| 299 |
* \param this_ pointer to own object attributes |
| 300 |
* \param that pointer to others object attributes |
| 301 |
* \return false if the rectangle does not intersect the other. (touching is not intersecting) |
| 302 |
*/ |
| 303 |
static inline bool geometry_rectangle_is_intersecting ( const geometry_rectangle_t *this_, const geometry_rectangle_t *that ); |
| 304 |
|
| 305 |
/*! |
| 306 |
* \brief determines if the given rectangle touches or intersects the other |
| 307 |
* |
| 308 |
* \param this_ pointer to own object attributes |
| 309 |
* \param that pointer to others object attributes |
| 310 |
* \return false if the rectangles are not contiguous. (touching is contiguous) |
| 311 |
*/ |
| 312 |
static inline bool geometry_rectangle_is_contiguous ( const geometry_rectangle_t *this_, const geometry_rectangle_t *that ); |
| 313 |
|
| 314 |
/*! |
| 315 |
* \brief determines if the given rectangle contains the other |
| 316 |
* |
| 317 |
* \param this_ pointer to own object attributes |
| 318 |
* \param that pointer to others object attributes |
| 319 |
* \return false if the rectangle does not contain the other. (touching outside is still containing) |
| 320 |
*/ |
| 321 |
static inline bool geometry_rectangle_is_containing ( const geometry_rectangle_t *this_, const geometry_rectangle_t *that ); |
| 322 |
|
| 323 |
/*! |
| 324 |
* \brief determines the chess-distance/manhattan-distance of a given coordinate to this geometry_rectangle_t |
| 325 |
* |
| 326 |
* \param this_ pointer to own object attributes |
| 327 |
* \param x x coordinate |
| 328 |
* \param y y coordinate |
| 329 |
* \return chess-distance from x/y to the rectangle; 0.0 if x/y is contained. |
| 330 |
*/ |
| 331 |
static inline double geometry_rectangle_calc_chess_distance ( const geometry_rectangle_t *this_, double x, double y ); |
| 332 |
|
| 333 |
/*! |
| 334 |
* \brief gets the intersect area of two geometry_rectangle_t |
| 335 |
* |
| 336 |
* \param this_ pointer to own object attributes |
| 337 |
* \param that pointer to others object attributes |
| 338 |
* \return space within the instersect of the rectangles. |
| 339 |
*/ |
| 340 |
static inline double geometry_rectangle_get_intersect_area ( const geometry_rectangle_t *this_, const geometry_rectangle_t *that ); |
| 341 |
|
| 342 |
/*! |
| 343 |
* \brief moves the geometry_rectangle_t |
| 344 |
* |
| 345 |
* \param this_ pointer to own object attributes |
| 346 |
* \param delta_x delta-x difference by which to shift the rectangle |
| 347 |
* \param delta_y delta-y difference by which to shift the rectangle |
| 348 |
*/ |
| 349 |
static inline void geometry_rectangle_shift ( geometry_rectangle_t *this_, double delta_x, double delta_y ); |
| 350 |
|
| 351 |
/*! |
| 352 |
* \brief expands or shrinks the geometry_rectangle_t |
| 353 |
* |
| 354 |
* The top left corner remains fix. |
| 355 |
* |
| 356 |
* \param this_ pointer to own object attributes |
| 357 |
* \param delta_width value by which to increase the width. In case of a negative value, the rectangle shrinks (but not below 0). |
| 358 |
* \param delta_height value by which to increase the height. In case of a negative value, the rectangle shrinks (but not below 0). |
| 359 |
*/ |
| 360 |
static inline void geometry_rectangle_enlarge ( geometry_rectangle_t *this_, double delta_width, double delta_height ); |
| 361 |
|
| 362 |
/*! |
| 363 |
* \brief expands or shrinks the geometry_rectangle_t to all sides |
| 364 |
* |
| 365 |
* The center remains fix. |
| 366 |
* |
| 367 |
* \param this_ pointer to own object attributes |
| 368 |
* \param delta_width value by which to increase the width twice. |
| 369 |
* In case of a negative value, the rectangle shrinks (but not below 0). |
| 370 |
* The left side moves to left by delta_width. |
| 371 |
* \param delta_height value by which to increase the height twice. |
| 372 |
* In case of a negative value, the rectangle shrinks (but not below 0). |
| 373 |
* The top side moves to top by delta_height. |
| 374 |
*/ |
| 375 |
static inline void geometry_rectangle_expand_4dir ( geometry_rectangle_t *this_, double delta_width, double delta_height ); |
| 376 |
|
| 377 |
/*! |
| 378 |
* \brief embraces a point so that the resulting the rectangle contains the original rectangle and the provided coordinates |
| 379 |
* |
| 380 |
* \param this_ pointer to own object attributes |
| 381 |
* \param x abscissa of the point to be embraced by the rectangle |
| 382 |
* \param y ordinate of the point to be embraced by the rectangle |
| 383 |
*/ |
| 384 |
static inline void geometry_rectangle_embrace ( geometry_rectangle_t *this_, double x, double y ); |
| 385 |
|
| 386 |
/*! |
| 387 |
* \brief sets the left coordinate of geometry_rectangle_t |
| 388 |
* |
| 389 |
* \param this_ pointer to own object attributes |
| 390 |
* \param left new left value |
| 391 |
*/ |
| 392 |
static inline void geometry_rectangle_set_left ( geometry_rectangle_t *this_, double left ); |
| 393 |
|
| 394 |
/*! |
| 395 |
* \brief sets the top coordinate of geometry_rectangle_t |
| 396 |
* |
| 397 |
* \param this_ pointer to own object attributes |
| 398 |
* \param top new top value |
| 399 |
*/ |
| 400 |
static inline void geometry_rectangle_set_top ( geometry_rectangle_t *this_, double top ); |
| 401 |
|
| 402 |
/*! |
| 403 |
* \brief sets the width of geometry_rectangle_t |
| 404 |
* |
| 405 |
* \param this_ pointer to own object attributes |
| 406 |
* \param width new width value |
| 407 |
*/ |
| 408 |
static inline void geometry_rectangle_set_width ( geometry_rectangle_t *this_, double width ); |
| 409 |
|
| 410 |
/*! |
| 411 |
* \brief sets the height of geometry_rectangle_t |
| 412 |
* |
| 413 |
* \param this_ pointer to own object attributes |
| 414 |
* \param height new heigth value |
| 415 |
*/ |
| 416 |
static inline void geometry_rectangle_set_height ( geometry_rectangle_t *this_, double height ); |
| 417 |
|
| 418 |
/*! |
| 419 |
* \brief prints the geometry_rectangle_t struct to the trace output |
| 420 |
* |
| 421 |
* \param this_ pointer to own object attributes |
| 422 |
*/ |
| 423 |
static inline void geometry_rectangle_trace ( const geometry_rectangle_t *this_ ); |
| 424 |
|
| 425 |
#include "geometry/geometry_rectangle.inl" |
| 426 |
|
| 427 |
#endif /* GEOMETRY_RECTANGLE_H */ |
| 428 |
|
| 429 |
|
| 430 |
/* |
| 431 |
Copyright 2016-2026 Andreas Warnke |
| 432 |
|
| 433 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 434 |
you may not use this file except in compliance with the License. |
| 435 |
You may obtain a copy of the License at |
| 436 |
|
| 437 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 438 |
|
| 439 |
Unless required by applicable law or agreed to in writing, software |
| 440 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 441 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 442 |
See the License for the specific language governing permissions and |
| 443 |
limitations under the License. |
| 444 |
*/ |