Classical Geometry · Computational Mathematics

Heron's Formula

Find the area of any triangle knowing only the lengths of its three sides — no angles, no height, no trigonometry required.

The Formula
\[ A=\sqrt{\,s\,(s-a)(s-b)(s-c)\,}\qquad s=\frac{a+b+c}{2} \]

Below you'll find a full derivation (two independent proofs), worked examples by hand, verified GNU Octave code, three interactive playgrounds, and the numerical pitfalls that bite real programs — with the fix.

r a b c

01The Statement

Take any triangle with side lengths \(a\), \(b\), and \(c\). Define its semiperimeter \(s\) — literally "half the perimeter":

\[ s=\frac{a+b+c}{2}. \]

Then its area \(A\) is given entirely by those three lengths:

\[ \boxed{\,A=\sqrt{s\,(s-a)(s-b)(s-c)}\,}. \]

That's the whole formula. You never need the height, you never need an angle, and you never need to know where the triangle sits in the plane. Three lengths in, one area out.

SymbolMeaningExample (a 3–4–5 triangle)
a, b, cThe three side lengthsa=3, b=4, c=5
sSemiperimeter, \((a+b+c)/2\)s = 12/2 = 6
s−a, s−b, s−cHow far each side falls short of \(s\)3, 2, 1
AThe area√(6·3·2·1) = √36 = 6
Sanity check

For the formula to give a real number, all of \(s-a,\,s-b,\,s-c\) must be positive. That is exactly the triangle inequality: each side must be shorter than the sum of the other two. If it fails, no triangle exists, and the expression under the square root goes negative.

02A Little History

Heron of Alexandria (also spelled Hero) was a Greek mathematician and engineer who lived in the first century CE — best estimates put him around 10–70 CE. He was a prolific inventor, credited with an early steam device (the aeolipile), vending mechanisms, and automated theatres.

The formula appears in his work Metrica, a treatise on measurement rediscovered only in 1896. Heron gives a fully worked numerical proof for a triangle with sides 13, 14, 15 (area 84). Though it carries his name, historians believe the result was likely known earlier — possibly to Archimedes two centuries before — but Heron's is the earliest surviving written derivation.

What makes it remarkable is its purely metric character: it converts a question about shape and orientation into one of arithmetic on lengths alone, a recurring theme in computational geometry to this day.

03Intuition — Why the Semiperimeter?

The appearance of \(s\) can feel mysterious. Here is one way to make peace with it. The product

\[ 16A^2=(a+b+c)(-a+b+c)(a-b+c)(a+b-c) \]

is the formula in its "raw" symmetric form (we will derive it in Proof I). Each factor is a perimeter-like combination of the sides. Dividing each side combination through by 2 turns those four factors into \(2s\), \(2(s-a)\), \(2(s-b)\), \(2(s-c)\); the four 2's multiply to 16 and cancel the leading 16, leaving the clean product under the root.

So the semiperimeter isn't magical — it is just the bookkeeping device that makes a naturally symmetric, four-factor product look tidy. Notice the deep symmetry: swap any two sides and the formula is unchanged, exactly as area should be.

Degeneration

As a triangle flattens toward a straight line, one factor — say \(s-c\) — slides toward \(0\), driving the area smoothly to \(0\). The formula knows when your triangle has collapsed.

04Worked Examples by Hand

Example 1 — the 3·4·5 right triangle

Sides: \(a=3,\ b=4,\ c=5\).   Semiperimeter: \(s=\tfrac{3+4+5}{2}=6\).

\[ A=\sqrt{6(6-3)(6-4)(6-5)}=\sqrt{6\cdot3\cdot2\cdot1}=\sqrt{36}=6. \]

Cross-check: a 3–4–5 triangle is right-angled, so \(A=\tfrac12\cdot3\cdot4=6\). ✓

Example 2 — a scalene 7·8·9 triangle

Semiperimeter: \(s=\tfrac{7+8+9}{2}=12\).

\[ A=\sqrt{12(12-7)(12-8)(12-9)}=\sqrt{12\cdot5\cdot4\cdot3}=\sqrt{720}=12\sqrt5\approx 26.8328. \]

There is no nice right angle to fall back on here — which is precisely where Heron earns its keep.

Example 3 — Heron's own 13·14·15 triangle

Semiperimeter: \(s=\tfrac{13+14+15}{2}=21\).

\[ A=\sqrt{21(21-13)(21-14)(21-15)}=\sqrt{21\cdot8\cdot7\cdot6}=\sqrt{7056}=84. \]

A perfect integer area — the same example Heron worked in Metrica.

△ ▽ △

05Proof I — Algebraic (via the Law of Cosines)

This is the quickest rigorous route. It assumes only two standard facts: the area formula \(A=\tfrac12 ab\sin C\) and the Law of Cosines.

Full derivation

Start from the trigonometric area, where \(C\) is the angle between sides \(a\) and \(b\):

\[ A=\tfrac12 ab\sin C \quad\Longrightarrow\quad 16A^2=4a^2b^2\sin^2C. \]

Replace \(\sin^2 C=1-\cos^2 C\):

\[ 16A^2=4a^2b^2\bigl(1-\cos^2 C\bigr)=4a^2b^2-(2ab\cos C)^2. \]

Use the Law of Cosines, \(c^2=a^2+b^2-2ab\cos C\), i.e. \(2ab\cos C=a^2+b^2-c^2\):

\[ 16A^2=4a^2b^2-\bigl(a^2+b^2-c^2\bigr)^2. \]

Factor the difference of two squares, \(X^2-Y^2=(X-Y)(X+Y)\), with \(X=2ab,\ Y=a^2+b^2-c^2\):

\[ 16A^2=\bigl(2ab-(a^2+b^2-c^2)\bigr)\bigl(2ab+(a^2+b^2-c^2)\bigr). \]

Recognise each bracket as another difference/sum of squares:

\[ 2ab-(a^2+b^2-c^2)=c^2-(a-b)^2,\qquad 2ab+(a^2+b^2-c^2)=(a+b)^2-c^2. \]

Factor both once more:

\[ 16A^2=\underbrace{(c-a+b)(c+a-b)}_{c^2-(a-b)^2}\cdot\underbrace{(a+b-c)(a+b+c)}_{(a+b)^2-c^2}. \]

Introduce \(s=\tfrac{a+b+c}{2}\). Then each factor becomes a multiple of two:

\[ a+b+c=2s,\quad -a+b+c=2(s-a),\quad a-b+c=2(s-b),\quad a+b-c=2(s-c). \]

Substitute — the four 2's give \(2^4=16\), cancelling the leading 16:

\[ 16A^2=2(s-a)\cdot 2(s-b)\cdot 2(s-c)\cdot 2s = 16\,s(s-a)(s-b)(s-c). \] \[ \therefore\quad A=\sqrt{s(s-a)(s-b)(s-c)}. \]

06Proof II — Geometric (via the Inscribed Circle)

This route is closer in spirit to Heron's own and reveals a beautiful by-product: an exact formula for the inradius \(r\). It uses three classical facts, each justified below.

Three lemmas, then the assembly

Lemma 1 — Area equals \(rs\). Join the incentre \(I\) (centre of the inscribed circle, radius \(r\)) to the three vertices. This cuts the triangle into three smaller triangles whose bases are \(a,b,c\) and whose common height is \(r\):

\[ A=\tfrac12 r a+\tfrac12 r b+\tfrac12 r c=\tfrac12 r(a+b+c)=r\,s. \]

Lemma 2 — Tangent lengths are \(s-a,\,s-b,\,s-c\). From each vertex the two tangent segments to the incircle have equal length; call them \(x,y,z\) from the vertices opposite \(a,b,c\). Pairing up the sides,

\[ y+z=a,\quad z+x=b,\quad x+y=c \;\Longrightarrow\; x+y+z=s, \]

so \(x=s-a,\ y=s-b,\ z=s-c\) (e.g. \(x=(x+y+z)-(y+z)=s-a\)).

Lemma 3 — Half-angle tangents. The incentre lies on each angle bisector, and \(r\) is perpendicular to a side at the point of tangency a distance \(s-a\) from vertex \(A\). Hence

\[ \tan\tfrac{A}{2}=\frac{r}{s-a},\qquad \tan\tfrac{B}{2}=\frac{r}{s-b},\qquad \tan\tfrac{C}{2}=\frac{r}{s-c}. \]

Assembly. Because \(\tfrac{A}{2}+\tfrac{B}{2}+\tfrac{C}{2}=\tfrac{\pi}{2}\), the half-angle tangents satisfy the identity

\[ \tan\tfrac{A}{2}\tan\tfrac{B}{2}+\tan\tfrac{B}{2}\tan\tfrac{C}{2}+\tan\tfrac{C}{2}\tan\tfrac{A}{2}=1. \]

Substituting Lemma 3 and clearing denominators:

\[ \frac{r^2\bigl[(s-c)+(s-a)+(s-b)\bigr]}{(s-a)(s-b)(s-c)}=1. \]

Now \((s-a)+(s-b)+(s-c)=3s-(a+b+c)=3s-2s=s\), so

\[ r^2=\frac{(s-a)(s-b)(s-c)}{s}\qquad\Longrightarrow\qquad r=\sqrt{\frac{(s-a)(s-b)(s-c)}{s}}. \]

Finally, feed this into Lemma 1, \(A=rs\):

\[ A=s\,r=s\sqrt{\frac{(s-a)(s-b)(s-c)}{s}}=\sqrt{s(s-a)(s-b)(s-c)}. \]

Bonus formula

The derivation hands you the inradius for free: \(\displaystyle r=\frac{A}{s}=\sqrt{\frac{(s-a)(s-b)(s-c)}{s}}\). The interactive triangle below draws exactly this circle.

07Interactive — Step-by-Step Calculator

Type three side lengths (or pick a preset). The widget validates the triangle inequality, walks through every intermediate value, and draws the triangle to scale.

Heron Calculator

08Interactive — Drag a Triangle

Grab any vertex and move it. The side lengths, semiperimeter, and area (computed by Heron's formula) update live. As an independent check, the area is recomputed with the unrelated shoelace formula from the coordinates — watch the two agree to the last digit. The dashed circle is the inscribed circle of radius \(r=A/s\) from Proof II.

Live Triangle · Heron vs Shoelace

Tip: drag the three solid dots. One unit = 40 pixels.

Area — Heron √(s(s−a)(s−b)(s−c))
Area — Shoelace (from coordinates)

09GNU Octave — Code You Can Run

Every snippet below was executed in GNU Octave 8.4; the output shown is the real result. In Octave each function normally lives in its own .m file named after the function.

1 · The basic function

heron.m
function A = heron(a, b, c)
  % HERON  Area of a triangle from its three side lengths.
  %   A = HERON(a, b, c) returns the area via Heron's formula.
  s = (a + b + c) / 2;            % semiperimeter
  A = sqrt(s * (s-a) * (s-b) * (s-c));
end
Octave session>> heron(3, 4, 5) ans = 6 >> heron(7, 8, 9) ans = 26.833 % = 12*sqrt(5)

2 · Guard against impossible triangles

A robust version refuses inputs that violate the triangle inequality instead of silently returning a complex number.

heron_safe.m
function A = heron_safe(a, b, c)
  % Validate the triangle inequality before computing.
  sides = sort([a, b, c]);        % ascending: shortest two first
  if sides(1) + sides(2) <= sides(3)
    error("heron_safe: %g, %g, %g cannot form a triangle", a, b, c);
  end
  s = (a + b + c) / 2;
  A = sqrt(s * (s-a) * (s-b) * (s-c));
end
Octave session>> heron_safe(2, 2, 10) error: heron_safe: 2, 2, 10 cannot form a triangle

3 · Vectorise over many triangles at once

Octave shines at array work. Swap * for the element-wise .* and you can process whole columns of triangles in one shot — no loop.

interactive session
a = [3;  5;  8];     % three triangles, one per row
b = [4; 12; 15];
c = [5; 13; 17];

s = (a + b + c) / 2;                       % column of semiperimeters
A = sqrt(s .* (s-a) .* (s-b) .* (s-c))     % element-wise -> column of areas
outputA = 6 30 60

4 · Cross-check with the shoelace formula

If you happen to have the vertex coordinates, the shoelace (Gauss) formula gives the area directly. It's a great independent test of a Heron implementation.

shoelace.m
function A = shoelace(P)
  % P is an n-by-2 matrix of polygon vertices (here, a triangle).
  x = P(:,1);  y = P(:,2);
  n = rows(P);
  idx = [2:n, 1];                 % "next" vertex, wrapping around
  A = 0.5 * abs(sum(x .* y(idx) - x(idx) .* y));
end
Octave session — a right triangle with legs 4 and 3>> P = [0 0; 4 0; 0 3]; >> shoelace(P) ans = 6 >> heron(4, 3, 5) % same triangle, by side lengths ans = 6

5 · Visualising the area surface

Fix one side at \(a=5\) and let the other two roam: the height of the surface is the Heron area, and it vanishes exactly where the triangle inequality fails (the dark border).

heron_surface.m
a = 5;
b = linspace(0.1, 10, 400);
c = linspace(0.1, 10, 400);
[B, C] = meshgrid(b, c);

s = (a + B + C) / 2;
A = sqrt(s .* (s - a) .* (s - B) .* (s - C));   % complex where invalid

contourf(B, C, real(A), 20, "LineColor", "none");
colorbar; axis equal tight;
xlabel("side b"); ylabel("side c");
title("Heron area of triangles with a = 5");
print("heron_surface.png", "-dpng");
resultSaves heron_surface.png — a filled contour map. The bright ridge runs along b ≈ c (near-isosceles, largest area); the area falls to 0 along the lines b + c = 5, |b - c| = 5 where the triangle degenerates.

10Numerical Stability — and Kahan's Fix

The textbook formula has a famous flaw on a computer. For a needle-like triangle (one extremely thin sliver), the quantities \(s\) and \(s-a\) are nearly equal large numbers, so subtracting them throws away most of your significant digits — catastrophic cancellation. The result can be badly wrong even in double precision.

William Kahan published a re-grouping that is provably accurate. Sort the sides so that \(a\ge b\ge c\), then evaluate with this exact parenthesisation (the parentheses are not optional):

\[ A=\tfrac14\sqrt{\bigl(a+(b+c)\bigr)\bigl(c-(a-b)\bigr)\bigl(c+(a-b)\bigr)\bigl(a+(b-c)\bigr)}. \]
heron_kahan.m
function A = heron_kahan(a, b, c)
  % Numerically stable area (Kahan). Sort so a >= b >= c first.
  s = sort([a, b, c], "descend");
  a = s(1); b = s(2); c = s(3);
  A = 0.25 * sqrt( (a + (b + c)) * (c - (a - b)) ...
                 * (c + (a - b)) * (a + (b - c)) );
end
Octave — isosceles needle a=b=1e9, c=1.5e-5 (true area is exactly 7500)>> heron(1e9, 1e9, 1.5e-5) ans = 7510.2 % naive: wrong in the 3rd digit! >> heron_kahan(1e9, 1e9, 1.5e-5) ans = 7500.0 % stable: exact

Try it yourself — the playground below runs the same IEEE-754 double arithmetic your browser uses, so you'll reproduce the cancellation live.

Stability Explorer · naive vs Kahan
Naive √(s(s−a)(s−b)(s−c))
Kahan (sorted, regrouped)
Rule of thumb

For everyday triangles the naive formula is perfectly fine. Reach for Kahan's version whenever sides can differ by many orders of magnitude, or whenever a triangle may be almost degenerate — common in computational geometry, meshing, and graphics.

11Beyond Triangles

Brahmagupta's formula — cyclic quadrilaterals

Around 628 CE the Indian mathematician Brahmagupta generalised Heron to any quadrilateral whose four vertices lie on a circle. With sides \(a,b,c,d\) and \(s=\tfrac{a+b+c+d}{2}\):

\[ A=\sqrt{(s-a)(s-b)(s-c)(s-d)}. \]

Set \(d=0\) and the fourth vertex collapses onto a third — the quadrilateral becomes a triangle and Brahmagupta reduces exactly to Heron. Heron is the \(d\to 0\) shadow of Brahmagupta.

The Cayley–Menger determinant — any dimension

The grand generalisation expresses the squared content of a simplex from its edge lengths alone. For a triangle it is the determinant

\[ 16A^2=-\begin{vmatrix}0&1&1&1\\[2pt]1&0&c^2&b^2\\[2pt]1&c^2&0&a^2\\[2pt]1&b^2&a^2&0\end{vmatrix}, \]

which expands precisely to the \(16A^2\) we factored in Proof I. The same determinant, enlarged, gives the volume of a tetrahedron from its six edges, and so on into higher dimensions — Heron's idea, scaled up forever.

△ ▽ △