How to Discretize a Polygon boundary in to equally spaced points?

4 次查看(过去 30 天)
The following code plots a polygon (Rectangle with curved corners).
%%
clc
clear all
close all
%%
h=polybuffer( polyshape([0.15 0 -0.15 -0.15 -0.15 0 0.15 0.15], [0.25 0.25 0.25 0.10 -0.05 -0.05 -0.05 0.10]),0.1);
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.
pgon1 = polyshape({h.Vertices(:,1)}, {h.Vertices(:,2)});
xb = (pgon1.Vertices(:,1))';
yb = (pgon1.Vertices(:,2))';
xb = xb(:);
yb = yb(:);
figure
plot(xb, yb, 'DisplayName','All Data')
axis([-0.4 0.4 -0.2 0.4])
Currently, I have a set of points near the curved region and very limited over the straight portions. I want to generate a set of boundary points for this polyshape that are equally spaced. How do I do that?

采纳的回答

Suraj Kumar
Suraj Kumar 2024-12-21
Based on my understanding you want to discretize the boundary of a polygon into equally spaced points.
To achieve this, you can refer to the following steps:
1. After defining the polygon, extract its vertices and calculate the Euclidean distances between consecutive vertices.You can use the function 'cumsum' to get the cumulative distances along the boundary.
xb = (pgon1.Vertices(:,1))';
yb = (pgon1.Vertices(:,2))';
distances = [0; cumsum(sqrt(diff(xb).^2 + diff(yb).^2))];
2. Choose the number of equally spaced points and use `linspace` to generate target distances along the perimeter.
numPoints = 30;
equalSpacedDistances = linspace(0, distances(end), numPoints);
3. Then apply `interp1` to interpolate the x and y coordinates at these distances, yielding points uniformly distributed along the boundary.
xEqualSpaced = interp1(distances, xb, equalSpacedDistances);
yEqualSpaced = interp1(distances, yb, equalSpacedDistances);
You can refer to the attached output for a better understanding:
To learn more about 'cumsum' and 'interp1' functions in MATLAB, please refer to the following links:
Happy Coding!

更多回答(1 个)

Walter Roberson
Walter Roberson 2024-12-21
编辑:Walter Roberson 2024-12-21
See John D'errico file exchange contribution interparc https://www.mathworks.com/matlabcentral/fileexchange/34874-interparc

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by