// Created by Igor Markov, March 2001 // This program illustrates the direction-test // (left-of aka counter-clock-wise) #include struct Point { double x, y; }; bool counterClockWise(Point p0, Point p1, Point p) // p is the most recent point, p1 is the next, p0 is the earliest point // This tests tell whether p is to the left of the directed segment p0->p1 // equivalent: vector (p-p0) turns counter-clockwise from vector (p1-p0) // (the case of all three points being on the same line is not tested for). // // This can be tested by computing the vector-product of the two vectors: // if it points "up", then the answer is yes, otherwise the answer is no. // Technically, we just need to compute the magnitude of the vector-product // (p-p0) x (p1-p0) which is the determinant of this matrix // ( p1.x-p0.x p1.y-p0.y ) // ( p.x-p0.x p.y-p0.y ) // then we compare the result to zero { return (p1.x-p0.x)*(p.y-p0.y)-(p.x-p0.x)*(p1.y-p0.y) < 0; } int main() { Point ar[4]={ {3,0}, {4,4}, {3,3}, {-1,4} }; cout << clockWise( ar[0], ar[1], ar[2] ) << endl; cout << clockWise( ar[1], ar[2], ar[3] ) << endl; return 0; }