今天我在 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;
}
Run Code Online (Sandbox Code Playgroud)
问: eigen 中是否有直接给出相交结果的函数?我相信我在这里做了很多手工代码。
二维中的线与二维中的超平面相同。对于这种情况,有一种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';
}
Run Code Online (Sandbox Code Playgroud)
结果[4, 10],就像您的代码一样。