Calculating distance

One of the formulas, that you will use all the time, is the formula that calculates the distance between two points on the screen. As far as I know it is an old invention from the old Greeks, more specific the guy called Pythagoras. He made a discovery, that ensured his fame long into this century - the Pythagorean Theorem.

The Pythagorean Theorem

Used in geometry, this theorem states:

In a right-angled triangle the area of the square on the hypotenuse is equal to the sum of the areas of the squares of the other two sides.

The odd word here is the hypotenuse. This is the name for the side that is opposite to the right angle. As you can read, the theorem deals with a bunch of squares - rectangles with equal width and height - and the relation between the hypotenuse, and the two others. So in plain English the theorem states:

If you start from the right angle, and make two squares extend with the length of of the sides that extends from it, the sum of these two areas combined is the same as one single square with a width of the long side on the opposite side of the right angle.

​You see, I tried to explain it simpler that the original theorem, but failed miserably. Maybe both quotes combined with the formula and a diagram does the job:

​a2 + b2 = c2

​Reverse engineer it, please

The tricky part here is that we just want the side and not an area. Luckily it isn't even a tricky part, because there is an easy way to calculate the side of a square, based on the area - the square root - well thought name. The root for the definition of a square is to take a length, and multiply it with it self. So we end up with

c = sqr((a * a) + (b * b))

C is almost always the length, we wan't to know, and A and B is almost always the values w know, because from any given two points you can draw a horizontal and vertical line. Find where the lines intersect, and measure the length from the intersection and out to points.

Fundamental implementations

If you want to find a distance between two points, this is how it is achieved in:

ActionScript

var a:Point = new Point(100, 100);
var b:Point = new Point(200, 200);
var l:int = b.x - a.x;
var h:int = b.y - a.y;
var c:Number = Math.sqrt((l*l) + (h*h));
trace(c);

 

JavaScript

var a =  {x:100, y:100};
var b =  {x:200, y:200};
var l =  b.x - a.x;
var h =  b.y - a.y;
var c = Math.sqrt((l*l) + (h*h));
console.log(c);

 

Processing

int[] a = {100, 100}; // {x,y}
int[] b = {200, 200}; // {x,y}
int l = b[0] - a[0];
int h = b[1] - a[1];
float c = sqrt((l*l) + (h*h));
println(c);

 

Java

int[] a = {100, 100}; // {x,y}
int[] b = {200, 200}; // {x,y}
int l = b[0] - a[0];
int h = b[1] - a[1];
float c = (float) Math.sqrt((l*l) + (h*h));
System.out.println(c);

 

 

Other implementations

Some of the more visual oriented languages like ActionScript and Processing has specific methods for calculating distances. ActionScript has a Point class, that only deals with points and the relation between them. In the same manner, Processing has a dist() method that that calculates the distance based on the given x- and y- coordinates, like this:

ActionScript

var a:Point = new Point(100, 100);
var b:Point = new Point(200, 200);
var c:Number = Point.distance(a,b);
trace(c);

Processing

int[] a = {100, 100};
int[] b = {200, 200};
float c = dist(a[0], a[1], b[0], b[1]);
println(c);