使用Boost Geometry的线相交 [英] Lines intersection using Boost Geometry

查看:643
本文介绍了使用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.

推荐答案

如果要测试无限线A是否与线段B相交,可以使用

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的两个端点(firstsecond)在A的哪一侧.然后,如果firstSidesecondSide之一为零,则意味着相应的端点正在触摸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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆