POJ scrambled Polygon convex bag

Source: Internet
Author: User
Tags cmath

Scrambled Polygon
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7214 Accepted: 3445

Description

A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments is called the vertices of the polygon. When one starts at any vertex of a closed polygon and traverses each bounding line segment exactly once, one comes back to The starting vertex.

A closed polygon is called convex if the line segment joining any and points of the polygon lies in the polygon. Figure 1 shows a closed polygon which are convex and one which is not convex. (informally, a closed polygon is convex if its border doesn ' t has any "dents".)

The subject of this problem are a closed convex polygon in the coordinate plane, one of the whose vertices is the origin (x = 0 , y = 0). Figure 2 shows an example. Such a polygon would have both properties significant for this problem.

The first is the vertices of the polygon would be confined to three or fewer of the four quadrants of the COO Rdinate plane. In the example shown in Figure 2, none of the vertices is in the second quadrant (where x < 0, y > 0).

To describe the second property, suppose you ' take a trip ' around the Polygon:start at (0, 0), visit all other vertices E Xactly once, and arrive at (0, 0). As you visit each vertex (other than (0, 0)), draw the diagonal, which connects the current vertex with (0, 0), and Calculat E The slope of this diagonal. Then, within each quadrant, the slopes of these diagonals would form a decreasing or increasing sequence of numbers, i.e., They'll be sorted. Figure 3 illustrates.

Input

The input lists the vertices of a closed convex polygon in the plane. The number of lines in the input would be is at least three and no more than 50. Each line contains the x and y coordinates of one vertex. Each x and y coordinate are an integer in the range-999..999. The vertex on the first line of the input file would be the origin, i.e., x = 0 and y = 0. Otherwise, the vertices is in a scrambled order. Except for the origin, no vertex'll be on the x-axis or the y-axis. No three vertices is colinear.

Output

The output lists the vertices of the given polygon, one vertex per line. Each vertex from the input appears exactly once in the output. The origin (0,0) is the vertex on the first line of the output. The order of vertices in the output would determine a trip taken along the polygon's border, in the counterclockwise direct Ion. The output format for each vertex is (x, y) as shown below.

Sample Input

0 070-5060 30-30-5080 2050-6090-20-30-40-10-6090 10

Sample Output

(0,0) ( -30,-40) ( -30,-50) ( -10,-60) (50,-60) (70,-50) (90,-20) (90,10) (80,20) (60,30)

The topic is very long, in fact, is to give a set of points, including the Origin point, to find out the set of points of the convex hull of each point, in accordance with the counter-clockwise direction from the origin of the output of the entire convex hull vertex
There are two ways to do this: one is Graham-scan, the other is the direct polar order, and the origin is the datum point to row
The code is as follows
/*Polar Coordinate sorting method*/#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#defineEPS 1e-8using namespacestd;structpoint{Doublex, y;};Const intMAXN = -;p oint p[maxn], pp;//pp is the datum pointintN;intSgnDoublex) {    if(Fabs (x) <EPS)return 0; returnX <0? -1:1;}Doubleget_direction (Point P1, point P2, point p3) {return((p3.x-p1.x) * (P2.Y-P1.Y)-(p2.x-p1.x) * (P3.Y-p1.y));}Doubleget_distance (Point p1, point p2) {returnsqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-p2.y));}BOOLcmpConstPoint P1,ConstPoint p2)//The comparison function of polar coordinate sorting{    if(SGN (Get_direction (PP, p1, p2)) <0)         return true; if(SGN (Get_direction (PP, p1, p2)) = =0&& get_distance (pp, p1) <get_distance (pp, p2))return true; return false; }intMain () {n=0;  while(~SCANF ("%LF%LF", &p[n].x, &p[n].y)) N++; inti;  for(i =0; I < n; i++)    {        if(p[i].x = =0&& P[i].y = =0)             Break; } pp=P[i]; P[i]= p[0]; p[0] =pp; Sort (p, p+N, CMP);  for(inti =0; I < n; i++) printf ("(%.0f,%.0f) \ n", p[i].x, P[I].Y); return 0;}
View Code

Ordinary Graham.

/************************************************************************* > File Name:poj_2007.cpp > Author:howe_young > Mail: [email protected] > Created time:2015 Year April 16 Thursday 14:47 43 sec ************************************************************************/#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<cstdio>#defineEPS 1e-8using namespacestd;structpoint{Doublex, y;};Const intMAXN = -;p oint P[MAXN];intN, top, CONVEX[MAXN];intSgnDoublex) {    if(Fabs (x) <EPS)return 0; returnX <0? -1:1;} BOOLcmpConstPoint P1,ConstPoint p2) {    return(P1.y = = P2.y && p1.x < p2.x) | | P1.y <p2.y);}Doubleget_direction (Point P1, point P2, point p3) {return((p3.x-p1.x) * (P2.Y-P1.Y)-(p2.x-p1.x) * (P3.Y-p1.y));}voidGraham () {Top=0;  for(inti =0; I < n; i++)    {         while(Top >1&& sgn (get_direction (P[convex[top-2]], P[convex[top-1]], p[i]) >=0) Top--; Convex[top++] =i; }    intTMP =top;  for(inti = n-2; I >=0; i--)    {         while(Top > TMP && sgn (get_direction (P[convex[top-2]], P[convex[top-1]], p[i]) >=0) Top--; Convex[top++] =i; }}intMain () {n=0;  while(~SCANF ("%LF%LF", &p[n].x, &p[n].y)) n++; Sort (p, p+N, CMP);    Graham (); intK;  for(k =0; K < top; k++)        if(p[convex[k]].x = =0&& P[convex[k]].y = =0)             Break;  for(inti = k; I < top-1; i++) printf ("(%.0f,%.0f) \ n", p[convex[i]].x, P[CONVEX[I]].Y);  for(inti =0; I < K; i++) printf ("(%.0f,%.0f) \ n", p[convex[i]].x, P[CONVEX[I]].Y); return 0;}
View Code

POJ scrambled Polygon convex bag

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.