Skip to content

Instantly share code, notes, and snippets.

@tooshitaka
Created March 24, 2017 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tooshitaka/23b177689a1f582bb8bb76bc47872186 to your computer and use it in GitHub Desktop.
Save tooshitaka/23b177689a1f582bb8bb76bc47872186 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#define EPS (1e-10)
#define equals(a, b) (fabs(a) - (b)) < EPS)
using namespace std;
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) :x(x), y(y) {}
Point operator + (Point p) { return Point(x + p.x, y + p.y); }
Point operator - (Point p) { return Point(x - p.x, y - p.y); }
Point operator * (double s) { return Point(x * s, y * s); }
Point operator / (double s) { return Point(x / s, y / s); }
double abs() {
return sqrt(norm());
}
double norm() {
return x * x + y * y;
}
bool operator < (const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
bool operator == (const Point &p) const {
return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
}
};
typedef Point Vector;
double cross(Vector a, Vector b) {
return a.x * b.y - a.y * b.x;
}
double dot(Vector a, Vector b) {
return a.x*b.x + a.y * b.y;
}
const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;
// Calculate the relation of points
int ccw(Point p0, Point p1, Point p2)
{
Vector a = p1 - p0;
Vector b = p2 - p0;
if (cross(a, b) > EPS) return COUNTER_CLOCKWISE;
if (cross(a, b) < -EPS) return CLOCKWISE;
if (dot(a, b) < -EPS) return ONLINE_BACK;
if (a.norm() < b.norm()) return ONLINE_FRONT;
return ON_SEGMENT;
}
int main() {
// Input
double x, y;
cin >> x >> y;
Point p0(x,y);
cin >> x >> y;
Point p1(x, y);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
cin >> x >> y;
Point p2(x, y);
// CCW
switch (ccw(p0,p1,p2))
{
case -1: cout << "CLOCKWISE" << endl;
break;
case 1: cout << "COUNTER_CLOCKWISE" << endl;
break;
case -2: cout << "ONLINE_FRONT" << endl;
break;
case 2: cout << "ONLINE_BACK" << endl;
break;
default:
cout << "ON_SEGMENT" << endl;
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment