The **Euclidean distance**, in any n-dimensional plane, is the length of a line segment connecting the two points.

The Pythagorean Theorem can be used to calculate the distance between two points, as shown in the figure below. If the points $(x_1,y_1)$ and $(x_2,y_2)$ are in *2-dimensional* space, then the Euclidean distance between them, represented by `d`

, is: $\sqrt{(x_2-x_1)^{2}+(y_2-y_1)^{2}}$

Similarly, if the points are in a *3-dimensional* space, the Euclidean distance will be represented by: $\sqrt{(x_2-x_1)^{2}+(y_2-y_1)^{2}+(z_2-z_1)^{2}}$

In general, for two points

`p`

and`q`

given by Cartesian coordinates in ann-dimensionalspace,`p`

is represented by $(p_1,p_2, ..., p_n)$,`q`

is represented by $(q_1, q_2,...,q_n)$, and the Euclidean distance is: $\sqrt{(p_1-q_1)^{2}+(p_2-q_2)^{2}+(p_3-q_3)^{2}+....+(p_n-q_n)^{2}}$

For reference, below is the implementation of Euclidian distance calculation in C#, using the `Math`

class to take the `square`

and `square-root`

.

using System;namespace eucledian_distance{class Program{static void Main(string[] args){double x1, x2, y1, y2;x1 = 3;x2 = 4;y1 = 5;y2 = 2;var distance = Math.Sqrt((Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)));Console.WriteLine($"The Eucledian distance between the two points is: {Math.Round(distance, 4)}");}}}

Copyright ©2024 Educative, Inc. All rights reserved

TRENDING TOPICS