How to obtain union of three shapes given the coordinates
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Oluwaseyi Ogun
am 13 Dez. 2021
Kommentiert: Oluwaseyi Ogun
am 13 Dez. 2021
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/832770/image.png)
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 13 Dez. 2021
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
%% 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
Weitere Antworten (2)
Alex Alex
am 13 Dez. 2021
Bearbeitet: Alex Alex
am 13 Dez. 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')
John D'Errico
am 13 Dez. 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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Map Display finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!