MATLAB exercise program (polygon vertices)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.