position_code.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Boost.Geometry
  2. // Copyright (c) 2025 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_ALGORITHMS_DETAIL_POSITION_CODE_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_POSITION_CODE_HPP
  8. #include <boost/geometry/algorithms/detail/direction_code.hpp>
  9. namespace boost { namespace geometry
  10. {
  11. #ifndef DOXYGEN_NO_DETAIL
  12. namespace detail
  13. {
  14. // Position coding of the point with respect to a segment.
  15. // This is a combination of side and direction_code.
  16. // It is counter clockwise from the segment.
  17. // (because polygons are on the right side of a segment, and this way
  18. // we can walk through the ranks ascending.
  19. //
  20. // 3
  21. // |
  22. // 4 * 2 *: p2
  23. // |
  24. // 1
  25. // ^ ^: p1
  26. template <typename Point, typename SideStrategy>
  27. int get_position_code(Point const& p1, Point const& p2, Point const& point, SideStrategy const& side_strategy)
  28. {
  29. using cs_tag = typename SideStrategy::cs_tag;
  30. auto const side = side_strategy.apply(p1, p2, point);
  31. if (side == 1)
  32. {
  33. // left of [p1..p2]
  34. return 4;
  35. }
  36. else if (side == -1)
  37. {
  38. // right of [p1..p2]
  39. return 2;
  40. }
  41. // collinear with [p1..p2]
  42. auto const dir_code = direction_code<cs_tag>(p1, p2, point);
  43. if (dir_code == -1)
  44. {
  45. // collinear, on [p1..p2] or before p1
  46. return 1;
  47. }
  48. else if (dir_code == 1)
  49. {
  50. // collinear with [p1..p2], but farther than p2
  51. return 3;
  52. }
  53. // The segment is degenerate
  54. return 0;
  55. }
  56. } // namespace detail
  57. #endif //DOXYGEN_NO_DETAIL
  58. }} // namespace boost::geometry
  59. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_POSITION_CODE_HPP