Intersection between line and circle when line ends inside circle (using geom2d)

I have a circle and a line drawn like so...
Image 1.png
Using the createLine function in the geom2d collection, the line is treated like this (i.e. continuous)...
Image 2.png
Therefore, when finding the intersection points between the line and the circle, two points are returned instead of one.
How can I solve this problem of showing an extra incorrect point, and is there a workaround you could suggest, either using geom2d or otherwise?
Many thanks.

댓글 수: 8

Would these be suitable?
polyxpoly is used for mapping and intersections is used for curves.
Looking at polyxpoly, would you be able to tell me how to input a circle into the function, as the example provided in the documentation uses longitude and latitude?
Many thanks for your help.
Provide experimental data
Position of line:
0.1947 0.4388
0.5173 0.5933
Center point of circle:
0.4850 0.6239
Radius of circle:
0.2035
% Position of line:
x1 = [0.1947
0.5173];
y1 = [0.4388
0.5933];
% Center point of circle:
x0 = 0.4850;
y0 = 0.6239;
% Radius of circle:
r = 0.2035;
t = linspace(0,2*pi,30);
x2 = r*cos(t) + x0;
y2 = r*sin(t) + y0;
[xc,yc] = polyxpoly(x1,y1,x2,y2)
To demonstrate both methods,
% Define circle
ang=0:0.1:2*pi;
xyr = [5 2 4]; %(xCenter,yCenter,radius)
xp=xyr(:,3)*cos(ang) + repmat(xyr(:,1),1,numel(ang)); %changed 190901 to work with r2016a
yp=xyr(:,3)*sin(ang) + repmat(xyr(:,2),1,numel(ang));
% Define line endpoints
xln = [1 6];
yln = [-1 1];
% plot it
clf()
plot(xp,yp,'k-') %circle
axis equal; grid on; hold on
plot(xln,yln,'b-') %line
% Method 1 uses this FEX:
% https://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections
[x0,y0] = intersections(xp,yp,xln,yln);
plot(x0,y0,'r*','MarkerSize',9,'LineWidth',2) %intersection point(s)
% Method 2
[xi,yi] = polyxpoly(xp,yp,xln,yln);
plot(xi,yi,'gh','MarkerSize',15,'LineWidth',2) %intersection point(s)
191121 093624-Figure 1.png
Someone should copy their comment to the answers section so it can be accepted.

댓글을 달려면 로그인하십시오.

 채택된 답변

  • Someone should copy their comment to the answers section so it can be accepted.
It should be me
% Position of line:
x1 = [0.1947
0.5173];
y1 = [0.4388
0.5933];
% Center point of circle:
x0 = 0.4850;
y0 = 0.6239;
% Radius of circle:
r = 0.2035;
t = linspace(0,2*pi,30);
x2 = r*cos(t) + x0;
y2 = r*sin(t) + y0;
[xc,yc] = polyxpoly(x1,y1,x2,y2)

댓글 수: 3

John D
John D 2019년 11월 21일
편집: John D 2019년 11월 21일
Thanks for demonstrating it.
2 Changes you need to make.
1) you're not extracting the x and y coordinates of the line correctly.
% x1 = [h.Position(1) h.Position(3)]; wrong
% y1 = [h.Position(2) h.Position(4)]; wrong
x1 = h.Position(:,1);
y1 = h.Position(:,2);
2) This one is minor. As long as your ROI line and circle objects are still on the plot, new lines will be plotted under them. If you mark the intersection with a small marker, you won't see it.
% solution one: make the marker large
plot(xc, yc, 'ro','markersize',12)
% solution two: remove the cirlce ROI object and replace it with your
% computed circle
delete(c)
hold on
plot(x2,y2,'b-', 'LineWidth',4)
% Plot intersection point on top
plot(xc, yc, 'ro')
Thanks, I realised this after I posted it!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Geographic Plots에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 11월 20일

댓글:

2019년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by