1. 程式人生 > 實用技巧 >2020杭電多校第八場—Clockwise or Counterclockwise(幾何)

2020杭電多校第八場—Clockwise or Counterclockwise(幾何)

After some basic geometric lessons, Cuber QQ has learned that one can draw one and only one circle across three given distinct points, on a 2D plane. Specialized in art, Cuber QQ has shown remarkable skills to draw circle in one stroke, especially when the stroke is done clockwise. He wonder whether he will be able to do that if 3 points has been given. In particular, he is given three distinct points A(x1,y1), B(x2,y2), C(x3,y3) which lie on a circle centered at O(0,0). Imagine starting from A, he draws the circle across B and finally gets C. Determine whether he is drawing clockwise or counterclockwise.

Input

The first line contains an integer T (1 ≤ T ≤ 1 000), denoting the number of test cases. In the next T lines, each line contains six space-separated integers x1, y1, x2, y2, x3, y3 (−109 ≤ x1,y1,x2,y2,x3,y3 ≤ 109) denoting the coordinate of A, B and C. It is guaranteed that A, B, C are pairwise distinct and |AO| = |BO| = |CO| > 0.

Output

For each test case, output one line containing “Clockwise” or “Counterclockwise”.

瞎做的2333

可以看到順時針分為如圖兩種情況:

  1. O點和P3都在P1P2這條有向線段的右側。
  2. O在P1P2左側,P3在右側。

然後用ToLeft判斷一下即可。

#include <bits/stdc++.h>
#define int long long
using namespace std;
//不開long long見祖宗 
struct Point
{
	int x, y;
} p[4];
int Area2(Point p, Point q, Point s)
{
	return p.x * q.y -p.y * q.x
	+ q.x * s.y - q.y * s.x
	+s.x * p.y -s.y * p.x;
}
bool ToLeft(Point p, Point q, Point s){	return Area2(p, q, s) > 0;}
signed main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int x1, y1, x2, y2, x3, y3;
		cin >> p[1].x >> p[1].y >> p[2].x >> p[2].y >> p[3].x >> p[3].y;
		Point temp = {0, 0};
		bool flag1 = ToLeft(p[1], p[2], temp), flag2 = ToLeft(p[2], p[3], temp), flag3 = ToLeft(p[3], p[1], temp), flag4 = ToLeft(p[1], p[2], p[3]);
		if(flag1 == 0 && flag4 == 0 || flag1 == 1 && flag4 == 0) cout << "Clockwise" << endl;
		else cout << "Counterclockwise" << endl;
	}
	return 0;
}