Intersection of circles and polygons?

35 ビュー (過去 30 日間)
Brain Splurge
Brain Splurge 2021 年 11 月 11 日
編集済み: Brain Splurge 2021 年 12 月 2 日
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.
  1 件のコメント
Star Strider
Star Strider 2021 年 11 月 11 日
The inpolygon function could be helpful here.
.

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

採用された回答

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 Center および File ExchangeElementary Polygons についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by