Calculates the distance between two points (x₁, y₁) and (x₂, y₂) as √((x₂−x₁)² + (y₂−y₁)²).
Explanation
This finds the straight-line distance between two points on the coordinate plane, along with their midpoint. The distance formula is the Pythagorean theorem in disguise: picture a right triangle whose legs are the horizontal gap and the vertical gap, and the distance is its hypotenuse.
d=(x2−x1)2+(y2−y1)2
The midpoint is nothing more than the average of the two coordinates.
M=(2x1+x2,2y1+y2)
(x1,y1), (x2,y2) — the two points
d — the distance between them
M — the midpoint, cutting the segment exactly in half
Example
For (x1,y1)=(0,0) and (x2,y2)=(3,4):
d=(3−0)2+(4−0)2=9+16=25=5
The distance is 5, and the midpoint is (20+3,20+4)=(1.5,2).
Notes
The differences get squared, so x2−x1 and x1−x2 give the same answer. The order of the points does not matter.
Negative coordinates behave no differently. From (−2,1) to (1,5) the gaps are 3 and 4, so the distance is 5.
A distance is never negative, and it is zero only when the two points coincide.
In three dimensions, add the squared difference in z under the same square root. Nothing else changes.