Program to find the Distance between two points

posted on 12 May 2026 , updated on 12 May 2026
geometry

Calculating the distance between two points is a fundamental operation in coordinate geometry. This logic is used constantly in routing algorithms (like Google Maps), collision detection in video games, and data clustering in machine learning.

1. Conceptual Understanding: The Mathematics

To find the straight-line distance between Point A (x1, y1) and Point B (x2, y2) on a 2D Cartesian plane, we use the complete Euclidean Distance Formula.

We first find the horizontal difference (run) and the vertical difference (rise) between the two points. These differences act as the two sides of a right-angled triangle. We then use the Pythagorean theorem to find the hypotenuse, which represents our direct distance.

Distance (d) = √((x2 - x1)2 + (y2 - y1)2)

2. Method 1: Using Standard Arithmetic Operators

We can write this formula explicitly in Python using standard operators. We will calculate the differences, square them, sum them together, and then find the square root (by raising to the power of 0.5).

# Method 1: Manual calculation using standard operators
def calculate_distance(x1, y1, x2, y2):
    # Calculate the horizontal and vertical differences
    x_diff = x2 - x1
    y_diff = y2 - y1
    
    # Calculate the sum of their squares
    sum_of_squares = (x_diff ** 2) + (y_diff ** 2)
    
    # Calculate the square root
    distance = sum_of_squares ** 0.5
    return distance

# Example: Distance between (1, 2) and (4, 6)
dist = calculate_distance(1, 2, 4, 6)
print(f"The distance between the two points is {dist}")
# Output: The distance between the two points is 5.0

3. Method 2: The Pythonic Way (math.dist)

Starting in Python 3.8, the standard math library introduced a remarkably convenient function specifically for this task: math.dist(). It allows you to pass the two points as tuples or lists, and it handles the entire calculation internally!

💡 Pro-Tip: Multi-Dimensional Distances
The incredible thing about math.dist(p1, p2) is that it doesn't just work for 2D points. If you pass it two 3D coordinates (x, y, z), or even 10-dimensional arrays, it will effortlessly calculate the Euclidean distance across all dimensions!
# Method 2: Using the optimized math.dist() function (Python 3.8+)
import math

# Define our points as tuples
point_a = (1, 2)
point_b = (4, 6)

# math.dist takes the two iterables directly
result = math.dist(point_a, point_b)

print(f"Distance between Point A and Point B: {result}")

💡 Why it Matters: Real-World Coordinate Math

  • Game Development: In a 2D game, if an enemy needs to trigger an attack when the player gets too close, the game is constantly running this exact distance calculation between the enemy's (x, y) and the player's (x, y) 60 times a second!
  • K-Nearest Neighbors (KNN): One of the most popular foundational machine learning algorithms relies entirely on this formula. To classify a new piece of data, KNN calculates the distance between that new data point and all existing data points to find its closest "neighbors".

Interactive Two-Point Distance Explorer

Adjust the coordinates of Point A and Point B to visualize the distance.

Point A (1, 2)

Point B (4, 6)

Step-by-Step Calculation:
d = √((x2 - x1)² + (y2 - y1)²)
d = √((4 - 1)² + (6 - 2)²)
d = √((3)² + (4)²)
d = √(9 + 16)
d = √(25)
Distance = 5.00

Related Programs

...

A Python program to find the Distance between two points in Co-ordinate geometry.

...

A Python program to find the Distance of a Point from the Origin in Co-ordinate geometry.

...

A Python program to find the Quadrant of a Point in Co-ordinate geometry.

Search
Download PYTHON
Download Python on your system from python.org downloads section