简体   繁体   English

使用 Boost Geometry 的线相交

[英]Lines intersection using Boost Geometry

How line can be represented using Boost Geometry?如何使用 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.据我所知,我可以使用boost::geometry::intersects ,但我不知道如何定义无限线。

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 :如果要测试无限线A是否与线段B相交,可以使用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. line类型简单地使用作为该线一部分的线段来表示线,但作为单独的类型,因此可以通过类型系统将其与线段区分开,以实现重载。

It first queries on which side of A the two endpoints ( first and second ) of B lie.它首先查询其上的侧A两个端点( firstsecond )的B谎言。 Then, if either of firstSide or secondSide are zero, this means the corresponding endpoint is touching A , so intersects is true.然后,如果firstSidesecondSide中的任何一个为零,这意味着相应的端点正在接触A ,因此intersects为真。 Otherwise, intersects is true iff the endpoints are on opposite sides of A .否则,如果端点位于A两侧,则intersects为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM