Point 683c0d0f
2D point/vector primitive. Foundation for all other geometry snippets — every geometry file in this notebook #includes this. cross(p) is the z-component of the 3D cross product (positive if p is counterclockwise from *this); cross(a,b) is the same but with *this as the pivot (positive if a->b turns left as seen from here).
Time: O(1) per operation. tested
content/geometry/point.h
struct Point {
ld x, y;
Point(ld x = 0, ld y = 0) : x(x), y(y) {}
bool operator==(Point p) const { return tie(x, y) == tie(p.x, p.y); }
Point operator+(Point p) const { return Point(x + p.x, y + p.y); }
Point operator-(Point p) const { return Point(x - p.x, y - p.y); }
Point operator*(ld d) const { return Point(x * d, y * d); }
Point operator/(ld d) const { return Point(x / d, y / d); }
ld dist2() const { return x * x + y * y; }
ld dist() const { return sqrtl(dist2()); }
ld dot(Point p) const { return x * p.x + y * p.y; }
ld angle() const { return atan2(y, x); }
ld cross(Point p) const { return x * p.y - y * p.x; }
ld cross(Point a, Point b) const { return (a - *this).cross(b - *this); }
Point unit() const { return *this / dist(); }
Point rotate(ld a) const { return Point(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); }
friend ostream &operator<<(ostream &os, Point p) { return os << p.x << " " << p.y; }
};