star_comb.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_STAR_COMB_HPP
  7. #define BOOST_GEOMETRY_STAR_COMB_HPP
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11. #include <boost/timer.hpp>
  12. #include <boost/tuple/tuple.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <test_overlay_p_q.hpp>
  15. template <typename Polygon, typename AddFunctor>
  16. inline void make_star(Polygon& polygon, AddFunctor functor,
  17. int count, double factor1, double factor2,
  18. double offset = 0.0,
  19. bool close = true,
  20. double orientation = 1.0)
  21. {
  22. // Create star
  23. double cx = 25.0;
  24. double cy = 25.0;
  25. double dx = 50.0;
  26. double dy = 50.0;
  27. double a1 = factor1 * 0.5 * dx;
  28. double b1 = factor1 * 0.5 * dy;
  29. double a2 = factor2 * 0.5 * dx;
  30. double b2 = factor2 * 0.5 * dy;
  31. double delta = orientation * boost::math::constants::pi<double>() * 2.0 / (count - 1);
  32. double angle = offset * delta;
  33. double x0, y0;
  34. bool first = true;
  35. for (int i = 0; i < count - 1; i++, angle += delta)
  36. {
  37. bool even = i % 2 == 0;
  38. double x = cx + (even ? a1 : a2) * sin(angle);
  39. double y = cy + (even ? b1 : b2) * cos(angle);
  40. functor(polygon, x, y, i);
  41. if (first)
  42. {
  43. x0 = x;
  44. y0 = y;
  45. first = false;
  46. }
  47. }
  48. if (close)
  49. {
  50. functor(polygon, x0, y0, count);
  51. }
  52. }
  53. template <typename Vector>
  54. void ccw_pushback(Vector& vector, double x, double y, int)
  55. {
  56. vector.push_back(boost::make_tuple(x, y));
  57. }
  58. template <typename Polygon, typename AddFunctor>
  59. inline void make_comb(Polygon& polygon, AddFunctor functor,
  60. int count,
  61. bool close = true,
  62. bool clockwise = true)
  63. {
  64. int n = 0;
  65. if (! clockwise)
  66. {
  67. typedef boost::tuple<double, double> tup;
  68. typedef std::vector<tup> vector_type;
  69. vector_type vec;
  70. // Create in clockwise order
  71. make_comb(vec, ccw_pushback<vector_type>, count, close, true);
  72. // Add in reverse
  73. // (For GCC 3.4 have it const)
  74. vector_type const& v = vec;
  75. for (vector_type::const_reverse_iterator
  76. it = v.rbegin(); it != v.rend(); ++it)
  77. {
  78. functor(polygon, it->get<0>(), it->get<1>(), n++);
  79. }
  80. return;
  81. }
  82. // Create comb
  83. functor(polygon, 25.0, 0.0, n++);
  84. functor(polygon, 0.0, 25.0, n++);
  85. functor(polygon, 25.0, 50.0, n++);
  86. // Function parameters
  87. double diff = (25.0 / (count - 0.5)) / 2.0;
  88. double b1 = -25.0;
  89. double b2 = 25.0 - diff * 2.0;
  90. double x1 = 50.0, x2 = 25.0;
  91. for (int i = 0; i < count - 1; i++)
  92. {
  93. functor(polygon, x1, (x1 + b1), n++); x1 -= diff;
  94. functor(polygon, x1, (x1 + b1), n++); x1 -= diff;
  95. functor(polygon, x2, (x2 + b2), n++); x2 -= diff;
  96. functor(polygon, x2, (x2 + b2), n++); x2 -= diff;
  97. }
  98. functor(polygon, x1, (x1 + b1), n++);
  99. if (close)
  100. {
  101. functor(polygon, 25.0, 0.0, 4);
  102. }
  103. }
  104. #endif // BOOST_GEOMETRY_STAR_COMB_HPP