POJ 2007 Scrambled Polygon 凸包
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 10Sample 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 凸包的更多相关文章
- POJ 2007 Scrambled Polygon [凸包 极角排序]
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8636 Accepted: 4105 ...
- POJ 2007 Scrambled Polygon 凸包点排序逆时针输出
题意:如题 用Graham,直接就能得到逆时针的凸包,找到原点输出就行了,赤果果的水题- 代码: /* * Author: illuz <iilluzen[at]gmail.com> * ...
- POJ 2007 Scrambled Polygon 极角序 水
LINK 题意:给出一个简单多边形,按极角序输出其坐标. 思路:水题.对任意两点求叉积正负判断相对位置,为0则按长度排序 /** @Date : 2017-07-13 16:46:17 * @File ...
- poj 2007 Scrambled Polygon(极角排序)
http://poj.org/problem?id=2007 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6701 A ...
- ●POJ 2007 Scrambled Polygon
题链: http://poj.org/problem?id=2007 题解: 计算几何,极角排序 按样例来说,应该就是要把凸包上的i点按 第三像限-第四像限-第一像限-第二像限 的顺序输出. 按 叉积 ...
- 简单几何(极角排序) POJ 2007 Scrambled Polygon
题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...
- POJ 2007 Scrambled Polygon(简单极角排序)
水题,根本不用凸包,就是一简单的极角排序. 叉乘<0,逆时针. #include <iostream> #include <cstdio> #include <cs ...
- POJ 2007 Scrambled Polygon (简单极角排序)
题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...
- poj 2007 Scrambled Polygon 极角排序
/** 极角排序输出,,, 主要atan2(y,x) 容易失精度,,用 bool cmp(point a,point b){ 5 if(cross(a-tmp,b-tmp)>0) 6 retur ...
随机推荐
- C#☞软件设计模型_基础
建模图有助于理解.阐明和传达代码的构思和软件系统必须支持的用户需求. 若要描述和传达用户需求,您可以使用统一建模语言 (UML) 用例图.活动图.类图和序列图. 若要描述和传达系统的功能,您可以使用 ...
- ios CAF音频转换为MP3
Url:http://blog.csdn.net/ysy441088327/article/details/7392842#reply 为了达到 iPhone 与 Android 实现音频互通. 那么 ...
- 造成session丢失的原因和解决方法
win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程, 造成保存在该进程中的session丢失. 因为Session,Appl ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
- GO:格式化代码
http://www.ituring.com.cn/article/39380 Go 开发团队不想要 Go 语言像许多其它语言那样总是在为代码风格而引发无休止的争论,浪费大量宝贵的开发时间,因此他们制 ...
- jQuery取CSS的HEX(16位)颜色值
//IE10以上版本.CHROME.FIREFOX中GRB颜色转HEX颜色值 $.fn.getBackgroundColor = function() { var rgb = $(this).css( ...
- 转:PHP如何阻止用户上传成人照片或者裸照
原文来自于:http://www.hackbase.com/tech/2014-10-12/69367.html 当前,网络淫秽色情信息屡禁不止.屡打不绝,严重危害未成年人身心健康,严重败坏社会风气, ...
- Automated Telephone Exchange
Time Limit: 3000MS Memory limit: 65536K 题目描述In St Petersburg phone numbers are formatted as “XXX–XX– ...
- 设计模式之装饰者模式(Decorator Pattern)
一.什么是装饰者模式? 装饰者模式能够完美实现“对修改关闭,对扩展开放”的原则,也就是说我们可以在不修改被装饰者的前提下,扩展被装饰者的功能. 再来看看我们的文件操作代码: 1 InputStream ...
- .net 开发框架
.NET开发人员值得关注的七个开源项目 2010年07月02日09:33 it168网站原创 作者:黄永兵 编译 编辑:胡铭娅 我要评论(0) [IT168技术分析]微软近几年在.NET社区开源项目方 ...

