We’ve underestimated the Pythagorean theorem all along. It’s not about triangles; it can apply to any shape. It’s not about a, b and c; it applies to **any formula** with a squared term.

It’s not about **distance** in the sense of walking diagonally across a room. It’s about **any distance**, like the “distance” between our movie preferences or colors.

If it can be measured, it can be compared with the Pythagorean Theorem. Let’s see why.

## Understanding The Theorem

We agree the theorem works. In any right triangle:

If a=3 and b=4, then c=5. Easy, right?

Well, a **key observation** is that a and b are at right angles (notice the little red box). Movement in one direction has **no impact** on the other.

It’s a bit like North/South vs. East/West. Moving North does not change your East/West direction, and vice-versa — the directions are independent (the geek term is **orthogonal**).

The Pythagorean Theorem lets you use find the **shortest path distance** between orthogonal directions. So it’s not really about right **triangles** — it’s about comparing “things” moving at right angles.

You:

If I walk 3 blocks East and 4 blocks North, how far am I from my starting point?Me:

5 blocks, as the crow flies. Be sure to bring adequate provisions for your journey.You:

Uh, ok.

## So what is “c”?

Well, we could think of c as just a number, but that keeps us in boring triangle-land. I like to think of c as a **combination of a and b**.

But it’s not a simple combination like addition — after all, c doesn’t equal a + b. It’s more a combination of components — the Pythagorean theorem lets us combine **orthogonal components** in a manner similar to addition. And there’s the magic.

In our example, C is 5 blocks of “distance”. But it’s more than that: it contains a **combination** of 3 blocks East and 4 blocks North. Moving along C means you go East and North at the same time. Neat way to think about it, eh?

## Chaining the Theorem

Let’s get crazy and chain the theorem together. Take a look at this:

Cool, eh? We draw **another** triangle in red, using c as one of the sides. Since c and d are at right angles (orthogonal!), we get the Pythagorean relation: c^{2} + d^{2} = e^{2}.

And when we replace c^{2} with a^{2} + b^{2} we get:

And that’s something: We’ve written e in terms of 3 orthogonal components (a, b and d). Starting to see a pattern?

## Put on your 3D Goggles

Think two triangles are strange? Try pulling one out of the paper. Instead of lining the triangles flat, tilt the red one up:

It’s the same triangle, just facing a different way. But now we’re in 3d! If we call the sides x, y and z instead of a, b and d we get:

Very nice. In math we typically measure the x-coordinate [left/right distance], the y-coordinate [front-back distance], and the z-coordinate [up/down distance]. And now we can find the 3-d distance to a point given its coordinates!

## Use Any Number of Dimensions

As you can guess, the Pythagorean Theorem generalizes to **any number of dimensions**. That is, you can chain a bunch of triangles together and tally up the “outside” sections:

You can imagine that each triangle is in its own dimension. If segments are at right angles, the theorem holds and the math works out.

## How Distance Is Computed

The Pythagorean Theorem is the basis for computing distance between two points. Consider two triangles:

- Triangle with sides (4,3) [blue]
- Triangle with sides (8,5) [pink]

What’s the distance from the tip of the blue triangle [at coordinates (4,3)] tot the tip of the red triangle [at coordinates (8,5)]? Well, we can create a **virtual triangle** between the endpoints by subtracting corresponding sides. The hypotenuse of the virtual triangle is the distance between points:

- Distance: (8-4,5-3) = (4,2) = sqrt(20) = 4.47

Cool, eh? In 3D, we can find the distance between points (x1,y1,z1) and (x2,y2,z2) using the same approach:

And it doesn’t matter if one side is bigger than the other, since the difference is squared and will be positive (another great side-effect of the theorem).

## How to Use Any Distance

The theorem isn’t limited to our narrow, spatial definition of distance. It can apply to **any orthogonal dimensions**: space, time, movie tastes, colors, temperatures. In fact, it can apply to any set of numbers (a,b,c,d,e). Let’s take a look.

## Measuring User Preferences

Let’s say you do a survey to find movie preferences:

- How did you like Rambo? (1-10)
- How did you like Bambi? (1-10)
- How did you like Seinfeld? (1-10)

How do we compare people’s ratings? Find similar preferences? Pythagoras to the rescue!

If we represent ratings as a “point” (Rambo, Bambi, Seinfeld) we can represent our survey responses like this:

- Tough Guy: (10, 1, 3)
- Average Joe: (5, 5, 5)
- Sensitive Guy: (1, 10, 7)

And using the theorem, we can see how “different” people are:

- Tough Guy to Average Joe: (10 – 5, 1 – 5, 3 – 5) = (5, -4, -2) = √(25 + 16 + 4) = 6.7
- Tough Guy to Sensitive Guy: (10 – 1, 1 – 10, 3 – 7) = (9, -9, -4) = √(81 + 81 + 16) = 13.34

We can compute the results using a^{2} + b^{2} + c^{2} = distance^{2} version of the theorem. As we suspected, there’s a large gap between the Tough and Sensitive Guy, with Average Joe in the middle. The theorem helps us **quantify this distance** and do interesting things like **cluster similar results**.

This technique can be used to rate Netflix movie preferences and other types of **collaborative filtering** where you attempt to make predictions based on preferences (i.e. Amazon recommendations). In geek speak, we represented preferences as a vector, and use the theorem to find the distance between them (and group similar items, perhaps).

## Finding Color Distance

Measuring “distance” between colors is another useful application. Colors are represented as red/green/blue (RGB) values from 0(min) to 255 (max). For example

- Black: (0, 0, 0) — no colors
- White: (255, 255, 255) — maximum of each color
- Red: (255, 0, 0) — pure red, no other colors

We can map out all colors in a “color space”, like so:

We can get distance between colors the usual way: get the distance from our (red, green, blue) value to black (0,0,0) [formally labeled delta e]. It appears humans can’t tell the difference between colors only 4 units apart; heck, even 30 units looks pretty close to me:

How similar do these look to you? The color distance gives us a **quantifiable** way to measure the distance between colors (try for yourself). You can even unscramble certain blurred images by cleverly applying color distance.

## The Point: You can measure anything

If you can represent a set of characteristics with numbers, you can compare them with the theorem:

- Temperatures during the week: (Mon, Tues, Wed, Thurs, Fri). Compare successive weeks to see how “different” they are (find the difference between 5-dimensional vectors).
- Number of customers coming into a store hour-by-hour, day-by-day, or week-by-week
- SpaceTime distance: (latitude, longitude, altitude, date). Useful if you’re making a time machine (or a video game that uses one)!
- Differences between people: (Height, Weight, Age)
- Differences between companies: (Revenue, Profit, Market Cap)

You can tweak the distance by weighing traits differently (i.e., multiplying the age difference by a certain factor). But the core idea is so important I’ll repeat it again: **if you can quantify it, you can compare it using the the Pythagorean Theorem.**

Your x, y and z axes can represent any quantity. And you aren’t limited to 3 dimensions. Sure, mathematicians would love to tell you about the other ways to measure distance (aka metric space), but the Pythagorean Theorem is the most famous and a great starting point.

## So, What Just Happened Here?

There’s so much to learn when revisiting concepts we were “taught”. Math is beautiful, but the elegance is usually buried under mechanical proofs and a wall of equations. We don’t need more proofs; we need interesting, intuitive results.

For example, the Pythagorean Theorem:

- Works for
**any shape**, not just triangles (like circles) - Works for
**any equation with squares**(like 1/2 m v^{2}) - Generalizes to
**any number of dimensions**(a^{2}+ b^{2}+ c^{2}+ …) - Measures
**any type of distance**(i.e. between colors or movie preferences)

Not too bad for a 2000-year old result, right? This is quite a brainful, so I’ll finish here for today (the previous article has more uses). Happy math.

## Leave a Reply

137 Comments on "How To Measure Any Distance With The Pythagorean Theorem"

[…] There’s much, much more to this beautiful theorem, such as measuring any distance. Enjoy. Posted October 24, 2007, under Math Tags: area, circle, hypotenuse, intuitive, Math, pythagorean, theorem, triangle Related Posts: […]

Very, very cool post.

I manage a data warehouse and get called in to analyze product data periodically — I see where I might be able to use this to some great effect.

I really need to go back to school and get a degree in statistics — if only school paid as well as data warehousing!

Thanks Bob, glad you liked it. Yes, there are tons of applications — if you have a warehouse full of product data, I’m sure there’s some interesting trends/groupings you’ll be able to pull up :)

This post pointed me to a possible issue in the new version of my political simulation game I’m currently developing: I use ideological axes to rate political parties’ positions, and I currently determine their similarity by taking the average difference for each axis. Of course, I need to average the squares of the differences!

Wow, this is very cool. I remember being taught this theorem in 4th grade math, but I never revisited it.

Now that I’m working on some things that could really benefit from collaborative filtering, I’m very happy to have come across this post!

This is just one of any number of distance functions. You might want to look up the others :

http://en.wikipedia.org/wiki/Metric_(mathematics)

Especially manhattan distance is useful. But it is by no means the only one. You describe euclidean distance, you also have manhattan distance, hamming distance, jaccard distance, accoustic metrics, …

Just as oele wrote there are more types of metrics. I’m not saying you should include any of that in this article but some of the things you say are simply not true, due to the fact that there are other metrics. Here’s where it goes wrong :”Measures any type of distance”. You should change this statement or remove it.

Apart from that, a very nice article!

I’m shocked that so many people didn’t know this. This is truly elementary material, and if articles like this need to be written for coders, the education system must be in a very sorry state.

Excellent, I knew all about Pythagoras theorem but I never thought about repeated applications and alternative uses.

Thanks, Andy.

@Wouter, Jesper: Thanks, I hope you have luck in applying it to your area :)

@oele: Thanks for the info — that may be a subject of a follow-up article.

@Josef: Thanks for the tip. I can rephrase to mean “provides one way to measure distance”. The key point is that the Pythagorean Theorem can generalize to N dimensions (it isn’t the only such theorem that does so).

@Ddd: Everyone has to start somewhere. And there’s always ways to look at “old” results in a new light.

@Andrew: Glad you liked it!

Ddd said, “I’m shocked that so many people didn’t know this. This is truly elementary material, and if articles like this need to be written for coders, the education system must be in a very sorry state.”

To start a dialog, I think you are confusion a computer science education with that of what coders do now a days. There are many programmers who have not had formal mathematical or computer science education. Therefore, there will be some that don’t have this “basic” skills as you call it.

So the computer science education (at least in some schools) has nothing to do with some people not knowing basic vector algebra. In other words, coders come in different flavours.

Thanks Jose, my thoughts exactly. Everyone starts in a different place: some people are designers who do programming on the side, others are programmers who do design on the side.

For some reason, math topics seem to encourage people to show off how much they know (and this attitude is partially why math isn’t a well-loved subject).

Me, I just like to write about what I find interesting, hoping other people enjoy it too.

Great post. I have used the multi-dimensional distance calculation to estimate similarity between all sorts of things, using different measures. The only thing to be careful about is scaling. The distances (dis-similarities) need to be re-weighted (or normalized), if scales are off.

It yields really interesting results.

vectors and matrices are good tools for studying the evolution of human behavior, see http://we.karleklund.net

@Matt: Thanks, glad you liked the post. You’re right, the multi-dimensional distance is a good starting point but needs to be tweaked/scaled appropriately.

@Karl: Thanks for the tip — there’s so many interesting uses.

[…] Math is beautiful This was just a very interesting read. I’ve always believed that math is more interesting than people give it credit for. Falling math standards are more due to bad teaching than the difficulty of the subject. […]

I was in a math class a few years ago where we proved the Pythagorean theorem multiple times in multiple ways. It was incredible to see how it works and why it works. I love the fact that math can be both incredibly complex and extremely simple all at the same moment.

I literally stumbled onto this post using StumbleUpon and think this is very cool. Like Bob I also work in Datawarehousing and on reading this post realised this concept could easily be applied to RFM (Recency, Frequency, Monetary) measures to identify groups for targeted marketing campaigns. Thanks.

Great article ! Thanks to share your knowledge.

Hi People,

Please correct me if I am missing something here.

As stated above this theorem

can apply to any orthogonal dimensions. In that sense if we need to measure the distance between people’s preference about movies then we have to first ensure that the three questions1. How did you like Rambo? (1-10)

2. How did you like Bambi? (1-10)

3. How did you like Seinfeld? (1-10)

are actually orthogonal. That is to say, there is no relation between the response that any random person would give for any two of the three questions. For instance if any one who like Bambi is also likely to like Seinfeld, then these questions are not orthogonal. I am not a maths/stats expert but I feel this can be checked by calculating the correlation coefficient between the responses to any two questions. The coefficient for all pairs should be close to 0, only then we can apply Pythagoras Theorem.

On similar lines we can say that temperatures on Mon through Friday are not likely to be orthogonal. Rather they are likely to be linear. If it has been hot on Monday, Tuesday is more likely to be hot than cold. The same is the case with Age, Height and Weight.

@Min: I agree, it can be really eye-opening to see old results in a new light, and how “simple” equations can be responsible for so much.

@Gav: Thanks for dropping by, I hope you find it helpful.

@Sylvain: You’re welcome, it’s a lot of fun and I end up learning so much when revisiting topics I thought I “knew”.

@Dhwani: Great point. Strictly speaking, the distance function works best when the quantities are orthogonal (i.e. movement in one direction has no impact on the other), but “correlation” shouldn’t be a factor. (I’m not a stats expert either, so don’t quote me on this :) ).

For example, it may be that everyone who moves North also moves East for some reason or another. But it doesn’t change how *far* they are from the starting point.

Similarly, there may be a correlation between liking Rambo and liking Bambi, but it doesn’t change the absolute distance between the preferences.

Having a correlation may cause preferences to be “clumped” along patterns (like being in the North-East) vs. being randomly distributed, but I think this is a separate issue from finding distance.

[…] his fault.Better Explained gets enthusiastic about Pythagoras. No Comments Leave a Commenttrackback addressThere was an error with your comment, please try again. name email (will not be published)url […]

Dear Kalid,

You write:

“For example, it may be that everyone who moves North also moves East for some reason or another. But it doesn’t change how *far* they are from the starting point.”

No, it doesen’t. But it changes how you can compute that distance. As an example: if you move first 3 units east and then 4 north, you are 5 units from the origin, according to Pythagoras. But if you first move 3 units east, and then 4 units north/east according to your comment, you are not 5 units from the origin. (you are slightly longer from the origin than 5 units in that case).

I forgot to say that I agree with Dhwani (post 18): it has everything to do with correlation.

Therefore, the description in you post is not correct for correlated data.

I also agree with oele and Josef (post 5 and 6) – there are hundreds of distance measures out there, each useful in a differenet application. While Euclidean distance is convenient and easy to compute, it may in many cases give a picture that is far from correct.

And Wouter Lievens (post 3): be careful, the measure you already use may or may not be better than Euclidean distance. There is nothing that says that Euclidean distance is the best measure for such cases.

[…] How To Measure Any Distance With The Pythagorean Theorem – […]