Lines intersection using Boost Geometry

邮差的信 提交于 2019-12-05 21:40:17

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!