Lines intersection using Boost Geometry

拜拜、爱过 提交于 2019-12-22 10:23:34

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/34133160/lines-intersection-using-boost-geometry

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