我已经概括了以下内容,以便可以使用除 a=10 之外的其他值...
a = 10; % constant line offset
th = 0; % constant angle of line
% rl = ?? - variable to find
% Coordinates of line:
% [xl, yl] = [ a + rl * cos(th), a + rl * sin(th) ];
rc = 1; % constant radius of circle
% alpha = ?? - variable to find
% Coordinates of circle:
% [xc, yc] = [ rc * cos(alpha), rc * sin(alpha) ];
我们想要交叉点,所以xl = xc,yl = yc
% a + rl * cos(th) = rc * cos(alpha)
% a + rl * sin(th) = rc * sin(alpha)
对两个方程两边求平方,然后求和。简化sin(a)^2 + cos(a)^2 = 1。扩大括号并进一步简化给出
% rl^2 + 2 * a * rl * (cos(th) + sin(th)) + 2 * a - rc^2 = 0
现在您可以使用二次公式得到rl 的值。
测试判别式:
dsc = (2 * a * (cos(th) + sin(th)) )^2 - 4 * (2 * a - rc^2);
rl = [];
if dsc < 0
% no intersection
elseif dsc == 0
% one intersection at
rl = - cos(th) - sin(th);
else
% two intersection points
rl = -cos(th) - sin(th) + [ sqrt(dsc)/2, -sqrt(dsc)/2];
end
% Get alpha from an earlier equation
alpha = acos( ( a + rl .* cos(th) ) ./ rc );
现在,根据每条线的某些已知和未知值,您有 0、1 或 2 个线与圆的交点。本质上这只是联立方程,请参阅本文开头了解数学基础
https://en.wikipedia.org/wiki/System_of_linear_equations