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

This is the documentation for a snapshot of the develop branch, built from commit 0da16e0695.
PrevUpHomeNext
touches (two geometries)

Checks if two geometries have at least one touching point (tangent - non overlapping)

Synopsis

template<typename Geometry1, typename Geometry2>
bool touches(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 touch each other

Header

Either

#include <boost/geometry.hpp>

Or

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

Conformance

The function touches implements function Touches from the OGC Simple Feature Specification.

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

[Note] Note

Implemented for Point/Linestring/MultiLinestring/Polygon/MultiPolygon.

Examples

Checks if two geometries have at least one touching point (tangent - non overlapping)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry; 1

int main()
{
    // Checks if the two geometries touch
    bg::model::polygon<bg::model::d2::point_xy<double> > poly1;
    bg::read_wkt("POLYGON((0 0,0 4,4 4,4 0,0 0))", poly1);
    bg::model::polygon<bg::model::d2::point_xy<double> > poly2;
    bg::read_wkt("POLYGON((0 0,0 -4,-4 -4,-4 0,0 0))", poly2);
    bool check_touches = bg::touches(poly1, poly2);
    if (check_touches) {
         std::cout << "Touches: Yes" << std::endl;
    } else {
        std::cout << "Touches: No" << std::endl;
    }

    bg::model::polygon<bg::model::d2::point_xy<double> > poly3;
    bg::read_wkt("POLYGON((1 1,0 -4,-4 -4,-4 0,1 1))", poly3);
    check_touches = bg::touches(poly1, poly3);
    if (check_touches) {
         std::cout << "Touches: Yes" << std::endl;
    } else {
        std::cout << "Touches: No" << std::endl;
    }

    return 0;
}

1

Convenient namespace alias

Output:

Touches: Yes

touches_two_geometries

Touches: No

PrevUpHomeNext