最常见的计算线路交叉C++的方法?

mrg*_*oom 7 c++ geometry boost point line

似乎没有办法计算线路交叉使用boost::geometry,但我想知道在C++中最常用的方法是什么?

我需要2D中两条无限线的交集算法,如果它更快,它可以是两个不同的函数,如:

bool line_intersection(line,line);
point line_intersetion(line,line);
Run Code Online (Sandbox Code Playgroud)

PS我真的试图避免轮子发明,所以倾向于使用一些库.

Hum*_*awi 0

这段代码应该适合你。您也许可以稍微优化一下:

template <class Tpoint>
    Tpoint line<Tpoint>::intersect(const line& other) const{
        Tpoint x = other.first - first;
        Tpoint d1 = second - first;
        Tpoint d2 = other.second - other.first;

        auto cross = d1.x*d2.y - d1.y*d2.x;

        auto t1 = (x.x * d2.y - x.y * d2.x) / static_cast<float>(cross);
        return first + d1 * t1;

    }
Run Code Online (Sandbox Code Playgroud)