2020牛客暑期多校训练营(第三场)C 计算几何

Operation Love

题目链接:https://ac.nowcoder.com/acm/contest/5668/C

题目描述:

一个手掌图形,共有20个点,通过按顺时针或逆时针的顺序给定这20个点的坐标,判断是左手还是右手。左手和右手是对称的,给定的点集可能是这个手掌图形旋转平移后的结果,但手掌图形的大小不会改变。

思路:

读题后很容易发现手掌底部这条线段的长度为9是固定的,并且相邻的拇指和小指的边的长度不一样,这样就很容易能判断出来给定的手掌是右手还是左手,唯一缺少的条件是如何判断这些点是顺时针还是逆时针给你的。

鞋带公式可以判断这个问题:通过鞋带公式可以以点的坐标求出这个多边形的面积,当这个面积计算结果为负数为,说明给出的点集是以顺时针遍历的,反之则为逆时针。

还需要注意的是这题eps开到0.1就够用了

代码:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e6;
const double PI = acos(-1.0);
const double eps = 0.1;
#define Test ll tesnum;tesnum = read();while(tesnum--)
ll read();
struct node
{
    double x,y;
}point[N];
int find()
{
    point[20].x = point[0].x;
    point[20].y = point[0].y;
    double ans = 0;
    for(int i = 0; i < 20; i++){
        ans+=0.5*(point[i].x+point[i+1].x)*(point[i+1].y-point[i].y);
    }
    if(ans<0){
        return 1;//顺
    }else{
        return 0;//逆
    }
}
double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    Test{
        for(int i = 0; i < 20; i++){
            cin>>point[i].x>>point[i].y;
        }
        int fl = find();
        bool f = false;//右手
        for(int i = 0; i < 20; i++){
            if(fabs(dis(point[i],point[(i+1)%20])-9.0)<=eps){
                node x = point[i];
                node y = point[(i+1)%20];
                node z = point[(i+2)%20];
                if((fl==1&&fabs(dis(y,z)-6.0)<=eps)||(fl==0&&fabs(dis(y,z)-8.0)<=eps)){
                    f = false;
                }
                if((fl==1&&fabs(dis(y,z)-8.0)<=eps)||(fl==0&&fabs(dis(y,z)-6.0)<=eps)){
                    f = true;
                }
                break;
            }

        }
        if(f){
            cout<<"left"<<endl;
        }else
            cout<<"right"<<endl;

    };
    return "BT7274", NULL;
}

inline ll read() {
    ll hcy = 0, dia = 1;char boluo = getchar();
    while (!isdigit(boluo)) {if (boluo == '-')dia = -1;boluo = getchar();}
    while (isdigit(boluo)) {hcy = hcy * 10 + boluo - '0';boluo = getchar();}
    return hcy * dia;
}
原文地址:https://www.cnblogs.com/cloudplankroader/p/13341747.html