代码之家  ›  专栏  ›  技术社区  ›  bullbo

圆和线之间的交点(极坐标)

  •  0
  • bullbo  · 技术社区  · 8 年前

    我想知道是否有一种方法可以找到用极坐标表示的直线和圆之间的交点。

    % 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还比较陌生,所以请耐心听我说。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Wolfie Radu Stefan    8 年前

    我概括了以下内容,以便除 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

        2
  •  0
  •   Florian    8 年前

    你需要用数字计算吗?这个问题有一个简单的解析解:要点 (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 如果判别式为正,则存在两个解(对应于两个交点),否则,不存在任何解。

    如果你算出数学,相交的条件是 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的示例。使用上述等式以闭合形式计算品红色标记。

    Matlab plot for circle_r = 15, th = pi/3