平面上顺序三点 A(x1,y1),B(x2,y2),C(x3,y3)
,则三角形两边的向量为:
AB = (x2-x1, y2-y1), AC = (x3-x1, y3-y1)
两向量叉积为:
|x2-x1, y2-y1|
|x3-x1, y3-y1|
值为:(x2-x1)*(y3-y1) - (y2-y1)*(x3-x1)
通过右手法则进行判断:
若AB*AC > 0
,则三点 A B C 是逆时针的
若AB*AC < 0
,则三点 A B C 是顺时针的
若AB*AC = 0
,则三点共线
🎉例题 oj1029
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int x3 = sc.nextInt();
int y3 = sc.nextInt();
// 向量AB
int pabX = x2 - x1;
int pabY = y2 - y1;
// 向量BC
int pbcX = x3 - x2;
int pbcY = y3 - y2;
// 两向量叉乘可以判断选择方向, >0 逆时针, <0 顺时针, =0 共线
int res = pabX*pbcY - pabY*pbcX; //叉乘
if (res > 0) System.out.println("Anticlockwise");
else if (res < 0) System.out.println("Clockwise");
else System.out.println("Collineation");
}
}
}