Points and Vectors
Vectors let us describe geometric movement without writing separate coordinate formulas in every algorithm. Subtracting two points creates a displacement; adding that displacement to a point moves it. This reusable Point structure deliberately supports both interpretations, while the explanation keeps their meanings separate. The program constructs a direction, interpolates along it and computes its length—operations reused in projections, lines and transformations.
Prerequisites: Structs, functions, operator overloading and Pythagoras.
Build the idea
Build a displacement
Let A=(1,2) and B=(4,6). The vector v=B−A=(3,4) says move three units right and four up. Unlike a point, the vector does not depend on where its arrow starts: translating both endpoints by the same amount leaves v unchanged. Implement subtraction component by component; subtracting in the opposite order reverses direction.
Combine and scale
Addition joins displacements head to tail. Multiplying v by k scales each component. Positive k preserves direction, negative k reverses it, and zero produces the zero vector. The expression A+k(B−A) is a point on the supporting line. Values 0≤k≤1 stay on the segment; k outside that interval extrapolates beyond an endpoint.
Measure length
Pythagoras gives |v|=√(vx²+vy²), so (3,4) has length 5. The distance between A and B is the length of B−A. hypotl computes this directly and is preferable to a naive squaring formula for floating values with very different magnitudes. When only comparing integer distances, exact squared lengths often avoid a square root entirely.
Trace the sample
At k=0.5, A+k·v=(2.5,4), the midpoint. The program first prints the displacement, then this interpolated point, then distance 5. Its overloaded operators return new Point values rather than modifying the inputs, making a chain such as a+v*k correspond directly to the mathematical formula. The structure uses long double because k need not be integral.
Respect zero and precision
A zero displacement is a perfectly valid vector, but it has no direction and cannot be normalized by division by its length. This program never normalizes, so coincident endpoints work. Six displayed decimals do not make the underlying answer exact. For integer predicates, use an integer Point variant instead of converting every operation to floating point for convenience.
C++17 implementation
This is a complete program. Compile with C++17 and enter the input described below.
#include<iostream>
#include<cmath>
#include<iomanip>
struct Point{
long double 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};
}
Point operator*(long double k)const{
return {x*k,y*k};
}
};
void print(Point p){
std::cout<<p.x<<' '<<p.y<<'\n';
}
int main(){
Point a,b;
long double k;
std::cin>>a.x>>a.y>>b.x>>b.y>>k;
std::cout<<std::fixed<<std::setprecision(6);
// Scale the displacement, then translate it back to the starting point.
Point v=b-a;
print(v);
print(a+v*k);
std::cout<<std::hypotl(v.x,v.y)<<'\n';
}
Complexity analysis
O(1) time and O(1) auxiliary space: a fixed number of component operations and one hypotenuse calculation.
Example input and output
Read ax ay bx by k as finite numbers, magnitudes at most 10^6. Print B−A, A+k(B−A), and the distance between A and B, each to six decimals.
Input
1 2 4 6 0.5
Output
3.000000 4.000000 2.500000 4.000000 5.000000
Half of (3,4) added to (1,2) yields (2.5,4); the displacement has length 5.
Practical applications
Use vectors in motion, interpolation, projections, collision geometry and rotations. Dot and cross products add angle and orientation information. Homogeneous coordinates become useful when composing many affine transformations.
Check your understanding
Which parameter stays inside the segment from A to B?
Put it into practice
Compute a midpoint and extend the same segment twice beyond A.
Show a hint
Use t=0.5 and t=2; verify the displacement interpretation.