How to obtain union of three shapes given the coordinates
Show older comments
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
More Answers (2)
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
Oluwaseyi Ogun
on 13 Dec 2021
Tivial.
- generate the three objects as polyshapes.
- 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));
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

