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;}

拨云见日,未来可期。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43899069/article/details/107989788