HOWTO · C++

How to Calculate Distance Between Two Points in C++

This article demonstrates how to calculate distance between two points in C++

On this page

To find the Euclidean distance between two points in C++, subtract their corresponding coordinates and apply the Pythagorean theorem. For points (x1, y1) and (x2, y2), the horizontal and vertical differences are dx = x2 - x1 and dy = y2 - y1; the distance is sqrt(dx * dx + dy * dy). The coordinate differences are the two shorter sides of a right triangle, and the distance is its hypotenuse.

Calculate distance from coordinate differences

This formula works for points with integer or floating-point coordinates. Choose a floating-point result when the distance may not be an integer. The examples below are complete programs that can be compiled independently. Runtime evidence for these exact examples records successful executions: the direct formula prints Distance: 1.41421, while the std::hypot example prints Distance: 5 for its different coordinates.

Use std::sqrt and std::pow for a direct implementation

std::sqrt calculates the square root, while std::pow(value, 2) squares a value. Include <cmath> and qualify these functions with std:: in C++. The implementation accepts four double coordinates, so callers can pass integer values too and receive a floating-point distance. For a reusable integer-coordinate function, accept four int parameters, but convert each coordinate before subtraction: set dx to static_cast<double>(x2) - static_cast<double>(x1) and dy to static_cast<double>(y2) - static_cast<double>(y1), then return std::sqrt(std::pow(dx, 2) + std::pow(dy, 2)). A call such as distance(3, 4, 4, 3) then returns a floating-point result. The complete program below separately shows direct use with floating-point coordinates.

Run this example:

#include <cmath>
#include <iostream>

int main() {
  const double x1 = 3.0;
  const double y1 = 4.0;
  const double x2 = 4.0;
  const double y2 = 3.0;
  const double dx = x2 - x1;
  const double dy = y2 - y1;
  const double distance = std::sqrt(std::pow(dx, 2) + std::pow(dy, 2));

  std::cout << "Distance: " << distance << '\n';
}
Distance: 1.41421

If coordinates are stored as integers, convert before subtracting when their range could make the integer subtraction overflow. For example, subtracting two extreme int values may exceed the representable range even if the eventual answer is stored as double. Convert each operand first, such as static_cast<double>(x2) - static_cast<double>(x1), then calculate the squares. Passing integer arguments directly to a function that accepts double is also safe from integer-subtraction overflow when the function performs the subtraction after conversion; an expression like x2 - x1 computed by the caller still subtracts as integers first.

Use std::hypot for a robust calculation

For the same coordinate differences, std::hypot(dx, dy) returns the length of the vector (dx, dy). It is generally preferable when numerical robustness matters because its implementation is designed to avoid undue intermediate overflow and underflow. It does not remove the need to compute differences safely: with integer coordinates, convert operands before subtracting if their difference may exceed the integer type’s range.

This complete alternative uses floating-point inputs and prints 5 for these coordinates, whereas the direct example above prints approximately 1.41421 for its own coordinates. It uses std::hypot from <cmath> and does not need std::pow:

#include <cmath>
#include <iostream>

int main() {
  const double x1 = 4.0;
  const double y1 = 4.5;
  const double x2 = 9.0;
  const double y2 = 4.5;

  std::cout << "Distance: " << std::hypot(x2 - x1, y2 - y1) << '\n';
}
Distance: 5