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

PrevUpHomeNext

model::segment

Class segment: small class containing two points.

Description

From Wikipedia: In geometry, a line segment is a part of a line that is bounded by two distinct end points, and contains every point on the line between its end points.

Model of

Segment Concept

Synopsis

template<typename Point>
class model::segment
{
  // ...
};

Template parameter(s)

Parameter

Description

typename Point

Constructor(s)

Function

Description

Parameters

segment()

Default constructor, no initialization.

segment(Point const & p1, Point const & p2)

Constructor taking the first and the second point.

Point const &: p1:

Point const &: p2:

Header

Either

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

Or

#include <boost/geometry/geometries/segment.hpp>

Examples

Declaration and use of the Boost.Geometry model::segment, modelling the Segment Concept

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>

namespace bg = boost::geometry;

int main()
{
    typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
    typedef bg::model::segment<point_t> segment_t;

    segment_t seg1; 1
    segment_t seg2(point_t(0.0, 0.0), point_t(5.0, 5.0)); 2

#ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX

    segment_t seg3{{0.0, 0.0}, {5.0, 5.0}}; 3

#endif

    bg::set<0, 0>(seg1, 1.0); 4
    bg::set<0, 1>(seg1, 2.0);
    bg::set<1, 0>(seg1, 3.0);
    bg::set<1, 1>(seg1, 4.0);

    double x0 = bg::get<0, 0>(seg1); 5
    double y0 = bg::get<0, 1>(seg1);
    double x1 = bg::get<1, 0>(seg1);
    double y1 = bg::get<1, 1>(seg1);

    std::cout << x0 << ", " << y0 << ", " << x1 << ", " << y1 << std::endl;

    return 0;
}

1

Default-construct a segment.

2

Construct, assigning the first and the second point.

3

Construct, using C++11 unified initialization syntax.

4

Set a coordinate.

5

Get a coordinate.

Output:

1, 2, 3, 4

PrevUpHomeNext