diff --git a/algorithms/geometry/geometry_functions.cpp b/algorithms/geometry/geometry_functions.cpp index 2fc624ce179c246bcd322d16a20fb98f6743ae9e..1e2a51adc0a807b5e44d9d96a9084f86a7f4dda3 100644 --- a/algorithms/geometry/geometry_functions.cpp +++ b/algorithms/geometry/geometry_functions.cpp @@ -6,13 +6,14 @@ template <typename T> struct Point { T x, y; + Point() {} Point(T x, T y) : 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); } T dot(Point p) { return (x*p.x) + (y*p.y); } - T cross(Point p) { return (x*p.y) - (y*p.y); } + T cross(Point p) { return (x*p.y) - (y*p.x); } // Returns angle between this and p: // atan2(y, x) is in the range [-180,180]. To get [0, 360], @@ -31,6 +32,13 @@ struct Point { return (cross(p) / (sqrt(dot(*this))*sqrt(p.dot(p)))); } + bool inside_triagle(Point a, Point b, Point c) { + bool c1 = (*this - b).cross(a - b) < 0; + bool c2 = (*this - c).cross(b - c) < 0; + bool c3 = (*this - a).cross(c - a) < 0; + return c1 == c2 && c1 == c3; + } + // Finds orientation of ordered triplet (a,b,c). // Colinear (0), Clockwise (1), Counterclockwise (2) static int orientation(Point a, Point b, Point c) {