- https://www.mathworks.com/help/matlab/ref/double.cumsum.html
- https://www.mathworks.com/help/matlab/ref/double.interp1.html
How to Discretize a Polygon boundary in to equally spaced points?
40 views (last 30 days)
Show older comments
Prasanna Routray
on 21 Dec 2024 at 5:50
Edited: Walter Roberson
on 21 Dec 2024 at 19:17
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);
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?
0 Comments
Accepted Answer
Suraj Kumar
on 21 Dec 2024 at 7:35
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!
0 Comments
More Answers (1)
Walter Roberson
on 21 Dec 2024 at 7:48
Edited: Walter Roberson
on 21 Dec 2024 at 19:17
See John D'errico file exchange contribution interparc https://www.mathworks.com/matlabcentral/fileexchange/34874-interparc
0 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!