代码之家  ›  专栏  ›  技术社区  ›  Klaus

用特征值计算两条直线的交点

  •  1
  • Klaus  · 技术社区  · 7 年前

    今天我在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;
    }
    

    问:本征函数中有没有一个函数直接给出相交结果? 我相信我在这里做了很多手工代码。

    1 回复  |  直到 7 年前
        1
  •  4
  •   chtz    7 年前

    二维直线与 Hyperplane 在两个维度上。在这种情况下 intersection 方法:

    #include <Eigen/Geometry>
    #include <iostream>
    int main() {
        typedef Eigen::Hyperplane<float,2> Line2;
        typedef Eigen::Vector2f Vec2;
        Vec2 a(8,2), b(9,5), c(6,6), d(5,9);
    
        Line2 ac = Line2::Through(a,c), bd=Line2::Through(b,d);
    
        std::cout << "Intersection:\n" << ac.intersection(bd) << '\n';
    }
    

    结果在 [4, 10] ,你的代码也是。