用特征计算两条线的交点

calculating the intersection point of two lines with eigen

今天我在 eigen 中做了第一步,找到了以下解决方案来获取交点:

#include <Eigen/Dense>
using namespace Eigen;
using namespace std;

int main() {
    // Calc intersection of line ac with bd:
    Vector2f a(8,2);
    Vector2f b(9,5);
    Vector2f c(6,6);
    Vector2f d(5,9);

    Matrix2f xx; 
    xx << c-a, b-d; 

    cout << "Here is the matrix xx:\n" << xx << endl;
    Vector2f x = xx.colPivHouseholderQr().solve(b-a);
    Vector2f intersect1( a + x(0)* ( c-a ) );
    Vector2f intersect2( b + x(1)* ( d-b ) );

    cout << "intersect1\n" << intersect1 << std::endl;
    cout << "intersect2\n" << intersect2 << std::endl;
}

问:eigen中有直接给出相交结果的函数吗? 我相信我在这里做了很多手工代码。

二维中的线与二维中的Hyperplane相同。对于这种情况,有一个 intersection 方法:

#include <Eigen/Geometry>
#include <iostream>
int main() {
    using Line2 = Eigen::Hyperplane<float,2>;
    using Vec2  = Eigen::Vector2f;
    Vec2 a(8,2);
    Vec2 b(9,5);
    Vec2 c(6,6);
    Vec2 d(5,9);

    Line2 ac = Line2::Through(a,c);
    Line2 bd = Line2::Through(b,d);

    std::cout << "Intersection:\n" << ac.intersection(bd) << '\n';
}

结果为 [4, 10],您的代码也是如此。