1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2015 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2015.
6 // Modifications copyright (c) 2015 Oracle and/or its affiliates.
8 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DIRECITON_CODE_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DIRECITON_CODE_HPP
18 #include <boost/geometry/core/access.hpp>
19 #include <boost/geometry/util/math.hpp>
21 namespace boost { namespace geometry
24 #ifndef DOXYGEN_NO_DETAIL
28 template <std::size_t Index, typename Point1, typename Point2>
29 inline int sign_of_difference(Point1 const& point1, Point2 const& point2)
32 math::equals(geometry::get<Index>(point1), geometry::get<Index>(point2))
36 (geometry::get<Index>(point1) > geometry::get<Index>(point2) ? 1 : -1);
40 // Gives sense of direction for point p, collinear w.r.t. segment (a,b)
41 // Returns -1 if p goes backward w.r.t (a,b), so goes from b in direction of a
42 // Returns 1 if p goes forward, so extends (a,b)
43 // Returns 0 if p is equal with b, or if (a,b) is degenerate
44 // Note that it does not do any collinearity test, that should be done before
45 template <typename Point1, typename Point2>
46 inline int direction_code(Point1 const& segment_a, Point1 const& segment_b,
49 // Suppose segment = (4 3,4 4) and p =(4 2)
50 // Then sign_a1 = 1 and sign_p1 = 1 -> goes backward -> return -1
52 int const sign_a0 = sign_of_difference<0>(segment_b, segment_a);
53 int const sign_a1 = sign_of_difference<1>(segment_b, segment_a);
55 if (sign_a0 == 0 && sign_a1 == 0)
60 int const sign_p0 = sign_of_difference<0>(segment_b, p);
61 int const sign_p1 = sign_of_difference<1>(segment_b, p);
63 if (sign_p0 == 0 && sign_p1 == 0)
68 return sign_a0 == sign_p0 && sign_a1 == sign_p1 ? -1 : 1;
73 #endif //DOXYGEN_NO_DETAIL
77 }} // namespace boost::geometry
79 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DIRECITON_CODE_HPP