Computational Geometry
Overview
Computational geometry problems you run into in contests are usually not hard, but if you run into one without having learned it, you just score zero. To avoid that, I decided to properly learn some computational geometry.
Result: I learned it all and still scored zero.
Vector Combo Meal
There's nothing much to say about the normal vector stuff, so I'll just talk about what the cross product means. The cross product is the signed area of the parallelogram enclosed by the two vectors. The picture makes it clear:

The code:
struct vec
{
double x, y;
vec(double _x = 0, double _y = 0) : x(_x), y(_y) {}
vec operator+(const vec &rhs) { return vec(x + rhs.x, y + rhs.y); }
vec operator-(const vec &rhs) { return vec(x - rhs.x, y - rhs.y); }
double operator*(const vec &rhs) const { return x * rhs.x + y * rhs.y; }
vec operator*(const double &rhs) { return vec(x * rhs, y * rhs); }
double operator^(const vec &rhs) const { return x * rhs.y - y * rhs.x; }
vec operator/(const double &rhs) const { return vec(x / rhs, y / rhs); }
double len2() { return x * x + y * y; }
double len() { return sqrt(len2()); }
vec normalize() { return (*this) / len(); }
};
Points, Lines
Position of a point relative to a line
If we want to tell whether point \(P\) is to the left or the right of a line with direction vector \(v\), we can pick a point \(Q\) on the line and then just check the sign of \(\vec{QP} \times v\).
Checking whether segments intersect
First deal with parallel segments, which you can do by checking the cross product twice. If they overlap, check whether there are three collinear points (you can also use the cross product to check the angle). Once that's settled, we properly check whether they intersect.
First check whether the quadrilateral regions occupied by the two segments overlap. If they do, check further: you can use the point-line position test above to see whether the two endpoints of one segment lie on opposite sides of the other segment. Combine the two and you're done. These are called the rapid rejection test and the straddle test.
Intersection point of two lines
Let me draw a picture:

If we want point G, since we know the coordinates of point \(B\), we can try to find \(\vec{BG}\) and then just add them. Finding \(\vec{BG}\) isn't hard: think of computing it directly as \(\vec{BG} = \vec{u} \times \frac{|\vec{BG}|}{|\vec{u}|}\). Notice there's a ratio-like thing at the end; we can express it with vector cross products, because with the same base, the ratio of areas means the ratio of heights.
vec intersect(vec a, vec b, vec c, vec d)
{
vec v = b - a, w = d - c, u = a - c;
return a + v * double((w ^ u) / (v ^ w));
}
Intersection point of two segments
First run the intersection check, then just handle it with the method for the intersection of two lines.
Point-line distance
If it's a segment, you first have to check the lengths to the two endpoints. Computing the point-line distance really just means finding the area of the parallelogram and then dividing by the base.
Polygons
Convex hull
Sort by the \(x\)-axis first, then just maintain a monotonic stack where the cross products are monotonic.
// P2742.cpp
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#define pr pair<double, double>
using namespace std;
int n;
vector<pr> points;
deque<int> prs;
double getDist(pr a, pr b)
{
return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
}
bool judge(int a, int b, int idx)
{
return (points[a].second - points[b].second) * (points[b].first - points[idx].first) >
(points[a].first - points[b].first) * (points[b].second - points[idx].second);
}
int main()
{
double ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
double x, y;
scanf("%lf%lf", &x, &y);
points.push_back(make_pair(x, y));
}
sort(points.begin(), points.end());
prs.push_front(0), prs.push_front(1);
for (int i = 2; i < n; i++)
{
while (prs.size() > 1 && !judge(prs[0], prs[1], i))
prs.pop_front();
prs.push_front(i);
}
for (int i = 0; i < prs.size() - 1; i++)
ans += getDist(points[prs[i]], points[prs[i + 1]]);
prs.clear();
prs.push_front(0), prs.push_front(1);
for (int i = 2; i < n; i++)
{
while (prs.size() > 1 && judge(prs[0], prs[1], i))
prs.pop_front();
prs.push_front(i);
}
for (int i = 0; i < prs.size() - 1; i++)
ans += getDist(points[prs[i]], points[prs[i + 1]]);
printf("%.2lf", ans);
return 0;
}
Checking whether a point lies inside a convex polygon
Odd inside, even outside. https://www.cnblogs.com/anningwang/p/7581545.html
Area of a convex polygon
This one is fairly simple, just:
\[ S = \frac{1}{2} \sum_{i = 1}^{n - 1} P_i \times P_{i + 1} i\]
记住这个“一般都不难”(划掉)
太难了(自闭