圆与线的交点(极坐标)
Intersection point between circle and line (Polar coordinates)
我想知道是否有一种方法可以找到用极坐标编写的直线和圆之间的交点。
% Line
x_line = 10 + r * cos(th);
y_line = 10 + r * sin(th);
%Circle
circle_x = circle_r * cos(alpha);
circle_y = circle_r * sin(alpha);
到目前为止,我已经尝试使用 intersect(y_line, circle_y)
函数,但没有成功。我对 MATLAB 比较陌生,所以请多多包涵。
我已经概括了下面的内容,以便可以使用 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
你需要用数字来做吗?这个问题有一个简单的解析解:点 (10 + r*cos(th),10 + r*sin(th))
在半径为 R
的圆上当且仅当
(10+r*cos(th))^2 + (10+r*sin(th))^2 == R^2
<=> 200+r^2 + 2*r*(cos(th)+sin(th)) == R^2
<=> r^2 + 2*r*sqrt(2)*sin(th+pi/4) + 200 - R^2 = 0
这是r
中的一个二次方程。如果判别式为正则有两个解(对应两个交点),否则有none.
如果你算一下,交集的条件是 100*(sin(2*th)-1)+circle_r^2 >= 0
,根是 -10*sqrt(2)*sin(th+pi/4)*[1,1] + sqrt(100*(sin(2*th)-1)+circle_r^2)*[1,-1]
。
这是一个 Matlab 图,作为 th = pi/3 和 circle_r = 15 的示例。洋红色标记在 closed-form 中使用上面显示的等式计算。
我想知道是否有一种方法可以找到用极坐标编写的直线和圆之间的交点。
% Line
x_line = 10 + r * cos(th);
y_line = 10 + r * sin(th);
%Circle
circle_x = circle_r * cos(alpha);
circle_y = circle_r * sin(alpha);
到目前为止,我已经尝试使用 intersect(y_line, circle_y)
函数,但没有成功。我对 MATLAB 比较陌生,所以请多多包涵。
我已经概括了下面的内容,以便可以使用 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
你需要用数字来做吗?这个问题有一个简单的解析解:点 (10 + r*cos(th),10 + r*sin(th))
在半径为 R
的圆上当且仅当
(10+r*cos(th))^2 + (10+r*sin(th))^2 == R^2
<=>
200+r^2 + 2*r*(cos(th)+sin(th)) == R^2
<=>
r^2 + 2*r*sqrt(2)*sin(th+pi/4) + 200 - R^2 = 0
这是r
中的一个二次方程。如果判别式为正则有两个解(对应两个交点),否则有none.
如果你算一下,交集的条件是 100*(sin(2*th)-1)+circle_r^2 >= 0
,根是 -10*sqrt(2)*sin(th+pi/4)*[1,1] + sqrt(100*(sin(2*th)-1)+circle_r^2)*[1,-1]
。
这是一个 Matlab 图,作为 th = pi/3 和 circle_r = 15 的示例。洋红色标记在 closed-form 中使用上面显示的等式计算。