直线相交的2D求解过程

文章展示了将2D平面坐标转换为3D并利用正弦定理求解两个线段交点的代码实例,包括向量运算和几何关系的应用。
摘要由CSDN通过智能技术生成

对于一个2D的平面的求解示意图和代码过程

方案1:

PSGMPoint point2dto3d(const PSGMPoint2D& pt)
{
    return PSGMPoint(pt.x(), pt.y(), 0.);
}
void func()
{
    PSGMPoint2D ptBeg1(4, 1);
    PSGMPoint2D ptEnd1(2, 4);
    PSGMPoint2D ptBeg2(2, 1);
    PSGMPoint2D ptEnd2(4, 4);
    PSGMDirection2D dir2d1 = (ptEnd1 - ptBeg1).normalized();
    PSGMDirection dir1 = (point2dto3d(ptEnd1) - point2dto3d(ptBeg1)).normalized();
    double dir1Len = dir1.norm();
    PSGMDirection dir2 = (point2dto3d(ptEnd2) - point2dto3d(ptBeg2)).normalized();
    double dir2Len = dir2.norm();

    const PSGMVector chord = point2dto3d(ptBeg2) - point2dto3d(ptBeg1);
    double chordLen = chord.norm();

    const PSGMVector n1 = dir1.cross(dir2);
    double n1Len = n1.norm();
    const double nLen = n1.squaredNorm();
    const PSGMVector n2 = dir2.cross(n1);
    double n2Len = n2.norm();
    auto n3 = n2 / nLen;
    double n3Len = n3.norm();

    // PSGM_ASSERT(std::fabs(nLen) > tol); // parallel lines

    // intersection of lines
    const double intersectParam = chord.dot(n3);
    PSGMPoint2D ptInt = ptBeg1 + dir2d1 * intersectParam;
}

方案2:

// 正弦定理:在任意一个平面三角形中,各边和它所对角的正弦值的比相等且等于外接圆的直径,即a/sinA = b/sinB =c/sinC = 2r=D(r为外接圆半径,D为直径)
void func1()
{
    PSGMPoint2D ptBeg1(4, 1);
    PSGMPoint2D ptEnd1(2, 4);
    PSGMPoint2D ptBeg2(2, 1);
    PSGMPoint2D ptEnd2(4, 4);
    PSGMDirection2D dir2d1 = (ptEnd1 - ptBeg1).normalized();
    PSGMDirection dir1 = (point2dto3d(ptEnd1) - point2dto3d(ptBeg1)).normalized();
    PSGMDirection dir2 = (point2dto3d(ptEnd2) - point2dto3d(ptBeg2)).normalized();
    const PSGMVector chord = point2dto3d(ptBeg1) - point2dto3d(ptBeg2);
    const PSGMVector n1 = dir1.cross(dir2);
    const PSGMVector n2 = dir2.cross(chord.normalized());

    double intersectParam = sqrt(chord.squaredNorm() * n2.squaredNorm() / n1.squaredNorm());

    // intersection of lines
    PSGMPoint2D ptInt = ptBeg1 + dir2d1 * intersectParam;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值