fenwick tree

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. O(1){O(1)}
p + v Returns the point obtained by moving p by v. O(1){O(1)}
b - a Returns the vector from a to b. O(1){O(1)}
dot(a, b) Returns the dot product of a and b. O(1){O(1)}
cross(a, b) Returns the cross product of a and b. O(1){O(1)}

Dot product

a⋅b=axbx+ayby=∣a∣ ∣b∣cos⁡θa \cdot b = a_x b_x + a_y b_y = |a|\,|b|\cos\theta

For nonzero vectors, θ\theta is the smaller angle between them. The dot product is positive below 90∘90^\circ, zero at 90∘90^\circ, and negative above 90∘90^\circ.

a b θ negativepositive zero on this line

dot(v, v) is the squared length of v. For the squared distance from a to b, use v = b - a.

Cross product

a×b=axby−aybx=∣a∣ ∣b∣sin⁡θa \times b = a_x b_y - a_y b_x = |a|\,|b|\sin\theta

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.

ab θ positivenegative zero on this line

cross(a, b) is the signed area of the parallelogram spanned by aa and bb.

ab area = |a × b|

To tell which side of a line a third point is on, use Orientation.