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 ...
随机推荐
- iOS中获取各种文件的目录路径和文件
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...
- Zookeeper的设计模式之观察者模式(十)
Watcher是Zookeeper用来实现distribute lock, distribute configure, distribute queue等应用的主要手段.要监控data_tree上的任 ...
- 导出Excel后其他按钮失效
在SharePoint中,当在页面上点击Export to Excel按钮后,第一次它能实现该功能,当再次点击该按钮时,页面上的所有按钮将失效,仅仅再次刷新该页面时按钮才会有效,首先想到出现该问题肯定 ...
- 关于win8.1“连接被远程计算机关闭”的一种解决方案
我就是连接的时候出现"连接被远程计算机关闭",然后想着可能是win8更新之后网络协议 出问题了,后来无意中发现e信在第一次启动的时候会在网络适配器中会多出很多网卡,其中三个是带感叹 ...
- BZOJ 1503 郁闷的出纳员
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- Grails架设和配置--起步
现在作这些配置有些轻车熟路了.. 因为RAILS ON RUBY和它真的有很多相像的,, 什么DRY,什么约定先于配置这些的概念... 然后,GITHUB上有好文档,可以一步一步的实践.. https ...
- 大众点评试水O2O新模式:实体店试穿,扫描二维码付款 现场取货
在餐饮美食行业取得不错的成绩之后,大众点评将触角延伸到了线下的传统商铺,开始涉足线下商品的 O2O 团购.和传统的线上下单,线下消费的 O2O 模式不同.大众点评的 O2O 团购用户,可在店内试穿后通 ...
- ASP.NET Routing
ASP.NET Routing Other Versions ASP.NET routing enables you to use URLs that do not have to map to ...
- 修改css
.content{ height: 100%; } .con{ border: 1px solid #eeeeee; display: inline-block; width:86.8%; ##修改这 ...
- mysq 日期相减
mysql> desc test200; +---------------+----------+------+-----+---------+-------+ | Field | Type | ...