HomeMath › Line Segment Length Calculator

Line Segment Length Calculator

Find the length of a line segment from its two endpoints. Enter the coordinates (x1, y1) and (x2, y2) — negatives and decimals are fine — and get the distance, the midpoint, and the slope in one pass.

Example: with x1 1 · y1 2 · x2 7 · y2 10 → Segment length: 10 units.

  • Midpoint(4, 6)
  • Slope1.333

Computed by the calculator below using its default values. Change any input to see your own numbers.

Segment length
Midpoint
Slope

Length = √((x2−x1)² + (y2−y1)²) — the Pythagorean theorem on the horizontal and vertical runs between the points.

The distance formula is hidden Pythagoras

Between any two points you can draw a right triangle: the horizontal leg is the change in x (dx = x2 − x1) and the vertical leg is the change in y (dy = y2 − y1). The segment itself is the hypotenuse, so its length is √(dx² + dy²). From (1, 2) to (7, 10), the legs are 6 and 8, and the length is √(36 + 64) = 10 — a scaled 3-4-5 triangle.

Signs never hurt you here, because each difference gets squared: (5 − (−3)) = 8 contributes the same 64 whether the run goes left or right. The midpoint is just the average of each coordinate, ((x1+x2)/2, (y1+y2)/2), and the slope dy/dx tells you the tilt — except for vertical segments, where dx = 0 and the slope is undefined rather than "very large".

How it’s calculated

Length = √((x2−x1)² + (y2−y1)²); midpoint = ((x1+x2)/2, (y1+y2)/2); slope = (y2−y1)/(x2−x1), reported as undefined when x2 = x1 (vertical) and 0 when y2 = y1 (horizontal). Coordinates are unitless — the length is in whatever units your axes use.

Flat 2D Cartesian coordinates only; for 3D points add (z2−z1)² under the root, and for lat/long positions use great-circle distance instead.

Segment lengths that come out whole

FromTodx, dyLength
(0, 0)(3, 4)3, 45
(1, 2)(7, 10)6, 810
(0, 0)(5, 12)5, 1213
(−4, −3)(4, 12)8, 1517
(0, 0)(7, 24)7, 2425

Computed with √(dx² + dy²); all rows are Pythagorean triples, which is why the lengths are integers.

Common mistakes

  • Subtracting coordinates in mismatched order (x2 − x1 but y1 − y2) — harmless for length since squares kill the sign, but it flips the slope's sign.
  • Forgetting to square before adding: √(dx² + dy²) is not dx + dy. From (1,2) to (7,10) the answer is 10, not 14.
  • Calling a vertical segment's slope "infinity" and pushing it through later algebra; treat it as undefined and handle x = constant lines separately.
  • Averaging only one coordinate for the midpoint — both x and y get averaged.

Frequently asked questions

What is the formula for the length of a line segment?

Length = √((x2 − x1)² + (y2 − y1)²), the distance formula. It is the Pythagorean theorem with the coordinate differences as the two legs.

Does the order of the points matter?

Not for length or midpoint — squaring removes the sign, and averages ignore order. Slope keeps the same value too, as long as you subtract both coordinates in the same order.

How do I find the midpoint?

Average the x-values and average the y-values: ((x1+x2)/2, (y1+y2)/2). For (1, 2) and (7, 10) the midpoint is (4, 6) — always exactly halfway along the segment.

What if the segment is vertical?

The length still works fine — it is just |y2 − y1|. The slope, though, is undefined because you would divide by zero; a vertical line is written x = constant instead of y = mx + b.

Does this work in 3D?

Extend the same idea: √(dx² + dy² + dz²). This page handles 2D; for three dimensions add the z-difference squared under the root.