Find intersection between line and circle

Hi I currently work with a small project regarding particle behaviour in closed domains.
I want to create a function which takes start point, and start direction for a point. The domain is a circle. How do I calculate the two intersections with the circle?
I know how to do the calculations but I have trouble with the implementation.
px = -0.5; % start x-coordinate of point
py = -0.5; % start y-coordinate of point
rx = 1; % x-direction of vector with the above startcoordinates
ry = 2; % y ------ || ---------
I tried using symbolic toolbox but I can't convert it back to normal variables.
fsolve won't help because the representation of a circle is not a function. What to do?
Thanks in advance
Regards

1 件のコメント

Andrew Newell
Andrew Newell 2011 年 4 月 27 日
Show me what you did using the symbolic toolbox and I'll show you how to convert it back to normal variables.

サインインしてコメントする。

 採用された回答

Teja Muppirala
Teja Muppirala 2011 年 4 月 27 日

2 投票

You're pretty close. Except you don't want to solve
'x^2 + y^2 = 0'
You want to solve 'x^2 + y^2 = R^2'
-------------------------------
Try this:
R = sym('R');
x = sym('px + t*rx');
y = sym('py + t*ry');
c = x^2+y^2-R^2;
t_sol = solve(c);
x_sol = subs(x,'t',t_sol)
y_sol = subs(y,'t',t_sol)
Then x_sol gives you the x values, and y_sol has the y values of the solution.

2 件のコメント

Andrew Newell
Andrew Newell 2011 年 4 月 27 日
This is how it would look in a function. Note that once you define t as a symbol, combinations involving t are symbols too.
function [xi,yi] = solveIntersection(px,py,rx,ry,r)
syms t
x = px + t*rx;
y = py + t*ry;
c = x^2+y^2-r^2;
t_sol = solve(c);
xi = subs(x,'t',t_sol);
yi = subs(y,'t',t_sol);
Kasper
Kasper 2011 年 4 月 28 日
Sweet. Thanks!

サインインしてコメントする。

その他の回答 (4 件)

Kasper
Kasper 2011 年 4 月 27 日

0 投票

I insert the parametrisation in the circle equation. And solve it.
x = sym('px + t*rx'); y = sym('py + t*ry');
c = x^2+y^2; solve(c)
ans =
-(px*rx + py*ry - i*px*ry + i*py*rx)/(rx^2 + ry^2)
-(px*rx + py*ry + i*px*ry - i*py*rx)/(rx^2 + ry^2)
I need this to respond to the numerical values which were assigned to it in the beginning. But I see there's an imaginary part, which I do not want.
Kasper
Kasper 2011 年 4 月 27 日

0 投票

Thanks but how do I substitute the real values into a 'sym' variable? Since I define all variables as 'sym' the predefined rx = 1,... doesn't really work with those final equations.

3 件のコメント

Teja Muppirala
Teja Muppirala 2011 年 4 月 27 日
Option 1: Use the subs command to put in the values of each variable.
subs([x_sol y_sol],{'R' 'rx', 'ry', 'px', 'py'},{1 8 6 0 0})
Or Option 2:
Copy the text of the symbolic expression and then use that just like a regular MATLAB expression where everything is a number.
R = 1;
px = 0;
py = 0;
rx = 8;
ry = 6;
x_sol = [px - (rx*(px*rx + py*ry + (R^2*rx^2 + R^2*ry^2 - px^2*ry^2 + 2*px*py*rx*ry - py^2*rx^2)^(1/2)))/(rx^2 + ry^2); px - (rx*(px*rx + py*ry - (R^2*rx^2 + R^2*ry^2 - px^2*ry^2 + 2*px*py*rx*ry - py^2*rx^2)^(1/2)))/(rx^2 + ry^2)]
y_sol = [px - (rx*(px*rx + py*ry - (R^2*rx^2 + R^2*ry^2 - px^2*ry^2 + 2*px*py*rx*ry - py^2*rx^2)^(1/2)))/(rx^2 + ry^2);
py - (ry*(px*rx + py*ry - (R^2*rx^2 + R^2*ry^2 - px^2*ry^2 + 2*px*py*rx*ry - py^2*rx^2)^(1/2)))/(rx^2 + ry^2)]
Teja Muppirala
Teja Muppirala 2011 年 4 月 27 日
Oops, I miscopied y_sol, but I think you get the idea.
Christopher Creutzig
Christopher Creutzig 2011 年 4 月 28 日
Instead of copying the symbolic expression manually, you might want to try using matlabFunction(x_sol(1)) etc. At the very least, it will vectorize better/more correctly.

サインインしてコメントする。

Kasper
Kasper 2011 年 4 月 27 日

0 投票

yes that will work the first option. But that won't make it a function if I manually have to input those numbers. But thanks the subs function helped a great deal.
Anupama
Anupama 2020 年 5 月 20 日

0 投票

find tthe point of intersections of a circle at (1,2) and radius 4 and a straight line 2x+3y =9 using graphical method.

質問済み:

2011 年 4 月 27 日

回答済み:

2020 年 5 月 20 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by