gsrect.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gsrect.h,v 1.5 2002/06/16 08:45:42 lpd Exp $ */
  14. /* Rectangle utilities */
  15. #ifndef gsrect_INCLUDED
  16. # define gsrect_INCLUDED
  17. #include "gxfixed.h"
  18. /* Check whether one rectangle is included entirely within another. */
  19. #define rect_within(inner, outer)\
  20. ((inner).q.y <= (outer).q.y && (inner).q.x <= (outer).q.x &&\
  21. (inner).p.y >= (outer).p.y && (inner).p.x >= (outer).p.x)
  22. /*
  23. * Intersect two rectangles, replacing the first. The result may be
  24. * anomalous (q < p) if the intersection is empty.
  25. */
  26. #define rect_intersect(to, from)\
  27. BEGIN\
  28. if ((from).p.x > (to).p.x) (to).p.x = (from).p.x;\
  29. if ((from).q.x < (to).q.x) (to).q.x = (from).q.x;\
  30. if ((from).p.y > (to).p.y) (to).p.y = (from).p.y;\
  31. if ((from).q.y < (to).q.y) (to).q.y = (from).q.y;\
  32. END
  33. /*
  34. * Merge two rectangles, replacing the first. The result may be
  35. * anomalous (q < p) if the first rectangle was anomalous.
  36. */
  37. #define rect_merge(to, from)\
  38. BEGIN\
  39. if ((from).p.x < (to).p.x) (to).p.x = (from).p.x;\
  40. if ((from).q.x > (to).q.x) (to).q.x = (from).q.x;\
  41. if ((from).p.y < (to).p.y) (to).p.y = (from).p.y;\
  42. if ((from).q.y > (to).q.y) (to).q.y = (from).q.y;\
  43. END
  44. /*
  45. * Calculate the difference of two rectangles, a list of up to 4 rectangles.
  46. * Return the number of rectangles in the list, and set the first rectangle
  47. * to the intersection. The resulting first rectangle is guaranteed not to
  48. * be anomalous (q < p) iff it was not anomalous originally.
  49. *
  50. * Note that unlike the macros above, we need different versions of this
  51. * depending on the data type of the individual values: we'll only implement
  52. * the variations that we need.
  53. */
  54. int int_rect_difference(gs_int_rect * outer, const gs_int_rect * inner,
  55. gs_int_rect * diffs /*[4] */ );
  56. /*
  57. * Check whether a parallelogram is a rectangle.
  58. */
  59. #define PARALLELOGRAM_IS_RECT(ax, ay, bx, by)\
  60. ( ((ax) | (by)) == 0 || ((bx) | (ay)) == 0 )
  61. /*
  62. * Convert a rectangular parallelogram to a rectangle, carefully following
  63. * the center-of-pixel rule in all cases.
  64. */
  65. #define INT_RECT_FROM_PARALLELOGRAM(prect, px, py, ax, ay, bx, by)\
  66. BEGIN\
  67. int px_ = fixed2int_pixround(px);\
  68. int py_ = fixed2int_pixround(py);\
  69. int qx_ = fixed2int_pixround((px) + (ax) + (bx)); /* only one is non-zero */\
  70. int qy_ = fixed2int_pixround((py) + (ay) + (by)); /* ditto */\
  71. \
  72. if (qx_ < px_)\
  73. (prect)->p.x = qx_, (prect)->q.x = px_;\
  74. else\
  75. (prect)->p.x = px_, (prect)->q.x = qx_;\
  76. if (qy_ < py_)\
  77. (prect)->p.y = qy_, (prect)->q.y = py_;\
  78. else\
  79. (prect)->p.y = py_, (prect)->q.y = qy_;\
  80. END
  81. #endif /* gsrect_INCLUDED */