Borrar filtros
Borrar filtros

converting shape into polygon with sides of a given length

2 visualizaciones (últimos 30 días)
MaryD
MaryD el 16 de Nov. de 2021
Respondida: Nivedita el 3 de Mayo de 2024
I want to convert given shape represented by set of points/alpha shape/polygon (not necessarily convex) into polygon in which each side has the same length. Of course it will only be an approximation but i wonder if there are some helpfull functions or algorithims to achive that.

Respuestas (1)

Nivedita
Nivedita el 3 de Mayo de 2024
Hello Mary,
Assuming that you want to keep the number of sides of the equal length polygon the same as the initial shape, here is an example code on how you could approach it:
% Define an irregular 6-sided shape (hexagon) with vertices
X = [0, 2, 4, 5, 3, 1];
Y = [0, -1, 0, 3, 5, 3];
% Number of sides (N)
N = length(X);
% Compute the centroid of the irregular shape
centroidX = mean(X);
centroidY = mean(Y);
% Calculate the average radius (distance from centroid to vertices)
distances = sqrt((X - centroidX).^2 + (Y - centroidY).^2);
avgRadius = mean(distances);
% Generate the equilateral polygon (hexagon) approximation
theta = linspace(0, 2*pi, N+1); % N+1 points, removing the last one to close the polygon
theta(end) = []; % Remove the duplicate point
X_poly = centroidX + avgRadius * cos(theta);
Y_poly = centroidY + avgRadius * sin(theta);
% Plotting for visualization
figure;
plot([X, X(1)], [Y, Y(1)], 'b-o', 'LineWidth', 2, 'MarkerFaceColor', 'b'); hold on; % Original shape
plot([X_poly, X_poly(1)], [Y_poly, Y_poly(1)], 'r-o', 'LineWidth', 2, 'MarkerFaceColor', 'r'); % Equilateral polygon
axis equal; grid on;
legend('Original', 'Equilateral Polygon', 'Location', 'northeast');
title('Approximation of an Irregular 6-sided Shape with an Equilateral Polygon');
xlabel('X-axis');
ylabel('Y-axis');
This script starts by defining an irregular hexagon through a set of X and Y coordinates for its vertices. It then calculates the centroid of this shape and uses the average distance from the vertices to the centroid to determine the radius for an equilateral hexagon. This equilateral hexagon is then plotted alongside the original shape for comparison.

Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by