Method and source check
Last checked:
Automated source and implementation review, including worked examples and documented numerical limits. This review does not provide professional advice or specialist approval.
Method
Subtract corresponding coordinates, use the Euclidean distance formula, and average coordinates to find the midpoint. Horizontal and vertical distances are unsigned spans. Use Math.hypot for coordinate differences and sum coordinate halves for the midpoint to avoid unnecessary intermediate overflow.
d = √((x₂−x₁)² + (y₂−y₁)²)
M = ((x₁+x₂)/2, (y₁+y₂)/2)
Variables and units
- x₁,y₁
- first point coordinates
- x₂,y₂
- second point coordinates
- d
- straight-line distance
Worked example
For (0,0) and (3,4), the distance is 5, horizontal span 3, vertical span 4, and midpoint (1.5,2).
Limitations
Displayed numeric values are rounded to six decimal places. When scaling for Math.round would overflow, the finite representable value is retained.
- Distance is a scalar; displacement is a directed vector whose magnitude equals straight-line distance.
- Assumes a flat 2D Cartesian coordinate system.
- Unsupported derived numerical ranges are rejected with a localized error on a visible form field; the calculations use floating-point arithmetic.
- The same point has distance 0.
- Distance is nonnegative and independent of point order.
- A 3e154 by 4e154 right-angle displacement yields a finite 5e154 length.