make.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Boost.Geometry
  2. // Copyright (c) 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_ALGORITHMS_DETAIL_MAKE_MAKE_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAKE_MAKE_HPP
  8. #include <boost/geometry/geometries/infinite_line.hpp>
  9. #include <boost/geometry/core/access.hpp>
  10. namespace boost { namespace geometry
  11. {
  12. #ifndef DOXYGEN_NO_DETAIL
  13. namespace detail { namespace make
  14. {
  15. template <typename Type, typename Coordinate1, typename Coordinate2>
  16. inline
  17. model::infinite_line<Type> make_infinite_line(Coordinate1 const& x1,
  18. Coordinate1 const& y1, Coordinate2 const& x2, Coordinate2 const& y2)
  19. {
  20. model::infinite_line<Type> result;
  21. result.a = y1 - y2;
  22. result.b = x2 - x1;
  23. result.c = -result.a * x1 - result.b * y1;
  24. return result;
  25. }
  26. template <typename Type, typename PointA, typename PointB>
  27. inline
  28. model::infinite_line<Type> make_infinite_line(PointA const& a, PointB const& b)
  29. {
  30. return make_infinite_line<Type>(geometry::get<0>(a), geometry::get<1>(a),
  31. geometry::get<0>(b), geometry::get<1>(b));
  32. }
  33. template <typename Type, typename Segment>
  34. inline
  35. model::infinite_line<Type> make_infinite_line(Segment const& segment)
  36. {
  37. return make_infinite_line<Type>(geometry::get<0, 0>(segment),
  38. geometry::get<0, 1>(segment),
  39. geometry::get<1, 0>(segment),
  40. geometry::get<1, 1>(segment));
  41. }
  42. template <typename Type, typename PointA, typename PointB, typename PointC>
  43. inline
  44. model::infinite_line<Type> make_perpendicular_line(PointA const& a, PointB const& b, PointC const& c)
  45. {
  46. // https://www.math-only-math.com/equation-of-a-line-perpendicular-to-a-line.html
  47. model::infinite_line<Type> const line = make_infinite_line<Type>(a, b);
  48. model::infinite_line<Type> result;
  49. result.a = line.b;
  50. result.b = -line.a;
  51. // Lines with any result.c are perpendicular to a->b
  52. // Calculate this result such that it goes through point c
  53. result.c = -result.a * geometry::get<0>(c) - result.b * geometry::get<1>(c);
  54. return result;
  55. }
  56. }} // namespace detail::make
  57. #endif // DOXYGEN_NO_DETAIL
  58. }} // namespace boost::geometry
  59. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAKE_MAKE_HPP