Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext
intersects (two geometries)

Checks if two geometries have at least one intersection.

Synopsis

template<typename Geometry1, typename Geometry2>
bool intersects(Geometry1 const & geometry1, Geometry2 const & geometry2)

Parameters

Type

Concept

Name

Description

Geometry1 const &

Any type fulfilling a Geometry Concept

geometry1

A model of the specified concept

Geometry2 const &

Any type fulfilling a Geometry Concept

geometry2

A model of the specified concept

Returns

Returns true if two geometries intersect each other

Header

Either

#include <boost/geometry/geometry.hpp>

Or

#include <boost/geometry/algorithms/intersects.hpp>

Conformance

The function intersects implements function Intersects from the OGC Simple Feature Specification.

The version with one parameter is additional and not described in the OGC standard

Examples

Check if two linestrings intersect each other

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

int main()
{
    // Calculate the intersects of a cartesian polygon
    typedef boost::geometry::model::d2::point_xy<double> P;
    boost::geometry::model::linestring<P> line1, line2;

    boost::geometry::read_wkt("linestring(1 1,2 2,3 3)", line1);
    boost::geometry::read_wkt("linestring(2 1,1 2,4 0)", line2);

    bool b = boost::geometry::intersects(line1, line2);

    std::cout << "Intersects: " << (b ? "YES" : "NO") << std::endl;

    return 0;
}

Output:

Intersects: YES

PrevUpHomeNext