3

How line can be represented using Boost Geometry?

I don't need finite segment, but I need infinite lines (maybe Segment or Linestring can be extended?)

As I understand I can use boost::geometry::intersects, but I don't know how to define infinite line.

mrgloom
  • 20,061
  • 36
  • 171
  • 301

1 Answers1

0

If you want to test whether an infinite line A intersects a line segment B, this can be done using boost::geometry::strategy::side::side_by_triangle:

template <typename Point>
struct line
{
    boost::geometry::model::segment<Point> segment;
};

template <typename Point>
bool intersects(line<Point> const& A, boost::geometry::model::segment<Point> const& B)
{
    using side = boost::geometry::strategy::side::side_by_triangle<>;
    auto const firstSide  = side::apply(A.segment.first, A.segment.second, B.first);
    auto const secondSide = side::apply(A.segment.first, A.segment.second, B.second);
    return firstSide == 0 || secondSide == 0 || (firstSide < 0) != (secondSide < 0);
}

The line type simply represents a line using a segment that is part of that line, but as a separate type so it can be distinguished from a segment by the type system, for the purposes of overloading.

It first queries on which side of A the two endpoints (first and second) of B lie. Then, if either of firstSide or secondSide are zero, this means the corresponding endpoint is touching A, so intersects is true. Otherwise, intersects is true iff the endpoints are on opposite sides of A.

Oktalist
  • 14,336
  • 3
  • 43
  • 63