找到线圆交点的距离

find distance in line-circle intersection

一条线与一个圆相交,圆心在 C (cx,cy),半径 r。该线由位置 P (px, py) 和方向 D (dx, dy) 描述。 P 位于圆内,直线无限长。从 P 到交点的距离 d 是多少?

float Intersect(Vector2d C, float r, Vector2d P, Vector2d D){

    ...

    return d;
}

您可以展开括号并求解未知的二次方程 t:

((px - cx) + t * dx)^2 + ((py - cy) + t * dy)^2 = r^2

如果 (dx,dy) 向量被归一化(单位长度),则 t 的值(正根,如果你的线确实是从圆内开始的射线)。

交点公式(当前语句不需要)

ix = px + t * dx
iy = py + t * dy