How can I calculate the angles between a polygons vertices?

21 views (last 30 days)
Due to my lack of MATLAB code knowledge, I am lost as to how I can calculate the angles between vertices of a random polygon.
Below I load and plot a polygon using the polyshape() function in which the x and y values are loaded from a txt file.
My goal is to calculate the angles shown with the red marker any suggestions how this can be acheived.

Accepted Answer

Star Strider
Star Strider on 13 Nov 2021
Try this —
X_Y_Val = [0.29218, 0.17609
0.56518, 0.27635
0.69555, 0.16324
0.83819, 0.49486
0.62653, 0.63882
0.27684, 0.49743];
X_Val = X_Y_Val(:,1);
Y_Val = X_Y_Val(:,2);
polygon = polyshape(X_Val, Y_Val);
V = [X_Y_Val(end,:); X_Y_Val; X_Y_Val(1,:)]; % Augmented Matrix For Angle Calculations
for k = 2:size(V,1)-1
anglr(:,k-1) = [(atan2(V(k-1,2)-V(k,2), V(k-1,1)-V(k,1))); (atan2(V(k+1,2)-V(k,2), V(k+1,1)-V(k,1)))]; % Calculate Radian Angles
angld(:,k-1) = rad2deg(anglr(:,k-1)); % Convert To Degrees
anglrinc(k-1) = mod(2*pi-diff(anglr(:,k-1)),2*pi); % Reduce Radian Angles
angldinc(k-1) = mod(360-diff(angld(:,k-1)),360); % Reduce Degree Angles
end
% A = [anglr; angld]; % Display Interim Results (Optional)
% Ainc =[anglrinc; angldinc]; % Display Final Results (Optional)
figure
plot(polygon)
hold on
% plot(X_Val, Y_Val, 'p')
hold off
grid
axis('equal')
text(X_Val, Y_Val, compose('\\angle%d = %.1f°',[1:size(X_Y_Val,1); angldinc].'))
The code is comment-documented and is relatively self-explanatory. The angles appear to be correct (although I did not measure them by hand).
.

More Answers (0)

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!