简体   繁体   中英

Intersection point between circle and line (Polar coordinates)

I'm wondering if there is a way of finding the intersection point between a line and a circle written in 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);

So far I've tried using the intersect(y_line, circle_y) function without any success. I'm relatively new to MATLAB so bear with me.

I have generalised the below so that other values than a=10 can be used...

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) ];

We want the intersection, so xl = xc , yl = yc

% a + rl * cos(th) = rc * cos(alpha)
% a + rl * sin(th) = rc * sin(alpha)

Square both sides of both equations and sum them. Simplifying sin(a)^2 + cos(a)^2 = 1 . Expanding brackets and simplifying further gives

% rl^2 + 2 * a * rl * (cos(th) + sin(th)) + 2 * a - rc^2 = 0

Now you can use the quadratic formula to get the value of rl .

Test discriminant:

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 );

Now you have 0, 1 or 2 points of intersection of the line with the circle, from certain known and unknown values about each line. Essentially this is just simultaneous equations, see the start of this article for a basis of the maths https://en.wikipedia.org/wiki/System_of_linear_equations

Do you need to do it numerically? This problem would have an easy analytical solution: The point (10 + r*cos(th),10 + r*sin(th)) is on a circle with radius R iff

(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

which is a quadratic equation in r . If the discriminant is positive, there are two solutions (corresponding to two intersection points), otherwise, there are none.

If you work out the math, the condition for intersection is 100*(sin(2*th)-1)+circle_r^2 >= 0 and the roots are -10*sqrt(2)*sin(th+pi/4)*[1,1] + sqrt(100*(sin(2*th)-1)+circle_r^2)*[1,-1] .

Here is a Matlab plot as an example for th = pi/3 and circle_r = 15. The magenta markers are calculated in closed-form using the equation shown above.

Matlab图,circle_r = 15,th = pi / 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM