Intersection of circles and polygons?
조회 수: 27 (최근 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
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Elementary Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!