필터 지우기
필터 지우기

How can I calculate the angles between a polygons vertices?

조회 수: 16 (최근 30일)
Xav Sholin
Xav Sholin 2021년 11월 13일
편집: Xav Sholin 2021년 11월 16일
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.

채택된 답변

Star Strider
Star Strider 2021년 11월 13일
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개)

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by