POJ 2007 Scrambled Polygon 凸包

时间:2023-01-29 16:53:58
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".) 
POJ 2007 Scrambled Polygon 凸包
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. 
POJ 2007 Scrambled Polygon 凸包 
POJ 2007 Scrambled Polygon 凸包

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 &lbrack;凸包 极角排序&rsqb;

    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. 简单几何&lpar;极角排序&rpar; 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. 读书笔记--SQL必知必会11--使用子查询

    11.1 子查询 查询(query),任何SQL语句都是查询.但此术语一般指SELECT语句. SQL还允许创建子查询(subquery),即嵌套在其他查询中的查询. 作为子查询的SELECT语句只能 ...

  2. Autofac

    程序集准备 Assembly: Autofac/Autofac.Integration.Mvc/System.Web.Mvc/System.Web.Helpers/System.Web.WebPage ...

  3. SQL注入的字符串连接函数

    在select数据时,我们往往需要将数据进行连接后进行回显.很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数.在sqli中,常见的字符串连接函数有concat(),group_ ...

  4. C&num;读txt文件并写入二维数组中(txt数据行,列未知)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. android button 函数调用栈

    Button button=(Button) findViewById(R.id.button);button.setOnClickListener(new Button.OnClickListene ...

  6. &lpar;译&rpar; Angular运行原理揭秘 Part 1

    当你用AngularJS写的应用越多, 你会越发的觉得它相当神奇. 之前我用AngularJS实现了相当多酷炫的效果, 所以我决定去看看它的源码, 我想这样也许我能知道它的原理. 下面是我从源码中找到 ...

  7. Angularjs基础教程

    Angularjs-基础教程 一些angualr的基础概念,适合入门. 1.下载 推荐 bower 或 npm 安装. bower install angular bower install angu ...

  8. Json安全

    1.不要使用*JSON数组,避免被<script>标签引用. 2.使用POST+密钥获取JSON,尽量不要用GET方式. 3.不要使用eval()将对象放入内存,eval()会执行所传入 ...

  9. Confluence 6 SQL 异常的问题解决

    如果你得到了与下面显示内容类似的信息话,那么你最好考虑修改 Confluence 的日志级别输出更多的信息.如果你考虑通过 Atlassian support 获得帮助,那么这些详细的错误信息能够更好 ...

  10. 滑动条QSlider

    QSlider只提供整数范围 滑块接受Tab键的焦点,并同时提供了一个鼠标滚轮和键盘接口.键盘接口如下: Left/Right 移动水平滑块一个步长.Up/Down 移动垂直滑块一个步长.PageUp ...