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

조회 수: 17 (최근 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