HDU5533 Dancing Stars on Me(计算几何)

Problem Description
The sky was brushed clean by the wind and the stars were cold in a black sky. What a wonderful night. You observed that, sometimes the stars can form a regular polygon in the sky if we connect them properly. You want to record these moments by your smart camera. Of course, you cannot stay awake all night for capturing. So you decide to write a program running on the smart camera to check whether the stars can form a regular polygon and capture these moments automatically.

Formally, a regular polygon is a convex polygon whose angles are all equal and all its sides have the same length. The area of a regular polygon must be nonzero. We say the stars can form a regular polygon if they are exactly the vertices of some regular polygon. To simplify the problem, we project the sky to a two-dimensional plane here, and you just need to check whether the stars can form a regular polygon in this plane.


Input
The first line contains a integer T indicating the total number of test cases. Each test case begins with an integer n, denoting the number of stars in the sky. Following n lines, each contains 2 integers xi,yi, describe the coordinates of n stars.

1≤T≤300
3≤n≤10010000≤xi,yi≤10000
All coordinates are distinct.


Output
For each test case, please output "`YES`" if the stars can form a regular polygon. Otherwise, output "`NO`" (both without quotes).


Sample Input
3
3
0 0
1 1
1 0
4
0 0
0 1
1 0
1 1
5
0 0
0 1
0 2
2 2
2 0


Sample Output
NO
YES
NO

题目讲的是给你n个点,然后问你能不能构成一个正多边形。
极角排序一下,然后计算每条边和每一个角,全部比较一遍即可。
极角排序:给定平面上的一些点,把它们按照一个选定的中心点排成顺(逆)时针。方法是把每个点于中心点转化成向量,通过判断叉积的正负来判断点的相对位置。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 1e5+5;
using namespace std;

inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
struct point{
    int x,y;
}p[maxn];
int cross(point p0,point p1,point p2){ 
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(point p1,point p2){ //极角排序
    int tmp=cross(p[0],p1,p2);
    if(tmp>0){
        return true;
    }
    return false;
}
double getl(point A,point B){ //计算边长
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
double getAngle(point A,point B,point C){ //计算角度
    double a=getl(B,C);
    double b=getl(A,C);
    double c=getl(A,B);
    double cosA=(b*b+c*c-a*a)/(b*c*2);
    return cosA;
}
double len[maxn];
double angle[maxn];
int main(){
    int Case=read();
    while(Case--){
        int n=read();
        for(int i=0;i<n;++i){
            p[i].x=read(),p[i].y=read();
        }
        sort(p,p+n,cmp); //极角排序
        for(int i=0;i<n;++i){
            len[i]=getl(p[i],p[(i+1)%n]);
            angle[i]=getAngle(p[i],p[(i+1)%n],p[(i+n-1)%n]);
        }
        bool flag=true;
        for(int i=1;i<n;++i){ //比较
            if(fabs(len[i]-len[i-1])>0||angle[i]!=angle[i-1]){
                flag=false;
                break;
            }
        }
        printf("%s\n",flag?"YES":"NO");
    }
}

猜你喜欢

转载自blog.csdn.net/viscu/article/details/69488540