Point
Represents a point or vector in the plane.
Implementation
template<class T>
struct Point {
T x{}, y{};
Point operator+(Point p) const { return {x + p.x, y + p.y}; }
Point operator-(Point p) const { return {x - p.x, y - p.y}; }
};
template<class T>
T dot(Point<T> a, Point<T> b) { return a.x * b.x + a.y * b.y; }
template<class T>
T cross(Point<T> a, Point<T> b) { return a.x * b.y - a.y * b.x; }
Interface
Coordinates default to zero.
| Operation | Description | Time |
|---|---|---|
Point<T>{x, y} |
Creates a point with coordinates x and y. |
|
p + v |
Returns the point obtained by moving p by v. |
|
b - a |
Returns the vector from a to b. |
|
dot(a, b) |
Returns the dot product of a and b. |
|
cross(a, b) |
Returns the cross product of a and b. |
Dot product
For nonzero vectors, is the smaller angle between them. The dot product is positive below , zero at , and negative above .
dot(v, v) is the squared length of v. For the squared distance from a to b, use v = b - a.
Cross product
cross(a, b) is positive if the shorter turn from a to b is counterclockwise. It is negative if it’s clockwise. Collinear vectors give zero.
cross(a, b) is the signed area of the parallelogram spanned by and .
To tell which side of a line a third point is on, use Orientation.