使用Boost Geometry的线相交 [英] Lines intersection using Boost Geometry
问题描述
如何使用Boost Geometry来表示线?
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?)
据我了解,我可以使用boost::geometry::intersects
,但是我不知道如何定义无限行.
As I understand I can use boost::geometry::intersects
, but I don't know how to define infinite line.
推荐答案
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);
}
line
类型只是使用作为该行一部分的线段来表示一条线,但是作为单独的类型,因此,为了重载,可以通过类型系统将其与线段区分开.
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.
它首先查询B
的两个端点(first
和second
)在A
的哪一侧.然后,如果firstSide
或secondSide
之一为零,则意味着相应的端点正在触摸A
,因此intersects
为true.否则,如果端点在A
的相对侧,则intersects
为真.
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
.
这篇关于使用Boost Geometry的线相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!