如何计算旋转椭圆点的切线?

How to calculate a rorate ellipse point's tangent?

我用 fitEllipse 得到了一个旋转椭圆,我想计算这个椭圆上的点的切线,我试过这个:

static Line getTangent(const RotatedRect & ell, const Point & p)
{
    // double rad = ell.angle*CV_PI/180;
    // double a = ell.size.width/2;
    // double b = ell.size.height/2;
    // if(fabs(rad-CV_PI/2)<CV_PI/4)
    // {
    //     rad -= CV_PI/2;
    //     b = ell.size.width/2;
    //     a = ell.size.height/2;
    // }
    double rad = (ell.angle-90)*CV_PI/180;
    double a = ell.size.height/2;
    double b = ell.size.width/2;
    double cr = cos(rad);
    double sr = sin(rad);
    double s = ell.center.x;
    double t = ell.center.y;
    double k = - b*b / (a*a) * ((p.x-s)*cr+(p.y-t)*sr) / (-(p.x-s)*sr+(p.y-t)*cr);
    return Line(k,p); // The line through p with grad k
}

椭圆{center:(523.965, 525.291), size:{444.735 x 662.827}, angle:81.7087} 点 p(313, 713) 结果是正确的,但是椭圆 {center:( 638.93, 639.36), size:{572.964 x 787.908}, angle:6.27164} with point p(756, 985) 结果是错误的,我猜是角度计算问题,但我不知道如何解决.有人可以帮助我吗?

一图胜千言:

您需要检查点是否位于椭圆之外(如果存在则使用 opencv 函数或使用椭圆方程)