这里可以引用算法(http://algs4.cs.princeton.edu/91primitives/)和此处(http://www.geeksforgeeks.org/orientation-3-ordered-points/),
尝试以下代码:p1 = {0,0},p2 = {4,4},p3 = {0,3}或p1 = {0,0},p2 = {4,4},p3 = {0,5},我认为两种情况都应该是时钟方式,但算法输出逆时针方式。
// A C++ program to find orientation of three points
#include <iostream>
using namespace std;
struct Point
{
int x, y;
};
// To find orientation of ordered triplet (p1, p2, p3).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p1, Point p2, Point p3)
{
// See 10th slides from following link for derivation
// of the formula
int val = (p2.y - p1.y) * (p3.x - p2.x) -
(p2.x - p1.x) * (p3.y - p2.y);
cout << val << endl;
if (val == 0) return 0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
// Driver program to test above functions
int main()
{
Point p1 = {0, 0}, p2 = {4, 4}, p3 = {0, 3};
int o = orientation(p1, p2, p3);
if (o==0) cout << "Linear";
else if (o == 1) cout << "Clockwise";
else cout << "CounterClockwise";
return 0;
}