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 are 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 two points of the polygon lies in the polygon. Figure 1 shows a closed polygon which is convex and one which is not convex. (Informally, a closed polygon is convex if its border doesn't have any "dents".) 

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

The first property is that the vertices of the polygon will be confined to three or fewer of the four quadrants of the coordinate plane. In the example shown in Figure 2, none of the vertices are 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 exactly once, and arrive at (0, 0). As you visit each vertex (other than (0, 0)), draw the diagonal that connects the current vertex with (0, 0), and calculate the slope of this diagonal. Then, within each quadrant, the slopes of these diagonals will form a decreasing or increasing sequence of numbers, i.e., they will be sorted. Figure 3 illustrates this point. 
 

Input

The input lists the vertices of a closed convex polygon in the plane. The number of lines in the input will be at least three but no more than 50. Each line contains the x and y coordinates of one vertex. Each x and y coordinate is an integer in the range -999..999. The vertex on the first line of the input file will be the origin, i.e., x = 0 and y = 0. Otherwise, the vertices may be in a scrambled order. Except for the origin, no vertex will be on the x-axis or the y-axis. No three vertices are 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 will determine a trip taken along the polygon's border, in the counterclockwise direction. The output format for each vertex is (x,y) as shown below.

Sample Input

0 0
70 -50
60 30
-30 -50
80 20
50 -60
90 -20
-30 -40
-10 -60
90 10

Sample Output

(0,0)
(-30,-40)
(-30,-50)
(-10,-60)
(50,-60)
(70,-50)
(90,-20)
(90,10)
(80,20)
(60,30) 题目叙述很长,其实就是给出一组包括原点在内的点,求出这组点的凸包的各个定点,按照逆时针方向从原点开始输出整个凸包的顶点
两种方法可以做:一个是Graham-Scan,还有就是直接极坐标排序,选取原点为基准点来排
代码如下
/*极坐标排序方法*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#define EPS 1e-8 using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn], pp;//pp是基准点
int n;
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
double get_direction(point p1, point p2, point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
double get_distance(point p1, point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
bool cmp(const point p1, const point p2)//极坐标排序的比较函数
{
if (sgn(get_direction(pp, p1, p2)) < )
return true;
if (sgn(get_direction(pp, p1, p2)) == && get_distance(pp, p1) < get_distance(pp, p2))
return true;
return false;
}
int main()
{
n = ;
while (~scanf("%lf %lf", &p[n].x, &p[n].y))
n++;
int i;
for (i = ; i < n; i++)
{
if (p[i].x == && p[i].y == )
break;
}
pp = p[i];
p[i] = p[];
p[] = pp; sort(p, p + n, cmp);
for (int i = ; i < n; i++)
printf("(%.0f,%.0f)\n", p[i].x, p[i].y); return ;
}

普通的Graham

/*************************************************************************
> File Name: poj_2007.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月16日 星期四 14时47分43秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define EPS 1e-8
using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn];
int n, top, convex[maxn];
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
bool cmp(const point p1, const point p2)
{
return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
}
double get_direction(point p1, point p2, point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
void Graham()
{
top = ;
for (int i = ; i < n; i++)
{
while (top > && sgn(get_direction(p[convex[top - ]], p[convex[top - ]], p[i])) >= )
top--;
convex[top++] = i;
}
int tmp = top;
for (int i = n - ; i >= ; i--)
{
while (top > tmp && sgn(get_direction(p[convex[top - ]], p[convex[top - ]], p[i])) >= )
top--;
convex[top++] = i;
}
}
int main()
{
n = ;
while (~scanf("%lf %lf", &p[n].x, &p[n].y)) n++;
sort(p, p + n, cmp);
Graham();
int k;
for (k = ; k < top; k++)
if (p[convex[k]].x == && p[convex[k]].y == )
break;
for (int i = k; i < top - ; i++)
printf("(%.0f,%.0f)\n", p[convex[i]].x, p[convex[i]].y);
for (int i = ; i < k; i++)
printf("(%.0f,%.0f)\n", p[convex[i]].x, p[convex[i]].y);
return ;
}

POJ 2007 Scrambled Polygon 凸包的更多相关文章

  1. POJ 2007 Scrambled Polygon [凸包 极角排序]

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8636   Accepted: 4105 ...

  2. POJ 2007 Scrambled Polygon 凸包点排序逆时针输出

    题意:如题 用Graham,直接就能得到逆时针的凸包,找到原点输出就行了,赤果果的水题- 代码: /* * Author: illuz <iilluzen[at]gmail.com> * ...

  3. POJ 2007 Scrambled Polygon 极角序 水

    LINK 题意:给出一个简单多边形,按极角序输出其坐标. 思路:水题.对任意两点求叉积正负判断相对位置,为0则按长度排序 /** @Date : 2017-07-13 16:46:17 * @File ...

  4. poj 2007 Scrambled Polygon(极角排序)

    http://poj.org/problem?id=2007 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6701   A ...

  5. ●POJ 2007 Scrambled Polygon

    题链: http://poj.org/problem?id=2007 题解: 计算几何,极角排序 按样例来说,应该就是要把凸包上的i点按 第三像限-第四像限-第一像限-第二像限 的顺序输出. 按 叉积 ...

  6. 简单几何(极角排序) POJ 2007 Scrambled Polygon

    题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...

  7. POJ 2007 Scrambled Polygon(简单极角排序)

    水题,根本不用凸包,就是一简单的极角排序. 叉乘<0,逆时针. #include <iostream> #include <cstdio> #include <cs ...

  8. POJ 2007 Scrambled Polygon (简单极角排序)

    题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...

  9. poj 2007 Scrambled Polygon 极角排序

    /** 极角排序输出,,, 主要atan2(y,x) 容易失精度,,用 bool cmp(point a,point b){ 5 if(cross(a-tmp,b-tmp)>0) 6 retur ...

随机推荐

  1. 03:计算(a+b)/c的值

    总时间限制:  1000ms 内存限制:  65536kB 描述 给定3个整数a.b.c,计算表达式(a+b)/c的值,/是整除运算. 输入 输入仅一行,包括三个整数a.b.c, 数与数之间以一个空格 ...

  2. Linux——搭建PHP开发环境第四步:composer

    原文链接:https://my.oschina.net/jiangbianwanghai/blog/473249 1.下载composer.phar [root#localhost opt]# cur ...

  3. xml格式化写入文件

    参考xml文件地址:http://cloudprint.cainiao.com/template/standard/101 代码: package main import ( "encodi ...

  4. [BZOJ 2724] [Violet 6] 蒲公英 【分块】

    题目链接:BZOJ - 2724 题目分析 这道题和 BZOJ-2821 作诗 那道题几乎是一样的,就是直接分块,每块大小 sqrt(n) ,然后将数字按照数值为第一关键字,位置为第二关键字排序,方便 ...

  5. ORACLE PL/SQL异常处理(Exception)学习笔记

    1.PL/SQL错误类型 错误类型 报告者 处理方法 编译时错误 PL/SQL编译器 交互式地处理:编译器报告错误,你必须更正这些错误 运行时错误 PL/SQL运行时引擎 程序化地处理:异常由异常处理 ...

  6. Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条

    Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条 异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通常会使用方法 Ap ...

  7. 【HDOJ】1760 A New Tetris Game

    博弈,主要是求SG值.终于做出点儿感觉. /* 1760 */ #include <cstdio> #include <cstring> #include <cstdli ...

  8. 【HDOJ】2364 Escape

    bfs.题目做的不细心,好多小错误.尤其注意起始点就是边界的情况.wa了八次. #include <iostream> #include <cstdio> #include & ...

  9. Unity 用C#脚本读取JSON文件数据

    读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...

  10. linux 里 /etc/passwd 、/etc/shadow和/etc/group 文件内容解释

    •/etc/passwd文件用于存放用户账户信息,每行代表一个账户,每个账户的各项信息用冒号分割,例如: root:x:::root:/root:/bin/bash username:password ...