Combine Two polyshape Objects Similar to addCell Functionality

9 views (last 30 days)
Hello everyone,
I am working with polyshape objects in MATLAB and want to combine (a bigger and a smaller) two polygons into one. I create a rectangular domain as the first polygon (p1) and a sawtooth-based shape as the second polygon (p2). My goal is combine these polygons, but obtain one polygon with two NumRegions, similar to the functionality of an addCell function (only 3D).
I know it is possible to generate the desired shape in a different way (but using only square, circles, etc). However, I am specifically using polyshape because I need to create custom shapes.
Here’s the code I’m using:
close all; clear; clc;
% First polygon
Lx = 40; % Length of the rectangular domain [mm]
Ly = 20; % Height of the rectangular domain [mm]
p1 = polyshape([0, Lx, Lx, 0], [Ly, Ly, 0, 0]); % Rectangular domain
% Second polygon
T = 10 * (1 / 50);
fs = 1000;
t = 0:1/fs:T-1/fs;
fun = sawtooth(2 * pi * 50 * t);
ini_tickness = 1 * ones(size(fun));
in_Domain = ini_tickness + normalize(fun, "range");
x_holo = linspace(0, Ly - 1, length(in_Domain));
y_holo = in_Domain;
p2 = polyshape([x_holo, fliplr(x_holo)], [zeros(size(x_holo)), fliplr(y_holo)]);
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.
p2 = rotate(p2, -90, [20, 0]); % Rotate
p2 = translate(p2, -19, -0.5); % translate
%Showing
figure(1);
subplot(2,1,1)
plot(p1);
axis equal;
subplot(2,1,2)
plot(p2);
axis equal;
pt=subtract(p1,p2);
figure(2);
plot(pt);
disp(['NumRegions:' num2str(pt.NumRegions)])
NumRegions:1

Answers (1)

Steven Lord
Steven Lord on 1 Dec 2024
p1 = nsidedpoly(4);
p2 = nsidedpoly(5);
The union function makes a polyshape with 1 region.
P = union(p1, p2)
P =
polyshape with properties: Vertices: [17x2 double] NumRegions: 1 NumHoles: 0
Concatenation makes an array of polyshapes.
P2 = [p1, p2]
P2 =
1x2 polyshape array with properties: Vertices NumRegions NumHoles
figure;
plot(p1);
axis([-1.5 1.5 -1.5 1.5]) % Make all the axes limits the same for visual comparison
title('square')
figure
plot(p2)
axis([-1.5 1.5 -1.5 1.5]) % Make all the axes limits the same for visual comparison
title('pentagon')
figure
plot(P)
axis([-1.5 1.5 -1.5 1.5]) % Make all the axes limits the same for visual comparison
title('union of a square and a pentagon')
figure
plot(P2)
axis([-1.5 1.5 -1.5 1.5]) % Make all the axes limits the same for visual comparison
title('two polyshapes, a square and a pentagon')
Note the differences in color in the plots of P and P2.
  1 Comment
Darby Paez
Darby Paez on 1 Dec 2024
Hi, thank you for your answer. However, that's not exactly what I was asking. Using union, the result is a single polygon with one NumRegions, and with concat ([]), they are treated as two polygons.
What I actually need is a single polyshape object with two NumRegions or Face, as I intend to use it to define subdomains. Additionally, as I mentioned earlier, I am using polyshape because I need to create custom shapes. Something I tried was using "subtranct" and addFaces to the result, but it was not possible.

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!