How to obtain union of three shapes given the coordinates

조회 수: 8 (최근 30일)
Oluwaseyi Ogun
Oluwaseyi Ogun 2021년 12월 13일
댓글: Oluwaseyi Ogun 2021년 12월 13일
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

채택된 답변

Steven Lord
Steven Lord 2021년 12월 13일
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

추가 답변 (2개)

Alex Alex
Alex Alex 2021년 12월 13일
편집: Alex Alex 2021년 12월 13일
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
Oluwaseyi Ogun
Oluwaseyi Ogun 2021년 12월 13일
ok. i tried that but what i got is not the union of the figure. I got an 'N' shape

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2021년 12월 13일
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)

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by