Intersection of circles and polygons?

28 次查看(过去 30 天)
I am a bit lost on how I can get the points where a circle intersects a polygon. The circle was created using the rectangle() function and the polygon was plotted using the polyshape() function.

采纳的回答

Matt J
Matt J 2021-11-11
编辑:Matt J 2021-11-11
If you approximate the circle as a polyshape as well, it is very easy to do with
Example:
pgon=polyshape( [2.8259 1.8997
1.3496 4.4206
3.3552 3.4178
4.3719 4.6992
4.8872 2.1226
3.2298 2.7214
3.3134 1.1337
1.1685 1.1616] ); %hypothetical polygon
R=1.5; [x0,y0]=deal(3,3); %Circle radius and center
t=linspace(0,360,1000).'; t(end)=[]; %circle angular samples
circle=polyshape([cosd(t), sind(t)]*R+[x0,y0]);
plot([pgon,circle]); axis equal
%find intersections
V=pgon.Vertices;
N=size(V,1);
V=V([1:N,1],:);
hold on
for i=1:N
xy=linexlines2D(circle,V(i,:),V(i+1,:));
plot(xy(1,:),xy(2,:),'or','MarkerFaceColor','r');
end
hold off
  1 个评论
Steven Lord
Steven Lord 2021-11-12
One easier way to approximate the circle by a polyshape is to create a regular N-sided polygon for a large value of N.
P = nsidedpoly(1000, 'Center', [3 3], 'Radius', 2);
plot(P)
axis equal
That looks pretty circular to me, though if you want you could increase 1000 to 10k or even higher.

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2021-11-11
编辑:Matt J 2021-11-12
This is similar to my other answer, but instead of approximating the circle as a polygon, it does an exact theoretical calculation of the intersections. It will probably be much faster.
pgon=polyshape( [2.8259 1.8997
1.3496 4.4206
3.3552 3.4178
4.3719 4.6992
4.8872 2.1226
3.2298 2.7214
3.3134 1.1337
1.1685 1.1616] ); %hypothetical polygon
R=1.5; [x0,y0]=deal(3,3); %Circle radius and center
plot(pgon); axis equal %plot polygon
hold on
fimplicit(@(x,y)(x-x0).^2+(y-y0).^2-R^2); %plot circle
%find intersections
V=pgon.Vertices;
N=size(V,1);
V=V([1:N,1],:);
for i=1:N % loop over the polygon edges
v0=V(i,:)-[x0,y0];
d=V(i+1,:)-V(i,:);
Ax=d(1); Bx=v0(1);
Ay=d(2); By=v0(2);
q=[Ax.^2+Ay.^2, 2*(Ax*Bx+Ay*By), Bx.^2+By.^2-R^2];
t=roots(q);
t(abs(imag(t))>1e-8*abs(real(t)))=[];
t=t(0<=t &t<=1);
if isempty(t), continue; end
xy=V(i,:)+t*d; %intersection(s) with current polygon edge
plot(xy(:,1),xy(:,2),'or','MarkerFaceColor','r');
end
hold off

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by