Triangle centers and formulas

Centroid, incenter, circumcenter, orthocenter, excenters, area from sides, angles and medians — all , all built from cross, dot and `PD`. Non-degenerate triangles only (collinear input makes several of these divide by zero).

PD centroid(P a, P b, P c) { return (toPD(a) + toPD(b) + toPD(c)) * (1.0 / 3); }
 
PD circumcenter(P a, P b, P c) {
    PD ab = toPD(b - a), ac = toPD(c - a);
    double d = 2 * cross(ab, ac);
    PD o = {ac.y * norm2(ab) - ab.y * norm2(ac), ab.x * norm2(ac) - ac.x * norm2(ab)};
    return toPD(a) + o * (1.0 / d);
}
PD circumcenter(PD a, PD b, PD c) {               // same, when the vertices are already floating (e.g. mid-Welzl)
    PD ab = b - a, ac = c - a;
    double d = 2 * cross(ab, ac);
    PD o = {ac.y * norm2(ab) - ab.y * norm2(ac), ab.x * norm2(ac) - ac.x * norm2(ab)};
    return a + o * (1.0 / d);
}
 
PD orthocenter(P a, P b, P c) {
    PD o = circumcenter(a, b, c);
    return toPD(a) + toPD(b) + toPD(c) - o * 2;
}
 
PD incenter(P a, P b, P c) {
    double la = dist(b, c), lb = dist(a, c), lc = dist(a, b);
    return (toPD(a) * la + toPD(b) * lb + toPD(c) * lc) * (1.0 / (la + lb + lc));
}
 
PD excenter(P a, P b, P c) {               // opposite a
    double la = dist(b, c), lb = dist(a, c), lc = dist(a, b);
    return (toPD(b) * lb + toPD(c) * lc - toPD(a) * la) * (1.0 / (lb + lc - la));
}
 
double inradius(P a, P b, P c) {
    double la = dist(b, c), lb = dist(a, c), lc = dist(a, b);
    return fabs((double)cross(a, b, c)) / (la + lb + lc);
}
 
double circumradius(P a, P b, P c) {
    double la = dist(b, c), lb = dist(a, c), lc = dist(a, b);
    return la * lb * lc / (2 * fabs((double)cross(a, b, c)));
}
 
double heron(double a, double b, double c) {         // area from side lengths, -1 if impossible
    array<double, 3> s = {a, b, c};
    sort(s.rbegin(), s.rend());                       // x >= y >= z
    auto [x, y, z] = s;
    if (z < 0 || x > y + z) return -1;
    return sqrt(max(0.0, (x + (y + z)) * (z - (x - y)) * (z + (x - y)) * (x + (y - z)))) / 4;
}
 
double angleFromSides(double a, double b, double c) {  // angle opposite side a
    return acos(max(-1.0, min(1.0, (b * b + c * c - a * a) / (2 * b * c))));
}
 
double medianLength(double a, double b, double c) {    // median to side a
    return sqrt(max(0.0, 2 * b * b + 2 * c * c - a * a)) / 2;
}
IdentityStatement
Euler line collinear,
Euler’s triangle formula (so always , equality only for equilateral)
Area,
Incenterweighted average of vertices by the opposite side lengths

Tested: circumcenter/incenter/centroid/orthocenter coincide for an equilateral triangle; orthocenter satisfies the Euler line relation on 10 random triangles; inradius/circumradius satisfy area = inradius * s = circumradius-based abc/(4R) matching the shoelace area; heron matches the shoelace area; medianLength matches the direct distance from a vertex to the opposite midpoint; angleFromSides matches the atan2(|cross|,dot) angle of Primitives; Euler’s formula checked numerically on random triangles.