9行代码AC——HDU 6857 -Clockwise or Counterclockwise(2020 Multi-University Training Contest 8)(判断三点顺序)
勵志用盡量少的代碼做高效表達
Problem Description
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 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 (?10^9≤x1,y1,x2,y2,x3,y3≤10 ^9) 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’’.
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
知識儲備
解此題所用到的基本知識是:數學幾何——向量叉乘。
相關知識為:
1、傳送門1——>高中必修四第二章平面幾何(基礎)
2、傳送門2——>點乘、叉乘在編程中的作用(涉及行列式)
簡單講一下叉乘:
一、向量的叉積:已知向量a=(x1,y1); 向量b=(x2,y2); 則a×b= x1*y2-x2*y1
二、叉積的結果也是一個向量,是垂直于向量a,b所形成的平面,如果看成三維坐標的話是在 z 軸上,上面結果是它的模。
三、方向判定:右手定則:
1、四指指向x向量(右手垂直于平面)
2、四指朝y向量彎曲(注意彎曲方向的夾角要小于180°)
3、大拇指指向為a*b的方向
如圖所示:
如下圖所示,由右手定則可知,若我們將x向量看做AB,y向量看做AC,根據手指的方向可得,首先經過B點要做逆時針運動,而大拇指朝上代表叉乘的結果大于0(因為如果結果為正,則向量在Z軸的正半軸),因此可得:當叉乘結果大于0時,做逆時針;反之做順時針。
再來看題:
1、設三點坐標為:A:(x1,y1); B(x2,y2); C(x3,y3);
2、得到AB向量等于(x2-x1, y2-y1); AC向量等于(x3-x1, y3-y1)
3、將兩個向量帶入叉乘公式,若結果小于零,則需逆時針,若結果大于零,則需順時針
4、編程
代碼展示:
#include<stdio.h> int main() {int T; scanf("%d", &T); while(T--) {double xa, ya, xb, yb, xc, yc;scanf("%lf%lf%lf%lf%lf%lf", &xa,&ya,&xb,&yb,&xc,&yc);double num = (xb-xa)*(yc-ya)-(yb-ya)*(xc-xa);printf(num>0?"Counterclockwise\n":"Clockwise\n");} return 0;}撥云見日,未來可期。
總結
以上是生活随笔為你收集整理的9行代码AC——HDU 6857 -Clockwise or Counterclockwise(2020 Multi-University Training Contest 8)(判断三点顺序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 约瑟夫环递推公式的由来(约瑟夫环公式法)
- 下一篇: 矩阵快速幂(教主传授)