fzu 2035 Axial symmetry 判轴对称多边形

来源:互联网 发布:红辣椒电影数据分析 编辑:程序博客网 时间:2024/04/16 19:10

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

240 00 11 11 060 04 04 21 21 40 4

Sample Output

Case 1: YESCase 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 0
原创粉丝点击