解题思路
题意:从第一个点开始逆时针输出组成多边形的点
凸包Graham算法
-
求q中y坐标最小的点p0,若具有最小坐标的点有多个,则取最左边的点作为po.
-
对q中剩余的点按逆时针相对p0的极角排序(也就是按极角从小到大排序)
-
打印按逆时针排列的栈中的各顶点
这道题已经告诉我们p0就是(0,0)了,就直接做就好了。
代码
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
int n=1;
struct c{
int x,y;
}a[55];
bool cmp(c l,c r)//按极角排序
{
return (l.x-a[1].x)*(r.y-a[1].y)-(l.y-a[1].x)*(r.x-a[1].y)>0;
}
int main(){
while(scanf("%d%d",&a[n].x,&a[n].y)!=EOF)
n++;
n--;
sort(a+2,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
printf("(%d,%d)\n",a[i].x,a[i].y);
}
}