infinite_line_functions.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Boost.Geometry
  2. // Copyright (c) 2018-2019 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_ARITHMETIC_LINE_FUNCTIONS_HPP
  7. #define BOOST_GEOMETRY_ARITHMETIC_LINE_FUNCTIONS_HPP
  8. #include <boost/geometry/core/access.hpp>
  9. #include <boost/geometry/core/config.hpp>
  10. #include <boost/geometry/geometries/infinite_line.hpp>
  11. #include <boost/geometry/util/math.hpp>
  12. #include <boost/geometry/util/select_most_precise.hpp>
  13. namespace boost { namespace geometry
  14. {
  15. namespace arithmetic
  16. {
  17. // Calculates intersection point of two infinite lines.
  18. // Returns true if the lines intersect.
  19. // Returns false if lines are parallel (or collinear, possibly opposite)
  20. template <typename Point, typename Type>
  21. inline bool intersection_point(model::infinite_line<Type> const& p,
  22. model::infinite_line<Type> const& q, Point& ip)
  23. {
  24. Type const denominator = p.b * q.a - p.a * q.b;
  25. static Type const zero = 0;
  26. if (math::equals(denominator, zero))
  27. {
  28. // Lines are parallel
  29. return false;
  30. }
  31. // Calculate the intersection coordinates
  32. geometry::set<0>(ip, (p.c * q.b - p.b * q.c) / denominator);
  33. geometry::set<1>(ip, (p.a * q.c - p.c * q.a) / denominator);
  34. return true;
  35. }
  36. //! Return a distance-side-measure for a point to a line
  37. //! Point is located left of the line if value is positive,
  38. //! right of the line is value is negative, and on the line if the value
  39. //! is exactly zero
  40. template <typename Type, typename CoordinateType>
  41. inline
  42. typename select_most_precise<Type, CoordinateType>::type
  43. side_value(model::infinite_line<Type> const& line,
  44. CoordinateType const& x, CoordinateType const& y)
  45. {
  46. // https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_an_equation
  47. // Distance from point to line in general form is given as:
  48. // (a * x + b * y + c) / sqrt(a * a + b * b);
  49. // In most use cases comparisons are enough, saving the sqrt
  50. // and often even the division.
  51. // Also, this gives positive values for points left to the line,
  52. // and negative values for points right to the line.
  53. return line.a * x + line.b * y + line.c;
  54. }
  55. template <typename Type, typename Point>
  56. inline
  57. typename select_most_precise
  58. <
  59. Type,
  60. typename geometry::coordinate_type<Point>::type
  61. >::type
  62. side_value(model::infinite_line<Type> const& line, Point const& p)
  63. {
  64. return side_value(line, geometry::get<0>(p), geometry::get<1>(p));
  65. }
  66. // Returns true for two lines which are supposed to be (close to) collinear
  67. // (which is not checked) and have a similar direction
  68. // (in practice up to 45 degrees, TO BE VERIFIED)
  69. // true: -----------------> p -----------------> q
  70. // false: -----------------> p <----------------- q
  71. template <typename Type>
  72. inline
  73. bool similar_direction(const model::infinite_line<Type>& p,
  74. const model::infinite_line<Type>& q)
  75. {
  76. return p.a * q.a >= 0 && p.b * q.b >= 0;
  77. }
  78. template <typename Type>
  79. inline bool is_degenerate(const model::infinite_line<Type>& line)
  80. {
  81. static Type const zero = 0;
  82. return math::equals(line.a, zero) && math::equals(line.b, zero);
  83. }
  84. } // namespace arithmetic
  85. }} // namespace boost::geometry
  86. #endif // BOOST_GEOMETRY_ARITHMETIC_LINE_FUNCTIONS_HPP