1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // QuickBook Example
3
4 // Copyright (c) 2020 Digvijay Janartha, Hamirpur, India.
5
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9
10 //[crosses
11 //` Checks if two geometries crosses
12
13 #include <iostream>
14
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/point_xy.hpp>
17 #include <boost/geometry/geometries/polygon.hpp>
18
19 namespace bg = boost::geometry; /*< Convenient namespace alias >*/
20
main()21 int main()
22 {
23 // Checks if the two geometries (here, a polygon and a linestring) crosses or not.
24 bg::model::polygon<bg::model::d2::point_xy<double> > poly;
25 bg::read_wkt("POLYGON((0 0,0 3,3 3,3 0,0 0))", poly);
26 bg::model::linestring<bg::model::d2::point_xy<double> > line1;
27 bg::read_wkt("LINESTRING(1 1,2 2,4 4)", line1);
28 bool check_crosses = bg::crosses(poly, line1);
29 if (check_crosses) {
30 std::cout << "Crosses: Yes" << std::endl;
31 } else {
32 std::cout << "Crosses: No" << std::endl;
33 }
34
35 // Edge case: linestring just touches the polygon but doesn't crosses it.
36 bg::model::linestring<bg::model::d2::point_xy<double> > line2;
37 bg::read_wkt("LINESTRING(1 1,1 2,1 3)", line2);
38 check_crosses = bg::crosses(poly, line2);
39 if (check_crosses) {
40 std::cout << "Crosses: Yes" << std::endl;
41 } else {
42 std::cout << "Crosses: No" << std::endl;
43 }
44
45 return 0;
46 }
47
48 //]
49
50
51 //[crosses_output
52 /*`
53 Output:
54 [pre
55 Crosses: Yes
56 Crosses: No
57
58 [$img/algorithms/crosses.png]
59
60 ]
61 */
62 //]
63