Intersection of circles and polygons?
Show older comments
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 Comment
Star Strider
on 11 Nov 2021
.
Accepted Answer
More Answers (1)
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
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

