How can I calculate the angles between a polygons vertices?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Xav Sholin
le 13 Nov 2021
Modifié(e) : Xav Sholin
le 16 Nov 2021
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.
0 commentaires
Réponse acceptée
Star Strider
le 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).
.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Elementary Polygons dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!