fzu 2035 Axial symmetry 判轴对称多边形

Description

Axial symmetry is so beautiful. We can find many axial symmetric objects in everyday life. Following are some axial symmetric figures.

Now, you are given a simple polygon. A simple polygon is a closed polygonal chain of line segments in the plane which do not have points in common other than the common vertices of pairs of consecutive segments.

To simplify the problem, the given simple polygon in this problem is special. All edges of the given polygon parallel to either X-axis or Y-axis. Your task is to examine whether the given polygon is axial symmetric.

Input

The first line of the input contains an integer T(T≤50), indicating the number of test cases. Each case begins with one integer n(10≤n≤500), the number of points. The next n lines indicate the points of the polygon, each with two integers x(-100,000≤x≤100,000) and y(-100,000≤y≤100,000). The points would be given either clockwise or counterclockwise.

Output

For each test case, print a line containing the test case number (beginning with 1) and if the polygon is axial symmetric, please output “YES”, or you should output “NO”.

Sample Input

2
4
0 0
0 1
1 1
1 0
6
0 0
4 0
4 2
1 2
1 4
0 4

Sample Output

Case 1: YES
Case 2: NO

思路:

首先将每条线段的中点按顺序插入顶点数组中。由于顶点是按顺时针或逆时针给出的,所以对称轴一定是i和i+n,枚举i即可

判断对称依据:当两点关于线段对称时,这两点分别到线段两点的距离是相等的。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 10;
int t, n, sn;

struct Point {
    double x, y;
} point[MAXN];

bool Equal(Point a, Point b, Point c, Point d) {
    return abs(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) - (pow(c.x - d.x, 2) + pow(c.y - d.y, 2))) < eps;
}

bool IsRight() {
    for (int i = 1; i <= n; ++i) {
        int j = i + n;
        bool yes = true;
        for (int k = i + 1, u = i - 1; k < j; ++k, --u) {
            if (u < 1) u = sn;
            if (!Equal(point[i], point[k], point[i], point[u]) || !Equal(point[j], point[k], point[j], point[u])) {
                yes = false;
                break;
            }
        }
        if (yes) return true;
    }
    return false;
}

int main() {
#ifdef NIGHT_13
    freopen("in.txt", "r", stdin);
#endif
    scanf("%d", &t);
    int cas = 0;
    while (t--) {
        scanf("%d", &n);
        sn = n * 2;
        for (int i = 1; i <= sn; i += 2) {
            scanf("%lf%lf", &point[i].x, &point[i].y);
        }
        for (int i = 2; i <= sn; i += 2) {
            point[i].x = (point[i - 1].x + point[(i + 1) > sn ? 1 : (i + 1)].x) / 2;
            point[i].y = (point[i - 1].y + point[(i + 1) > sn ? 1 : (i + 1)].y) / 2;
        }
        printf("Case %d: %s\n", ++cas, IsRight() ? "YES" : "NO");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值