1. 程式人生 > 實用技巧 >C - Clockwise or Counterclockwise

C - Clockwise or Counterclockwise

https://vjudge.net/contest/389195#problem/C

It is preferrable to read the pdf statment.

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 pointsA(x1,y1),B(x2,y2),C(x3,y3)which lie on a circle centered atO(0,0). Imagine starting fromA, he draws the circle acrossBand finally getsC. Determine whether he is drawing clockwise or counterclockwise.

InputThe first line contains an integerT(1T1000), denoting the number of test cases.


In the nextTlines, each line contains six space-separated integersx1,y1,x2,y2,x3,y3(109x1,y1,x2,y2,x3,y3109) denoting the coordinate ofA,BandC.

It is guaranteed thatA,B,Care pairwise distinct and|AO|=|BO|=|CO|>0.
OutputFor each test case, output one line containing ''Clockwise'' or ''Counterclockwise''.Sample Input

3
1 2 2 1 -1 -2
4 3 -4 3 3 4
4 -3 4 3 3 4

Sample Output

Clockwise
Clockwise
Counterclockwise

題意:

  給三個點A,B,C,三點到原點距離相等,從A到B再經過C來形成圓,問該圓是逆時針還是順時針形成。

思路:

  求向量

  內積>0 為順時針形成

  內積<0 為逆時針形成

(原本還考慮了用參考點的左右相對位置關係來做太麻煩

程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=2007;
//const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;


ll x[4] = {0};
ll y[4] = {0};
ll S(int a , int b , int c)
{
	ll s = x[a]*y[b] - y[a]*x[b] + x[b]*y[c] - y[b]*x[c] + x[c]*y[a] - y[c]*x[a];
	return s;
}
int main()
{
	int T = 0;
	cin >> T;

	while(T--)
	{
		cin >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3];
		bool f = S(1,2,3) > 0;
		if(!f)
		{
		 	cout << "Clockwise" <<endl;
		}
		else
		{
			cout << "Counterclockwise" << endl;
		}
	}
	return 0;
}