Simple polygon (judging clockwise and counterclockwise)

Link: https://ac.nowcoder.com/acm/problem/15962

Source: Niuke.com

topic description

To keep all players happy, Nowcoder practice sessions always include some very basic questions. For example:

Given the coordinates of the vertices of a simple polygon in clockwise or counterclockwise direction, please answer whether the polygon is clockwise or counterclockwise.

Enter a description:

The input contains N+1 rows.

The first line contains an integer N denoting the number of vertices of the simple polygon.

In the following N lines, the i-th line contains two integers xi, yi, denoting the coordinates of the i-th vertex in the simple polygon.

Output description:

If simple polygons are given in clockwise order, output "clockwise" (without quotes) on one line. Otherwise, print "counterclockwise'' (without quotes).

At first, I tried to use my own method to solve the problem, using the idea of ​​coordinate change to solve the problem, but it took a lot of effort and I only got 77 points. Later I learned that there is a mathematical principle in it, to be precise, it is a knowledge point in the field of computer graphics. As long as you know this knowledge point, you can solve this problem soon.

Knowledge points:

For three points on the plane (x0,y0), (x1,y1), (x2,y2). (x1, y1) forms a vector with the previous vertex (x0, y0)

(x1-x0, y1-y0), and the latter vertex (x2, y2) form a vector (x2-x1, y2-y1). Calculate the following determinant to get a=(x1-x0)*(y2-y1)-(x2-x1)*(y1-y0). If a is positive, it is counterclockwise; if a is negative, it is clockwise. For general simple polygons, the above values ​​need to be calculated for each point of the polygon. If there are more positive values, it will be counterclockwise; if there are more negative values, it will be clockwise.

Code for 100 points:

# include <iostream>
# include <algorithm>

using namespace std;

const int maxn = 50;
int N;
int x[maxn];
int y[maxn];
int ans = 0;

int main()
{
    cin >> N;
    int x1, y1, x2, y2;
    for (int i = 1; i <= N; i++) {
        cin >> x[i] >> y[i];
        if (i >= 3) {
            //选取x[i-1]为中心点
            x1 = x[i - 1] - x[i - 2];
            y1 = y[i - 1] - y[i - 2];
            //(x1,y1)为该点与前一个点(前一个点指向该点)构成的向量
            x2 = x[i] - x[i - 1];
            y2 = y[i] - y[i - 1];
            //(x2,y2)为该点与后一个点(该点指向后一个点)构成的向量
            if (x1 * y2 - x2 * y1 > 0) {
                //大于0,为逆时针
                ans--;
            }
            else {
                //小于0,为顺时针
                ans++;
            }
        }
    }
    if (ans > 0) {
        cout << "clockwise" << endl;
    }
    else {
        cout << "counterclockwise" << endl;
    }
    return 0;
}

If you don't know this knowledge point, it will be more difficult to solve this problem. It can be seen that it is very important to pay attention to accumulating some knowledge to solve some problems. The above is my opinion, and I am happy to share it with you.

おすすめ

転載: blog.csdn.net/CXR_XC/article/details/129533620