Boolean operations with polyshape objects
6 views (last 30 days)
Show older comments
Oluwaseyi Ogun
on 8 Dec 2022
Edited: John D'Errico
on 8 Dec 2022
Hello everyone, I am trying to subtract a sector from a circle but I am getting some unwanted cut in the result as highlighted in red in the attached image. Please how may I remove this? Or is there a better way to achieve the same thing? Your help is very much appreciated
Below is the code.
r1 = 1.5; sec_ang=40; sec_ang=sec_ang*pi/180;
beta=60; beta= beta*pi/180; r2 = r1 + 2;
beta = beta*ones(1,2); % rotation angle
sec_ang = sec_ang*ones(1,2); % sectorial angle
r = [r1, r2] ; % radii
x0 = 0; y0 = 0;
theta = 0:pi/360:2*pi;
geom=[];
for i = 1:length(r)
circ = [r(i)*cos(theta)+x0 ; r(i)*sin(theta)+y0]; % generate points on the circle
a1 = beta(i); % start angle of arc
a2 = a1 + sec_ang(i); % end angle of arc
t = linspace(a1, a2);
xy = [x0 + (r(i)+1e-2)*cos(t); y0 + (r(i)+1e-2)*sin(t)]; % points on the arc of a sector
poly_sec= polyshape([x0,xy(1,:),x0], [y0, xy(2,:), y0]); %generate sector
p_circ = polyshape(circ(1,:), circ(2,:));
p_sub=subtract(p_circ,poly_sec); % subtract the sector from the circle
geom = [geom, p_sub.Vertices'];
end
p = polyshape(geom(1,:), geom(2,:));
plot(p,'FaceColor', 'k','LineWidth', 2)
axis equal
0 Comments
Accepted Answer
John D'Errico
on 8 Dec 2022
Edited: John D'Errico
on 8 Dec 2022
Computers are such literal things. They do what you tell them to do, not what you want them to do. The fix lies in the mind reading toolbox, which is still under beta tasting. And sadly, it may never get out of beta testing. :)
Anyway, what you did was not overtly wrong. It just had that annoying horizontal line.
But how about this instead?
r = [1.5 3.5];
xy0 = [0 0];
theta = linspace(0,2*pi,1000)';
theta(end) = []; % avoids warning messages from polyshape
secang = 40*pi/180;
secrot = 60*pi/180;
Cin = polyshape(r(1)*[cos(theta),sin(theta)] + xy0);
Cout = polyshape(r(2)*[cos(theta),sin(theta)] + xy0);
ring = subtract(Cout,Cin); % This is an important part.
plot(ring)
axis equal
Note that there is no horizontal line at theta = 0 now. This is because I created the polyshape in a way that avoids it. There is a subtle difference in what I did if you think carefully about it.
Next, create a sector to remove. I'll go out to a distance of 100 from the origin. A triangular wedge of pie, essentially.
rotmatrix = @(rotangle) [cos(rotangle) sin(rotangle);-sin(rotangle) cos(rotangle)];
sector = polyshape([0 0;100 0;100*[cos(secang) sin(secang)]]*rotmatrix(secrot) + xy0);
partialring = subtract(ring,sector);
plot(partialring)
axis equal
Easy peasy. No steenkin' loops either. :)
More Answers (0)
See Also
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!