How to compute Manhattan Distance between two points in C++

In this tutorial, we will be learning how to compute Manhattan Distance between two points in C++. Manhattan Distance is a very simple distance between two points in a Cartesian plane. The program can be used to calculate the distance easily when multiple calculations using the same formula are required.

Compute Manhattan Distance between two points in C++

The Manhattan Distance is required to be calculated in various mathematics problems. It is used as a measure to calculate difference between the position of points in the Cartesian form. Manhattan Distance is also used in some machine learning (ML) algorithms, for eg. KNN algorithm (K Nearest Neighbours). When distances for multiple pairs of points are to be calculated, writing a program for the same can save a lot of time. Let us see the steps one by one.

 

1. Libraries

Code :

#include <iostream>
#include <cmath>

iostream : basic input and output functions

cmath : mathematical functions

 

2. Input function

Code :

void input(double &x, double &x1, double &y, double &y1)
{

  cout<<"\nCalculate Manhattan Distance\n";
  
  cout<<"\nP1(x1,y1)";
  cout<<"\nEnter x1 : ";
  cin>>x;
  cout<<"Enter y1 : ";
  cin>>y;

  cout<<"\n\nP2(x2,y2)";
  cout<<"\nEnter x2 : ";
  cin>>x1;
  cout<<"Enter y2 : ";
  cin>>y1;

}

In this function, we take the input values from the user which is the values for x and y coordinates for two points. These are the points that we need to calculate the Manhattan Distance for. The parameters are passed by reference to store the user entered data in the original objects itself and avoid local storage.

Output :

Calculate Manhattan Distance

P1(x1,y1)
Enter x1 : 1
Enter y1 : 3


P2(x2,y2)
Enter x2 : 3
Enter y2 : 5

 

3. Manhattan Distance Calculation

The Manhattan Distance between two points is calculated using a simple formula.

How to compute Manhattan Distance between two points in C++

Code :

void manhattan_distance(const double x1, const double x2, const double y1, const double y2)
{

  double distance;
  int x_dif, y_dif;
  
  x_dif = x2 - x1;
  y_dif = y2 - y1;

  if(x_dif < 0)
    x_dif = -x_dif;

  if(y_dif < 0)
    y_dif = -y_dif;

  distance = x_dif + y_dif;


  cout<<"\n\nManhattan Distance between P1("<<x1<<","<<y1<<") and P2("<<x2<<","<<y2<<") : "<<distance;

}

The values for x1, x2, y1, y2 are put in the formula to calculate the value for distance. The condition for the modulus in the formula is checked using if() condition. Negative values are converted to positive values using ‘-‘ operator.

The difference in the x coordinates for both the points is calculated. The same is done for the y coordinates. Both these values checked and positive values are added to calculate the final Manhattan Distance.

The input() and manhattan_distance() functions are called in the main() where the variables are declared.

Output :

Manhattan Distance between P1(1,3) and P2(3,5) : 4

 

Complete Code

#include <iostream>
#include <cmath>

using namespace std;

void input(double &x, double &x1, double &y, double &y1)
{

  cout<<"\nCalculate Manhattan Distance\n";
  cout<<"\nP1(x1,y1)";
  cout<<"\nEnter x1 : ";
  cin>>x;
  cout<<"Enter y1 : ";
  cin>>y;

  cout<<"\n\nP2(x2,y2)";
  cout<<"\nEnter x2 : ";
  cin>>x1;
  cout<<"Enter y2 : ";
  cin>>y1;

}

void manhattan_distance(const double x1, const double x2, const double y1, const double y2)
{

  double distance;
  int x_dif, y_dif;

  x_dif = x2 - x1;
  y_dif = y2 - y1;

  if(x_dif < 0)
    x_dif = -x_dif;

  if(y_dif < 0)
    y_dif = -y_dif;

  distance = x_dif + y_dif;
  cout<<"\n\nManhattan Distance between P1("<<x1<<","<<x2<<") and P2("<<y1<<","<<y2<<") : "<<distance<<endl;

}

int main(void)
{

  double x1,x2;
  double y1,y2;

  input(x1,x2,y1,y2);
  manhattan_distance(x1,x2,y1,y2);

  return 0;

}

Output  :

Calculate Manhattan Distance

P1(x1,y1)
Enter x1 : 1
Enter y1 : 3


P2(x2,y2)
Enter x2 : 3
Enter y2 : 5


Manhattan Distance between P1(1,3) and P2(3,5) : 4

 

You may also learn,

Python Program to Compute Euclidean Distance

K – Nearest Neighbor Algorithm (KNN)

Leave a Reply

Your email address will not be published. Required fields are marked *