After a simple polygon is generated, it is sometimes necessary to judge the concave and convex of each vertex of the polygon.
Calculate the two vectors of the vertex to be processed and the adjacent vertex, and then calculate the cross multiplication of the two vectors. Based on the positive and negative values of the obtained result, we can judge the concave and convex.
If the result is negative, it is a concave vertex, and the regular expression is a convex vertex.
Concave vertices are represented by O, and convex vertices are represented.
The result is as follows:
The Matlab code is as follows:
Clear all; close all; clc; n = 20; P = rand (n, 2); P = createsimplypoly (p); % create a simple polygon hold on; for I = 1: n if I = 1% process the first vertex V1 = P (n, :)-P (1, :); % vector v2 = P (2, :)-P (1, :); % elseif I = n %, the last vertex V1 = P (n-1, :)-P (n ,:); v2 = P (1, :)-P (n, :); else % other vertices V1 = P (I-1, :)-P (I, :); v2 = P (I + 1, :)-P (I, :); end r = det ([V1; V2]); % if r> 0 plot (P (I, 1), P (I, 2), '*'); elseif r <0 plot (P (I, 1), P (I, 2), 'O '); endendplot (P (:, 1), P (:, 2); P = circshift (P, 1); plot (P (:, 1), P (:, 2 ));
Createsimplypoly. m
Function p = createsimplypoly (p) CEN = mean (p); Ang = atan2 (P (:, 1)-CEN (1), P (:, 2) -CEN (2); % P = [p, Ang]; P = sortrows (p, 3 ); % sort by polar angle P = P (:,); End
MATLAB exercise program (polygon vertices)