
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Distance Between Two Points in C++
Problem Description
In this problem, we are given coordinate points of a 2-dimensional plane and a 3-dimensional plane, and we have to find the distance between these two points. In this article, we are going to discuss how we can find the distance between two points in C++.
Approaches to Calculate Distance Between Two Points
To solve this problem, here are the two approaches that can you use:
Using 2D Coordinates
In this approach, we use the direct distance formula to calculate the distance between two points. The formula for calculating the distance between two points in 2D space is derived from the Pythagorean theorem:
Distance between two points = ?((x2 - x1)² + (y2 - y1)²)
We use the above formula. We create a function and directly apply the distance formula using the sqrt()
and pow()
inbuilt functions. The sqrt()
function is used to find the square root, and the pow()
function is used to calculate power on numbers.
Example
Input:
x1 = 3, y1 = 4
x2 = 7, y2 = 1
Output:
The distance between the two points is: 5
Explanation:
We use the distance formula to calculate the distance between two points:
Distance = ?((x2 - x1)² + (y2 - y1)²)
= ?((7 - 3)² + (1 - 4)²)
= ?(16 + 9) = ?25 = 5
Implementation Code
#include<bits/stdc++.h> using namespace std; // Function to calculate the distance between two points double calculateDistance(double x1, double y1, double x2, double y2) { double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); return distance; } // Main function to apply the distance formula int main() { double x1 = 3; double y1 = 4; double x2 = 7; double y2 = 1; // Calculate and display the distance double distance = calculateDistance(x1, y1, x2, y2); cout << "The distance between the two points is: " << distance << endl; return 0; }
Output
The distance between the two points is: 5
Using 3D Coordinates
In this problem, we are given three coordinates of the plane. For 3-dimensional space, one additional coordinate z
is included for each point. We can find the distance between two points (x1, y1, z1) and (x2, y2, z2) in the 3D coordinate system using the formula:
Distance = ?((x2 - x1)² + (y2 - y1)² + (z2 - z1)²)
This formula is derived from the Pythagorean theorem and is used to calculate the direct distance between two points in 3D space.
Example
Input:
x1 = 1, y1 = 2, z1 = 3
x2 = 4, y2 = 6, z2 = 8
Output:
The distance between the two points is: 7.07107
Explanation:
We use the distance formula to calculate the distance between two points in 3D space:
Distance = ?((x2 - x1)² + (y2 - y1)² + (z2 - z1)²)
= ?((4 - 1)² + (6 - 2)² + (8 - 3)²)
= ?(9 + 16 + 25) = ?50 ? 7.07107
Implementation Code
#include<bits/stdc++.h> using namespace std; // Function to calculate the distance between two points in 3D double calculateDistance3D(double x1, double y1, double z1, double x2, double y2, double z2) { // Apply the distance formula return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2)); } int main() { double x1 = 1; double y1 = 2; double z1 = 3; double x2 = 4; double y2 = 6; double z2 = 8; // Calculate and display the distance double distance = calculateDistance3D(x1, y1, z1, x2, y2, z2); cout << "The distance between the two points in 3D is: " << distance << endl; return 0; }
Output
The distance between the two points in 3D is: 7.07107
Time Complexity: O(1)
Space Complexity: O(1)