LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsPoints and PolygonsAnglesConvex Hull
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Vectors

In this lesson we will learn about vectors - the object that almost every geometry problem is actually built out of (not to be confused with the data structure).

What is a vector

A vector is an arrow: it has a direction and a length, but it does not have a fixed position. We write it as a pair of numbers, v=(x,y), which we read as "move x to the right and y up".

So a point and a vector are stored in exactly the same way - as two numbers. The difference is only in how we think about them:

  • a point is a place (where something is)
  • a vector is a movement (how to get somewhere)

Two arrows that are equally long and point in the same direction are the same vector, no matter where we draw them. That is what "no fixed position" means, and it is the reason vectors are so convenient: we can move them around freely to wherever the calculation needs them.

vector-no-position.png

From points to vectors

The vector that takes us from point A to point B is just the difference of their coordinates:

AB=(Bx​−Ax​,By​−Ay​)

This is the single most used operation in geometry problems. Almost every task starts by taking the points from the input and turning them into vectors - and from that moment on we work only with vectors.

For example, for A = (1, 2) and B = (4, 6):

AB=(4−1,6−2)=(3,4)

Which says exactly what we expect: to get from A to B we move 3 right and 4 up.

Careful with the order: BA is not the same as AB, it is the exact opposite arrow, BA=−AB. Subtracting in the wrong order is one of the most common bugs in geometry code.

vector-from-points.png

Operations on vectors

There are three basic things we can do with vectors:

Addition - doing one movement after the other:

u+v=(ux​+vx​,uy​+vy​)

Subtraction - the movement from the tip of v to the tip of u:

p​−v=(px​−vx​,py​−vy​)

Scaling - stretching the arrow by a number k. The direction stays the same and the length gets multiplied by k; a negative k flips the arrow around:

k⋅v=(k⋅vx​,k⋅vy​)
vector-operations.png

Length of a vector

A vector has a length (also called its intensity), and it is exactly the distance formula from the previous lesson:

∣v∣=vx2​+vy2​​

Implementation

In C++ we can write all of this as a few small functions next to our struct.

Vector.cpp
#include <bits/stdc++.h>

using namespace std;

struct v{

    long long x, y;
};

v add(v a, v b){

    return {a.x + b.x, a.y + b.y};
}

v sub(v a, v b){

    return {a.x - b.x, a.y - b.y};
}

v scale(v a, long long k){

    return {a.x * k, a.y * k};
}

long long len2(v a){ // squared length - stays an integer!

    return a.x * a.x + a.y * a.y;
}

double len(v a){

    return sqrt((double)len2(a));
}

double dist(v a, v b){

    return len(sub(b, a));
}

int main(){

    v a = {1, 2};
    v b = {4, 6};

    v ab = sub(b, a); // the vector from a to b

    cout<<"Vector AB: ("<<ab.x<<", "<<ab.y<<")\n";
    cout<<"Length: "<<len(ab)<<'\n';
    cout<<"Distance from a to b: "<<dist(a, b);

    return 0;
}

Output:
Vector AB: (3, 4)
Length: 5
Distance from a to b: 5

Summary

Operation Formula Note
Vector A to B (Bx​−Ax​,By​−Ay​) subtract the start point
Addition (ux​+vx​,uy​+vy​) one movement after another
Subtraction (ux​−vx​,uy​−vy​) mind the order
Scaling (k⋅vx​,k⋅vy​) negative k flips it
Length vx2​+vy2​​ same formula as distance

Vectors are the main tool we use to solve geometry problems, and in the next lessons we will see how to use them!