agg_bounding_rect.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // Permission to copy, use, modify, sell and distribute this software
  6. // is granted provided this copyright notice appears in all copies.
  7. // This software is provided "as is" without express or implied
  8. // warranty, and with no claim as to its suitability for any purpose.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. //
  16. // bounding_rect function template
  17. //
  18. //----------------------------------------------------------------------------
  19. #ifndef AGG_BOUNDING_RECT_INCLUDED
  20. #define AGG_BOUNDING_RECT_INCLUDED
  21. #include "agg_basics.h"
  22. namespace agg
  23. {
  24. //-----------------------------------------------------------bounding_rect
  25. template<class VertexSource, class GetId, class CoordT>
  26. bool bounding_rect(VertexSource& vs, GetId& gi,
  27. unsigned start, unsigned num,
  28. CoordT* x1, CoordT* y1, CoordT* x2, CoordT* y2)
  29. {
  30. unsigned i;
  31. double x;
  32. double y;
  33. bool first = true;
  34. *x1 = CoordT(1);
  35. *y1 = CoordT(1);
  36. *x2 = CoordT(0);
  37. *y2 = CoordT(0);
  38. for(i = 0; i < num; i++)
  39. {
  40. vs.rewind(gi[start + i]);
  41. unsigned cmd;
  42. while(!is_stop(cmd = vs.vertex(&x, &y)))
  43. {
  44. if(is_vertex(cmd))
  45. {
  46. if(first)
  47. {
  48. *x1 = CoordT(x);
  49. *y1 = CoordT(y);
  50. *x2 = CoordT(x);
  51. *y2 = CoordT(y);
  52. first = false;
  53. }
  54. else
  55. {
  56. if(CoordT(x) < *x1) *x1 = CoordT(x);
  57. if(CoordT(y) < *y1) *y1 = CoordT(y);
  58. if(CoordT(x) > *x2) *x2 = CoordT(x);
  59. if(CoordT(y) > *y2) *y2 = CoordT(y);
  60. }
  61. }
  62. }
  63. }
  64. return *x1 <= *x2 && *y1 <= *y2;
  65. }
  66. //-----------------------------------------------------bounding_rect_single
  67. template<class VertexSource, class CoordT>
  68. bool bounding_rect_single(VertexSource& vs, unsigned path_id,
  69. CoordT* x1, CoordT* y1, CoordT* x2, CoordT* y2)
  70. {
  71. double x;
  72. double y;
  73. bool first = true;
  74. *x1 = CoordT(1);
  75. *y1 = CoordT(1);
  76. *x2 = CoordT(0);
  77. *y2 = CoordT(0);
  78. vs.rewind(path_id);
  79. unsigned cmd;
  80. while(!is_stop(cmd = vs.vertex(&x, &y)))
  81. {
  82. if(is_vertex(cmd))
  83. {
  84. if(first)
  85. {
  86. *x1 = CoordT(x);
  87. *y1 = CoordT(y);
  88. *x2 = CoordT(x);
  89. *y2 = CoordT(y);
  90. first = false;
  91. }
  92. else
  93. {
  94. if(CoordT(x) < *x1) *x1 = CoordT(x);
  95. if(CoordT(y) < *y1) *y1 = CoordT(y);
  96. if(CoordT(x) > *x2) *x2 = CoordT(x);
  97. if(CoordT(y) > *y2) *y2 = CoordT(y);
  98. }
  99. }
  100. }
  101. return *x1 <= *x2 && *y1 <= *y2;
  102. }
  103. }
  104. #endif