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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章