Hdu2108: Determine whether a polygon is a convex polygon or a concave polygon.

Source: Internet
Author: User

The question is not difficult. We still use the vector cross product to see the figure.

Next, you only need to judge the relationship between one side and one point counterclockwise. If the cross product is greater than 0, it indicates that there is an inner angle greater than 180, that is, a concave polygon.

# Include <iostream> # include <math. h> using namespace STD; struct node {int X, Y;} p [100]; int seg (node P1, node P2, node P3) {int d = (p3.x-p1.x) * (p2.y-p1.y)-(p2.x-p1.x) * (p3.y-p1.y); If (d <0) return 1; else return 0 ;}int main () {int N; while (scanf ("% d", & n) = 1 & N) {for (INT I = 0; I <n; I ++) scanf ("% d", & P [I]. x, & P [I]. y); P [N]. X = P [0]. x, p [N]. y = P [0]. y; P [n + 1]. X = P [1]. x, p [n + 1]. y = P [1]. y; int flag = 0; If (n <= 3) Flag = 0; else {If (SEG (P [0], P [1], p [2]) {for (INT I = 3; I <n + 2; I ++) if (! SEG (P [I-2], p [I-1], p [I]) {flag = 1; break;} else flag = 0;} else flag = 1 ;} if (FLAG) printf ("Concave \ n"); else printf ("convex \ n");} return 0 ;}

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.