guest@cp-base:~/home/geometry$
templates/geometry-points.cpp
compilable
$cat templates/geometry-points

Geometry Points

Generic 2D point with full operator support: dot, cross, distance, rotation, unit, normal.

[Geometry]|
Jul 9, 2026
|explanatory_notes.md|
#geometry#point#vector#2d#cross-product
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

template <typename T = ll>
struct Point {
    T x, y;

    Point(T x = 0, T y = 0) : x(x), y(y) {}

    Point operator+(const Point &p) const { return {x + p.x, y + p.y}; }
    Point operator-(const Point &p) const { return {x - p.x, y - p.y}; }
    Point operator*(T c) const { return {x * c, y * c}; }
    Point operator/(T c) const { return {x / c, y / c}; }
    bool operator==(const Point &p) const { return x == p.x && y == p.y; }
    bool operator<(const Point &p) const { return tie(y, x) < tie(p.y, p.x); }

    T dot(const Point &p) const { return x * p.x + y * p.y; }
    T cross(const Point &p) const { return x * p.y - y * p.x; }
    T cross(const Point &a, const Point &b) const { return (a - *this).cross(b - *this); }

    T dist() const { return x * x + y * y; }
    T dist(const Point &p) const { return (*this - p).dist(); }
    ld distance() const { return sqrtl((ld)dist()); }
    ld distance(const Point &p) const { return sqrtl((ld)dist(p)); }

    ld angle() const { return atan2l((ld)y, (ld)x); }
    ld angle(const Point &p) const { return atan2l((ld)cross(p), (ld)dot(p)); }

    Point perp() const { return {-y, x}; }
    Point unit() const { T d = dist(); return d ? *this / d : Point(); }
    Point normal() const { return perp().unit(); }

    Point rotate(ld a) const {
        return Point(x * cosl(a) - y * sinl(a), x * sinl(a) + y * cosl(a));
    }
    Point rotate(const Point &p, ld a) const { return (*this - p).rotate(a) + p; }

    friend istream &operator>>(istream &in, Point &p) { return in >> p.x >> p.y; }
    friend ostream &operator<<(ostream &out, const Point &p) { return out << p.x << ' ' << p.y; }
};
42 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)

2D Point Template

A reusable generic point struct with arithmetic, geometric operations, and I/O.

Operations

  • Arithmetic+, -, *, / for vector math

  • Dot productab=axbx+ayby\vec{a} \cdot \vec{b} = a_x b_x + a_y b_y

  • Cross producta×b=axbyaybx\vec{a} \times \vec{b} = a_x b_y - a_y b_x (signed area of parallelogram)

  • 3-point crossp.cross(a, b) = cross product of vectors PA\overrightarrow{PA} and PB\overrightarrow{PB}

  • dist() — squared distance (integer-safe). distance() — actual Euclidean distance (returns long double)

  • angle() — polar angle via atan2l. angle(p) — signed angle between *this and p

  • perp() — 90° CCW rotation: (y,x)(-y, x)

  • rotate(a) — rotate by angle aa radians. rotate(p, a) — rotate around point pp

  • unit() — unit vector. normal() — perpendicular unit vector
  • When to Use

  • Any geometry problem needing point/vector operations

  • Building block for convex hull, line intersection, polygon algorithms

  • Template parameter T lets you switch between int, ll, double, long double
  • Notes

  • Comparison sorts by (y,x)(y, x) — useful for angular sweep and convex hull

  • dist() returns squared distance to avoid floating-point — use for integer comparisons

  • Use distance() only when you need the actual Euclidean length
  • Complexity

  • All operations — O(1)O(1)