Poj 3850 simple polygon

Source: Internet
Author: User
/* Question: given some vertices, find a sequence of points and follow the sequence connection point (the beginning and end are also connected) to form a polygon (any line and other lines do not want to be handed in) just sort the polar angles directly. Pay attention to the case of Collinearity, except for the first place with a very small distance from the starting position, other locations are far away from the pole */# include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> const int maxn = 2009; const double EPS = 1e-8; struct point {Double X, Y; int ID;} p [maxn], H [maxn]; inline double distance (const point & P1, const point & p2) {return SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));} // calculate the inline double multiply (const point & sp, const point & EP, const point & OP) {return (sp. x-op.x) * (Ep. y-op.y)-(Ep. x-op.x) * (sp. y-op.y);} // determine whether SP, EP, OP meet the left turn int CMP (const void * a, const void * B) {// sort point * P1 = (point *) A; point * P2 = (point *) B; Double T = multiply (* P2, * P1, P [0]); If (T> EPS) return 1; else if (FABS (t) <= EPS) {If (distance (P [0], * P1) <distance (P [0], * P2) return 1; else return-1;} else return-1;} int CMP1 (const void * a, const void * B) {// sort point * P1 = (point *) A; point * P2 = (point *) B; Double T = multiply (* P2, * P1, P [0]); If (T> EPS) return 1; else if (FABS (t) <= EPS) {If (distance (P [0], * P1)> distance (P [0], * P2) return 1; else return-1;} else return-1;} void anglesort (point P [], int N) {// find the point int I, K = 0; point temp; for (I = 1; I <n; I ++) if (P [I]. x <p [K]. X | (P [I]. X = P [K]. x) & (P [I]. Y <p [K]. y) k = I; temp = P [0], p [0] = P [K], p [k] = temp; qsort (p + 1, n-1, sizeof (point), CMP); for (I = 2; I <n; I ++) // find the vertices on the edge, re-order if (FABS (multiply (P [0], p [1], p [I])> EPS) {break;} qsort (p + 1, I-1, sizeof (point), CMP1);} void graham_scan (point P [], point ch [], int N, Int & Len) {// create a convex packet int I, top = 2; anglesort (p, n);} int main () {int I, j, N, Len; double D, ans; int CA; scanf ("% d", & Ca); While (ca --) {scanf ("% d", & N); for (I = 0; I <N; I ++) {scanf ("% lf", & P [I]. x, & P [I]. y); P [I]. id = I;} graham_scan (p, H, N, Len); int flag = 1; for (INT I = 0; I <n; I ++) {If (FLAG) {printf ("% d", P [I]. ID); flag = 0; continue;} printf ("% d", P [I]. ID);} puts ("");} return 0 ;}

Related Article

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.