DEV Community

Agoes Jayanata
Agoes Jayanata

Posted on

The Geometry Behind a Stair Stringer and Rafter Length Calculator

Framing is just applied geometry. Every stair stringer and every rafter reduces to a right triangle: a vertical rise, a horizontal run, and the hypotenuse that the lumber has to cover. Get the triangle right and the structure works. The stair calculator and rafter calculator at FixUpMath were built with Astro and published free because the math — not a paywall — should be the product. Building either one is a compact project that teaches you integer ceilings, the Pythagorean theorem, and how to turn pitch into a slope ratio in JavaScript.

Stairs: rise and run

A stair is defined by its total rise (the vertical distance from one finished floor to the next) and its total run (how far the run of treads extends horizontally). The code's first job is to turn those into per-step dimensions.

Building codes in the U.S. cap the riser at 7.75 in and typically require a minimum tread run around 10 in. Given a total rise in inches, the number of risers is the ceiling of total-rise divided by the max riser:

function riserCount(totalRiseIn, maxRiserIn = 7.75) {
  return Math.ceil(totalRiseIn / maxRiserIn);
}
Enter fullscreen mode Exit fullscreen mode

Math.ceil again — same reason. If a 90 in rise gives you 11.61 risers, you can't build 11.61 of anything. Twelve risers at 7.5 in each satisfy the code and split the rise evenly. The uniform riser height is then totalRiseIn / riserCount, and the total run is riserCount * treadRunIn.

Stringer length via Pythagoras

A straight-run stringer carries every riser and every tread. Its horizontal projection is the total run; its vertical projection is the total rise. The cut length along the hypotenuse is:

function stringerLength(totalRiseIn, totalRunIn) {
  return Math.sqrt(totalRiseIn ** 2 + totalRunIn ** 2);
}
Enter fullscreen mode Exit fullscreen mode

That's the theoretical length. In practice you add a few inches for the heel cut, the toe cut, and the bottom notch — and you buy the 2×12 a foot or two longer than the computed hypotenuse. In UI terms, show the computed length and the recommended purchase length separately so the user can see where the extra came from.

Rafters: span and pitch

A rafter is the same shape of problem. You know the span (the horizontal distance the roof covers) and the pitch (rise per 12 in of run, written as a ratio like 6/12). A 6/12 pitch means the roof rises 6 in for every 12 in it runs.

Half the span is the horizontal run the rafter covers. The rise over that run is pitchRatio * halfSpan, and the rafter length is the hypotenuse:

function rafterLength(spanIn, pitchNumerator, pitchDenominator = 12) {
  const halfSpan = spanIn / 2;
  const rise = (pitchNumerator / pitchDenominator) * halfSpan;
  const rafter = Math.sqrt(halfSpan ** 2 + rise ** 2);
  return { rafter, rise, halfSpan };
}
Enter fullscreen mode Exit fullscreen mode

Return an object, not a single number. The rise and half-span are the intermediate values the user needs to audit, and the rafter length is the number that goes on the cut list. For a 28 ft (336 in) span at 6/12: half-span 168 in, rise 84 in, rafter ≈ 189.7 in (about 15 ft 9.7 in).

Where the code trips

Three edge cases show up repeatedly:

  • Riser count rounding. Always Math.ceil. A 7.74 in riser is still a code-compliant answer only if every riser is ≤ 7.75 in, and Math.round can produce a taller riser that fails inspection.
  • Pitch normalization. Let the user type "6/12" or just "6" and normalize it. A bare number is conventionally "N over 12". Label the assumption.
  • Units. Stair users think in inches; roofers often think in feet for the span. Convert at the boundary and keep one canonical unit (inches) inside the math.

Putting a stair + rafter example together

A deck stair with a 96 in total rise, 10 in treads:

  • risers = ceil(96 / 7.75) = 13
  • uniform riser = 96 / 13 ≈ 7.38 in
  • total run = 13 × 10 = 130 in
  • stringer = sqrt(96² + 130²) ≈ 161.8 in (buy a 2×12 in 14 ft)

A gable roof, 24 ft (288 in) span at 8/12:

  • half-span = 144 in
  • rise = (8/12) × 144 = 96 in
  • rafter = sqrt(144² + 96²) ≈ 173.2 in (about 14 ft 5.2 in)

Why this matters

Right-triangle calculators are small enough to finish in an afternoon and real enough to matter. They teach the same pattern that comes up everywhere in tooling code: decompose the geometry, compute the intermediates, round the units that have to be whole, and show the user the work. The stair calculator and rafter calculator are free, built with Astro, and a reasonable reference if you're practicing the pattern.

Top comments (0)