Hey everyone!
Geometry problems can seem a bit intimidating at first $$$-$$$ with all the floating-point errors, tricky trigonometric functions, and things like dot and cross products. But don’t worry! In this short blog, I’ll show you a simple and reliable way to handle many geometry challenges using just basic integer math. No fancy templates or advanced techniques needed $$$-$$$ just a clean, straightforward approach that makes these problems much easier to manage.
Quick review on functions:
area2(A,B,C): Twice the area of triangle ABC. Equals 0 if A, B, and C are collinear. It's the basis of other functions.area2(P): Twice the area of polygon P calculated by summing triangle areas.in(p,P): Checks if point p lies inside polygon P. Compares polygon area with the sum of triangles formed with p.dis2(A,B): Squared distance between points A and B, useful for length comparisons without the square root.dis(l,o): Distance between point o and line l. Uses integer area but returns real distance.isParallel(l1,l2): Checks if two lines l1 and l2 are parallel using three equal-area checks.isOnLine(l,p): Checks if point p lies exactly on line l.isOnLineFragment(x,y,a): Checks if point a lies on the line segment from x to y.
Implementation:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Point = pair<int,int>;
struct Line{
Point a, b;
ll len2() const{
int dx = a.first - b.first;
int dy = a.second - b.second;
return ll(dx)*dx + ll(dy)*dy;
}
};
using Polygon = vector<Point>;
inline ll area2(const Point& A, const Point& B, const Point& C){
return llabs((ll)A.first*B.second + (ll)B.first*C.second + (ll)C.first*A.second
- (ll)A.second*B.first - (ll)B.second*C.first - (ll)C.second*A.first);
}
ll area2(const Polygon& P){
if(P.size() < 3u) return 0;
ll s = 0;
for(int i = 1; i + 1 < (int) P.size(); i++) s += area2(P[0], P[i], P[i+1]);
return s;
}
bool in(const Point& p, const Polygon& P){
ll sum = 0, n = P.size(), p_area = area2(P);
for(int i = 0; i < n && sum <= p_area; i++)
sum += area2(p, P[i], P[(i+1)%n]);
return sum == p_area;
}
inline ll dis2(const Point& A, const Point& B){
return Line{A, B}.len2();
}
double dis(const Line& l, const Point& o){
return (double) area2(o, l.a, l.b) / sqrt((double)l.len2());
}
bool isParallel(const Line& l1, const Line& l2){
ll a1=area2(l1.a, l2.a, l2.b);
ll a2=area2(l1.b, l2.a, l2.b);
Point c{2*l1.b.first - l1.a.first, 2*l1.b.second - l1.a.second}; // some other point on line l1
ll a3=area2(c,l2.a,l2.b);
return a1 == a2 && a2 == a3;
}
bool isOnLine(const Line& l, const Point& p){
return area2(p, l.a, l.b) == 0;
}
bool isOnLineFragment(const Point& x, const Point& y, const Point& a){
if(!isOnLine(Line{x,y}, a)) return false;
return min(x.first, y.first) <= a.first && a.first <= max(x.first, y.first) &&
min(x.second, y.second) <= a.second && a.second <= max(x.second, y.second);
}
Feel free to share your tricks — and be careful, these functions are not checked! 💀



