点 {A,B,C,D} 形成 4 个三角形:ABC、ABD、ACD 和 BCD。计算它们的方向会产生一个 4 位二进制签名,该签名确定凸包的顺序如下:
signature key: "+" for counterclockwise, "-" for clockwise
A A A B
B B C C convex hull, in counterclockwise order:
C D D D
+ + + + quadrilateral ABCD
+ + + - triangle ABD (C is internal)
+ + - + triangle ABC (D is internal)
+ + - - quadrilateral ABDC
+ - + + triangle BCD (A is internal)
+ - + - (should not happen)
+ - - + quadrilateral ADBC
+ - - - triangle ADC (B is internal)
(inverting all signature bits reverses hull orientation)
- + + + triangle CDA (B is internal)
- + + - quadrilateral CBDA
- + - + (should not happen)
- + - - triangle DCB (A is internal)
- - + + quadrilateral CDBA
- - + - triangle CBA (D is internal)
- - - + triangle DBA (C is internal)
- - - - quadrilateral DCBA
您可以计算任何给定三角形的方向(顺时针或逆时针),如下所示:
struct Point {
float x, y;
Point(float xx,float yy):x(xx),y(yy){}
};
Point operator+(Point A, Point B) { return Point(A.x+B.x,A.y+B.y); }
Point operator-(Point A, Point B) { return Point(A.x-B.x,A.y-B.y); }
float orientation(Point A, Point B, Point C) {
Point AB = B - A;
Point AC = C - A;
return AB.x*AC.y - AB.y*AC.x; // 2-D equivalent to the 3-D cross-product
}
如果三角形 ABC 是逆时针的,该函数orientation(A,B,C)
返回一个正值,如果它是顺时针的,该函数返回一个负值,如果它是退化的,则返回零。(如果你的坐标系是左手而不是右手,那么顺时针换成逆时针,但这并不重要......)