public static boolean isClockwise(List pointList) {
double maxY = 0.0D;
int index = 0;
//找到Y值最大的点及其前一点和后一点
for (int i = 0; i < pointList.size(); i++) {
PointD pt = (PointD) pointList.get(i);
if (i == 0) {
maxY = pt.Y;
index = 0;
continue;
}
if (maxY < pt.Y) {
maxY = pt.Y;
index = i;
}
}
int front = index == 0 ? pointList.size() - 2 : index - 1;
int middle = index;
int after = index + 1;
//利用矢量叉积判断是逆时针还是顺时针。设矢量P = ( x1, y1 ),Q = ( x2, y2 ),则矢量叉积定义为由(0,0)、p1、p2和p1+p2
//所组成的平行四边形的带符号的面积,即:P × Q = x1*y2 - x2*y1,其结果是一个标量。
//显然有性质 P × Q = - ( Q × P ) 和 P × ( - Q ) = - ( P × Q )。
//叉积的一个非常重要性质是可以通过它的符号判断两矢量相互之间的顺逆时针关系:
//若 P × Q > 0 , 则P在Q的顺时针方向。
//若 P × Q < 0 , 则P在Q的逆时针方向。
//若 P × Q = 0 , 则P与Q共线,但可能同向也可能反向。
//解释:
//a×b=(ay * bz - by * az, az * bx - ax * bz, ax * by - ay * bx) 又因为az bz都为0,所以a×b=(0,0, ax * by - ay * bx)
//根据右手系(叉乘满足右手系),若 P × Q > 0,ax * by - ay * bx>0,也就是大拇指指向朝上,所以P在Q的顺时针方向,一下同理。
PointD frontPt = (PointD) pointList.get(front);
PointD middlePt = (PointD) pointList.get(middle);
PointD afterPt = (PointD) pointList.get(after);
return (afterPt.X - frontPt.X) * (middlePt.Y - frontPt.Y)
- (middlePt.X - frontPt.X) * (afterPt.Y - frontPt.Y) > 0.0D;
}