La distanza tra due punti $latex P = (x_{1}, y_{1}) $, $latex Q = (x_{2}, y_{2}) $ è calcolabile, utilizzando il teorema di Pitagora, mediante la formula:

[latex] \sqrt{ (x_{1} – x_{2})^{2} + (y_{1} – y_{2})^{2} } [/latex]

Il seguente codice C++ fornisce una soluzione, introducendo una classe Point, rappresentante un punto del piano cartesiano:

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
	private:
		double x;
		double y;
	public:
		Point(double x, double y);
		double X();
		double Y();
		double distance(const Point& other);
		static double distance(const Point& first, const Point& second);
};

Point::Point(double x, double y) : x(x), y(y){}

double Point::X()
{
	return this->x;
}

double Point::Y()
{
	return this->y;
}

double Point::distance(const Point& other)
{
	return sqrt( pow(this->x - other.x, 2) + pow(this->y - other.y, 2) );
}

double Point::distance(const Point& first, const Point& second)
{
	return sqrt( pow(first.x - second.x, 2) + pow(first.y - second.y, 2) );
}

int main()
{
	Point first(1,2);
	Point second(2, 8);

	cout << first.distance(second) << endl;
	cout << Point::distance(first, second) << endl;
}

Il seguente codice, invece, implementa lo stesso algoritmo in C#:

using System;

namespace Test
{
    class Program
    {
        class Point
        {
            private double x;
            private double y;

            public Point(double x, double y)
            {
                this.x = x;
                this.y = y;
            }

            public double X
            {
                get { return this.x; }
                set { this.x = value; }
            }

            public double Y
            {
                get { return this.y; }
                set { this.y = value; }
            }

            public double distance(Point other)
            {
                return Math.Sqrt(
                    Math.Pow(this.x - other.x, 2) +
                    Math.Pow(this.y - other.y, 2));
            }

            public static double distance(Point first, Point second)
            {
            return Math.Sqrt(
                    Math.Pow(first.x - second.x, 2) +
                    Math.Pow(first.y - second.y, 2));
            }
        }

        static void Main(string[] args)
        {
            Point first = new Point(1, 2);
            Point second = new Point(2, 8);
            Console.WriteLine(first.distance(second).ToString());
            Console.WriteLine(Point.distance(first, second).ToString());
        }
    }
}