Line Length Calculator – Measure Segment Length in 2D and 3D
A line length (or line segment length) is the straight-line distance between two defined endpoints. This calculator applies the Euclidean distance formula derived from the Pythagorean theorem to compute that distance precisely, and also returns the slope, angle of inclination, midpoint, and unit vector of the segment — all from a single pair of coordinate inputs.
The Euclidean Distance Formula
Given two points A = (x₁, y₁) and B = (x₂, y₂) in a flat coordinate plane, the length of segment AB is:
d = √((x₂ − x₁)² + (y₂ − y₁)²)
This formula follows directly from the Pythagorean theorem. The horizontal separation Δx and vertical separation Δy form the two legs of a right triangle, and the segment itself is the hypotenuse. Squaring, summing, and taking the square root yields the exact length.
Extending to 3D Space
For points in three-dimensional space, a third coordinate Z is added. The 3D formula is:
d = √((x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²)
Toggle the calculator to 3D mode to reveal Z-coordinate fields. This is useful for spatial geometry, CAD design, 3D game development, and physics problems involving three-dimensional displacement vectors.
Additional Outputs Explained
Beyond the primary line length, the calculator also displays:
- Δx and Δy (and Δz in 3D) — the signed component differences (x₂ − x₁), (y₂ − y₁), and (z₂ − z₁). These are the projections of the segment onto each axis.
- Slope — defined as m = Δy / Δx in 2D. A positive slope rises left to right; a negative slope falls. When Δx = 0 (vertical segment), slope is mathematically undefined and the calculator displays "Undefined".
- Angle — the angle the segment makes with the positive x-axis, in degrees. Computed as
θ = arctan2(Δy, Δx) × (180 / π). Values range from −180° to 180°. - Midpoint — the point exactly halfway between A and B:
M = ((x₁ + x₂) / 2, (y₁ + y₂) / 2). - Unit vector — the segment direction normalized to length 1:
û = (Δx / d, Δy / d). Unit vectors encode pure direction without magnitude and are widely used in physics, computer graphics, and game development.
Pythagorean Triple Detection
When the three values |Δx|, |Δy|, and d are all whole numbers, the calculator checks whether they match a Pythagorean triple — a set of integers (a, b, c) satisfying a² + b² = c². The classic example is (3, 4, 5): a segment from (0, 0) to (3, 4) has a length of exactly 5. Other well-known triples include 5-12-13, 8-15-17, and 7-24-25. Recognizing these triples is useful in geometry proofs, construction, and puzzle design.
Common Applications
Education and geometry — verifying side lengths in coordinate geometry problems, proving triangle properties, and computing perimeters of polygons whose vertices are given as coordinate pairs.
Engineering and CAD — measuring distances between component endpoints on technical drawings, checking tolerance compliance, and computing cable or pipe runs between two known positions.
Game and app development — computing the distance between two game objects for collision detection, pathfinding heuristics, and rendering decisions. The unit vector output is especially useful for normalizing movement direction.
Physics — finding displacement magnitude from initial and final position coordinates, and computing the direction of the displacement vector using the angle or unit vector outputs.
Horizontal and Vertical Segments
Axis-aligned segments have a simplified length calculation. For a horizontal segment (y₁ = y₂), the length reduces to |x₂ − x₁|. For a vertical segment (x₁ = x₂), the length reduces to |y₂ − y₁|. The calculator detects these cases automatically and labels the result accordingly.
Precision and Limitations
All calculations use JavaScript's 64-bit IEEE 754 floating-point arithmetic, which provides roughly 15–16 significant decimal digits — sufficient for almost all practical applications. The Pythagorean triple detector uses a small tolerance (10⁻⁹) to handle floating-point rounding and correctly identifies near-integer results as exact. The tool operates in abstract coordinate units — if your coordinates are in meters, the output is in meters; no unit conversion is applied.