Combine Two polyshape Objects Similar to addCell Functionality
26 views (last 30 days)
Show older comments
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)]);
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)])
0 Comments
Answers (1)
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)
Concatenation makes an array of polyshapes.
P2 = [p1, p2]
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.
2 Comments
See Also
Categories
Find more on Elementary Polygons 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!




