How to obtain union of three shapes given the coordinates

I am trying to obtain the union of shapes comprising of 2 rectangles and a circle. Please does anyone know how to get the union coordinates? .Below is the matlab code, and the corresponding figure.
cy_l=0.3; length of rectangle outside the circle
r=0.5; % radius of circle
a=0.3; % width of rectangle
b=2*r+2*cy_l; % length of rectangle
centre=[0.5 0.3]; %circle centre
theta=0:2*pi/360:2*pi;
circ=[r*cos(theta')+centre(1) r*sin(theta')+centre(2)]; % circle coordinates
%% rectangle
xy=[centre(1)-(r+cy_l), centre(2)+a/2]; % top left coordinates of rectangle1
xy1=[centre(1)-a/2, centre(2)+(r+cy_l)]; %top left coordinate of rectangle2
R1=[xy(1), xy(1), xy(1)+b, xy(1)+b, xy(1);
xy(2)-a, xy(2), xy(2), xy(2)-a, xy(2)-a]; % coordinates of rectangle 1
R2= [ xy1(1), xy1(1), xy1(1)+a, xy1(1)+a, xy1(1);
xy1(2)-b, xy1(2), xy1(2), xy1(2)-b, xy1(2)-b]; %coordinates of rectangle 2
plot(circ(:,1), circ(:,2), 'k', R1(1,:), R1(2,:),'k', R2(1,:), R2(2,:), 'k', 'LineWidth', 2)
grid on
axis equal

 Accepted Answer

I'd use polyshape.
cy_l=0.3; % length of rectangle outside the circle
r=0.5; % radius of circle
a=0.3; % width of rectangle
b=2*r+2*cy_l; % length of rectangle
centre=[0.5 0.3]; %circle centre
theta=0:2*pi/360:2*pi;
circ=polyshape(r*cos(theta')+centre(1), r*sin(theta')+centre(2)); % Circle
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
%% rectangle
xy=[centre(1)-(r+cy_l), centre(2)+a/2]; % top left coordinates of rectangle1
xy1=[centre(1)-a/2, centre(2)+(r+cy_l)]; %top left coordinate of rectangle2
R1=polyshape([xy(1), xy(1), xy(1)+b, xy(1)+b, xy(1)], ...
[xy(2)-a, xy(2), xy(2), xy(2)-a, xy(2)-a]); % rectangle 1
R2= polyshape([xy1(1), xy1(1), xy1(1)+a, xy1(1)+a, xy1(1)], ...
[xy1(2)-b, xy1(2), xy1(2), xy1(2)-b, xy1(2)-b]); % rectangle 2
plot([circ, R1, R2])
axis equal
% Show the union in a separate figure for comparison
figure
plot(union([circ, R1, R2]), 'FaceColor', 'g')
axis equal

More Answers (2)

Alex Alex
Alex Alex on 13 Dec 2021
Edited: Alex Alex on 13 Dec 2021
may be command "polyxpoly" help you
[xi1,yi1] = polyxpoly(circ(:,1), circ(:,2), R1(1,:), R1(2,:))
[xi2,yi2] = polyxpoly(circ(:,1), circ(:,2), R2(1,:), R2(2,:))
[xi3,yi3] = polyxpoly(R1(1,:), R1(2,:), R2(1,:), R2(2,:))
plot(xi1,yi1, 'o')
plot(xi2,yi2, 'o')
plot(xi3,yi3, 'o')

1 Comment

ok. i tried that but what i got is not the union of the figure. I got an 'N' shape

Sign in to comment.

Tivial.
  1. generate the three objects as polyshapes.
  2. Compute the union.
For example...
cy_l=0.3; % length of rectangle outside the circle
r=0.5; % radius of circle
a=0.3; % width of rectangle
b=2*r+2*cy_l; % length of rectangle
centre=[0.5 0.3]; %circle centre
theta=0:2*pi/360:2*pi;
circ=[r*cos(theta')+centre(1) r*sin(theta')+centre(2)]; % circle coordinates
%% rectangle
xy=[centre(1)-(r+cy_l), centre(2)+a/2]; % top left coordinates of rectangle1
xy1=[centre(1)-a/2, centre(2)+(r+cy_l)]; %top left coordinate of rectangle2
R1=[xy(1), xy(1), xy(1)+b, xy(1)+b, xy(1);
xy(2)-a, xy(2), xy(2), xy(2)-a, xy(2)-a]; % coordinates of rectangle 1
R2= [ xy1(1), xy1(1), xy1(1)+a, xy1(1)+a, xy1(1);
xy1(2)-b, xy1(2), xy1(2), xy1(2)-b, xy1(2)-b]; %coordinates of rectangle 2
PSc = polyshape(circ(:,1),circ(:,2));
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
PSr1 = polyshape(R1(1,:),R1(2,:));
PSr2 = polyshape(R2(1,:),R2(2,:));
PSu = union(union(PSc,PSr1),PSr2);
plot(PSu)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!