poj 2007 Scrambled Polygon 凸包点排序逆时针输出
来源:互联网 发布:单片机呼吸灯原理图 编辑:程序博客网 时间:2024/05/16 11:35
求一多边形按逆时针顺序输出
用graham算法可以直接对其按逆时针排序
然后找到原点,把从原点开始把数组输出一遍
#include <stdio.h> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define sqr(a) ((a) * (a)) #define dis(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)) const int MAXN = 110; const double PI = acos(-1.0); struct Point { int x; int y; Point(double a = 0, double b = 0) : x(a), y(b) {} friend bool operator < (const Point &l, const Point &r) { return l.y < r.y || (l.y == r.y && l.x < r.x); } } p[MAXN], ch[MAXN]; double mult(Point a, Point b, Point o) { return (a.x - o.x) * (b.y - o.y) >= (b.x - o.x) * (a.y - o.y); } int Graham(Point p[], int n, Point res[]) { int top = 1; sort(p, p + n); if (n == 0) return 0; res[0] = p[0]; if (n == 1) return 0; res[1] = p[1]; if (n == 2) return 0; res[2] = p[2]; int i; for (i = 2; i < n; i++) { while (top && (mult(p[i], res[top], res[top - 1]))) top--; res[++top] = p[i]; } int len = top; res[++top] = p[n - 2]; for (i = n - 3; i >= 0; i--) { while (top != len && (mult(p[i], res[top], res[top - 1]))) top--; res[++top] = p[i]; } return top; } int n; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);#endif while (scanf("%d%d", &p[n].x, &p[n].y) != EOF) n++; n = Graham(p, n, ch); int t,i; for (i = 0; i < n; i++) if (ch[i].x == 0 && ch[i].y == 0) { t = i; break; } for (i = t; i < n; i++) printf("(%d,%d)\n", ch[i].x, ch[i].y); for (i = 0; i < t; i++) printf("(%d,%d)\n", ch[i].x, ch[i].y); return 0; }
0 0
- POJ 2007 Scrambled Polygon 凸包点排序逆时针输出
- poj 2007 Scrambled Polygon 凸包点排序逆时针输出
- POJ 2007 || Scrambled Polygon(逆时针输出凸包顶点,以原点为起始点
- poj 2007 Scrambled Polygon--按极角排序
- poj 2007 Scrambled Polygon 极角排序
- POJ 2007 Scrambled Polygon(极角排序)
- POJ 2007 Scrambled Polygon 极角排序
- poj 2007 Scrambled Polygon [极角排序]
- POJ 2007 Scrambled Polygon 极角排序
- poj 2007 Scrambled Polygon 【极角排序】
- POJ 2007 Scrambled Polygon(极角排序)
- POJ 2007 Scrambled Polygon (极角排序)
- poj 2007 Scrambled Polygon
- poj 2007 Scrambled Polygon
- POJ 2007 Scrambled Polygon
- POJ 2007 Scrambled Polygon
- POJ 2007 Scrambled Polygon
- POJ 2007 Scrambled Polygon
- EJB(四)JPA 分布式事务处理
- awk:语法命令学习
- 关于时间管理的一些沉淀
- android 获取联系人
- Android 关于Bitmap对象于内存管理
- poj 2007 Scrambled Polygon 凸包点排序逆时针输出
- 僵尸进程和孤儿进程的区别
- 原来Github上的README.md文件这么有意思——Markdown语言详解
- 崔壽城 猿亭 敗船之喩
- 网页按钮css
- 学习Golang语言(1)
- Apache Commons-logging使用实例
- linux inode已满解决方法
- [: 3: y: unexpected operator问题