How To Measure Any Distance With The Pythagorean Theorem
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: c2 + d2 = e2.
And when we replace c2 with a2 + b2 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:
1. How did you like Rambo? (1-10)
2. How did you like Bambi? (1-10)
3. 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) = 6.7
- Tough Guy to Sensitive Guy: (10 - 1, 1 - 10, 3 - 7) = (9, -9, -4) = 13.34
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 v2)
- Generalizes to any number of dimensions (a2 + b2 + c2 + …)
- 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.
66 Comments »
Trackbacks & Pingbacks
-
Pingback by Surprising Uses of the Pythagorean Theorem | BetterExplained — November 5, 2007 @ 9:28 am
-
Pingback by Math is beautiful « Words of (Semi) Importance — November 6, 2007 @ 10:53 pm
-
Pingback by Shine, Perishing Republic « Vlorbik on Math Ed — November 7, 2007 @ 4:21 pm
-
Pingback by QuickLinks vom 4. November bis zum 8. November — instant-thinking.de — November 8, 2007 @ 5:31 pm
-
Pingback by The Pythagorean Theorem — November 11, 2007 @ 8:23 am
-
Pingback by Understanding Quake’s Fast Inverse Square Root | BetterExplained — November 29, 2007 @ 1:14 pm
-
Pingback by The Best Of November… | Peety Passion — December 5, 2007 @ 3:24 am
-
Pingback by Carnival of Mathematics #22 » Fun Math Blog — December 14, 2007 @ 12:28 pm
-
Pingback by A Visual, Intuitive Guide to Imaginary Numbers | BetterExplained — December 23, 2007 @ 10:24 am
-
Pingback by نظرات دو ستونه و وبلاگ ریاضی — January 5, 2008 @ 10:45 am
-
Pingback by Rescaling the Pythagorean Theorem | BetterExplained — February 16, 2008 @ 1:17 pm
-
Pingback by How To Analyze Data Using the Average | BetterExplained — March 14, 2008 @ 12:24 pm
-
Pingback by A Gentle Introduction To Learning Calculus | BetterExplained — May 3, 2008 @ 9:34 am
Comments
RSS feed for comments on this post. TrackBack URI
Leave a comment
Have a question? Know an explanation that caused your own a-ha moment? Write about it here.




RSS

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!
Bob — November 5, 2007 @ 12:58 pm
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
Kalid — November 5, 2007 @ 1:19 pm
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!
Wouter Lievens — November 6, 2007 @ 2:38 am
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!
Jesper — November 6, 2007 @ 3:09 am
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, …
oele — November 6, 2007 @ 3:17 am
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!
Josef Svenningsson — November 6, 2007 @ 6:13 am
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.
Ddd — November 6, 2007 @ 7:08 am
Excellent, I knew all about Pythagoras theorem but I never thought about repeated applications and alternative uses.
Thanks, Andy.
Andrew White — November 6, 2007 @ 8:58 am
@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!
Kalid — November 6, 2007 @ 9:06 am
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.
Jose Sandoval — November 6, 2007 @ 11:55 am
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.
Kalid — November 6, 2007 @ 1:40 pm
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.
Matt — November 6, 2007 @ 3:31 pm
vectors and matrices are good tools for studying the evolution of human behavior, see http://we.karleklund.net
Karl Eklund — November 6, 2007 @ 3:42 pm
@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.
Kalid — November 6, 2007 @ 9:34 pm
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.
Min — November 7, 2007 @ 1:03 am
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.
Gav — November 7, 2007 @ 2:16 am
Great article ! Thanks to share your knowledge.
Sylvain Romiguier — November 7, 2007 @ 3:14 am
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 questions
1. 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.
Dhwani — November 7, 2007 @ 4:26 am
@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.
Kalid — November 7, 2007 @ 2:43 pm
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).
Thomas — November 8, 2007 @ 11:28 am
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.
Thomas — November 8, 2007 @ 11:39 am
So where’s the link to download the source code?
Clinton Gallagher — November 9, 2007 @ 10:21 pm
@Thomas: Thanks for dropping by. I should have been more clear by what I meant with the term correlation.
If there’s a “physical” reason why moving East would also move North, then yes, distance can’t be measured using the theorem.
If moving East pushed you North as well (due to a strange gust of wind) then you couldn’t measure distance accurately, as someone moving East would have “free” distance in the North direction as well.
If there’s simply a “psychological” reason (i.e., everyone decides to move along a line where x = y) then the theorem can work. Imagine people all decide to walk along a path directly North-East; they aren’t *required* to, but just happen to do so. In that case there is a 1-1 correlation between their North and East location, but distance can still be computed fine.
@Clinton: Not sure what you mean about source code!
Kalid — November 10, 2007 @ 12:34 pm
I don’t think kids realise when they learn this how important this is - I just used Pythagoras last week to calculate the area in my bay window. My girlfriend thought I was a genius.
LLLL — November 14, 2007 @ 1:31 am
For LLLL,
What was the shape of your bay window?
DJ — November 14, 2007 @ 2:55 am
Wow, amazing!
Chris Meisenzahl — November 14, 2007 @ 6:01 am
This is a but a special case. The special portion is the fact that space must be euclidean, that is flat. This will fail if used in a curved space (like the one we live in) over a long distance. What you really want to talk about is the Metric Tensor on a Riemannian manifold.
george — November 14, 2007 @ 7:05 am
In 3 dimensions the familiar pythagorean theorem is a special case of the law of cosines (http://en.wikipedia.org/wiki/Law_of_cosines) which contains the inner product. This formula does NOT extend to higher dimensions in a trivial way (the inner product term gets more complicated). As always be careful in generalizing a special case!
george — November 14, 2007 @ 7:16 am
@Chris: Thanks, glad you liked it.
@George: Thanks for the info, it may be helpful for people who want to dive into more details [I may do a separate follow-up on these topics].
Kalid — November 14, 2007 @ 1:11 pm
Good job. Just add a warning against abuse.
“Qualitative is nothing but poor quantitative.”
Rutherford
“Not everything that counts can be counted, and not everything that can be counted counts.”
Einstein
My heart is with old man Bert here. I’m sick of meaningless measures overdose. Think a moment before you produce your whatevermeter.
Martín — November 15, 2007 @ 8:10 pm
Hi Martin, thanks for dropping by. Yes, when you have a hammer everything starts looking like a nail.
It’s good to bend your brain with new uses, but all things in moderation. Don’t go trying to rank your favorite kid this way
.
Kalid — November 16, 2007 @ 2:33 am
Prior to reading your post, I had to determine if my 16 foot canoe would fit in the cheapest rental space. The dealer said (remarkably) that it might, if I could figure the length, width, height dimensions properly (I was surprised that he knew it could be calculated, and if so, why he was holding a job renting storage spaces). The result of my calculation (Pythagoras Theorem in 3 dimensions) predicted that I wouldn’t be able to close and lock the overhead door if the canoe were laid flat on the floor, but I might be able to it one end of the canoe were raised up to the ceiling in the storage space. Interesting post, and it certainly promises to go beyond the usual conceptual bounds of three dimensions.
paddingtonstation — November 19, 2007 @ 4:57 pm
Hi, I’m glad it was useful for you! (This is about as real-world as it gets).
Just be sure to leave a bit of buffer — the Pythagorean Theorem works well for things like strings or rods of zero-thickness; make sure the sides of the canoe don’t hit the corners
Kalid — November 19, 2007 @ 5:12 pm
hello, very nice article. i really enjoyed it. my friends are appreciating too [:)]
Anonymous — November 20, 2007 @ 3:37 am
:)
Anonymous — November 20, 2007 @ 3:38 am
Glad you liked it!
Kalid — November 20, 2007 @ 10:00 am
I just discovered this site and love it! I am studying engineering in school, but I hate to learn things (especially math) without really understanding them- this page has really opened up a new way for me to think of dot products in 3dims, as well as other concepts I have learned from a different angle. thanks!!!
al — November 23, 2007 @ 1:57 am
Hi Al, thanks for the comment! Yes, I also hate learning things without a deep understanding — I’m glad you’ve found the articles useful
Kalid — November 27, 2007 @ 4:01 am
Could someone let me know how to measure the distance of a function like y(t) = at^2 + bt + c where t is time and a, b and c are constants ?
sridhar chitta — November 29, 2007 @ 9:23 am
Hi sridhar, I’m not quite sure of the question. If distance is one-dimensional and based on time, then you can simply take the difference [y(t2) - y(t1)].
Kalid — November 30, 2007 @ 11:54 pm
i have maths homework -
it sais
find the distance between these two poiints by using the pythagorean theorem
and i still have no idea how do to it
jess. — December 9, 2007 @ 6:39 am
Hi Jess, for problems like this you need to plug in values for a and b, and use the equation
c = square root of(a^2 + b^2). You might want to ask your math teacher to show with a diagram. Hope this helps.
Kalid — December 14, 2007 @ 12:41 pm
Wow that is intense
jade — January 10, 2008 @ 7:17 pm
How do you find an angle using triginometry?
michael — January 10, 2008 @ 7:18 pm
@jade: Thanks for the comment, some of this stuff can be brain-bending at first.
@michael: You can find the angle in a right triangle using the arc tangent (called atan). On a calculator you can do atan(b/a) where b is the height and a is the width. In a 3-4-5 right triangle (a=3, b=4, c=5), you could do atan(4/3) and get 53.13 degrees.
Kalid — January 10, 2008 @ 7:59 pm
Looks like a math trick
Wonder if this very technic is utilized by some applications, like Photoshop.
collector — January 22, 2008 @ 7:30 am
Hi collector, yep, I’m sure Photoshop must use color distance in some of its transformations (brightness/contrast/color shifting).
Kalid — January 28, 2008 @ 7:08 pm
Great! Was led to this post while searching for better (graphical) explanations of measuring distances. Thanks!
Hrishikesh — February 13, 2008 @ 6:54 am
Thanks Hrishikesh, glad you enjoyed it.
Kalid — February 14, 2008 @ 2:11 pm
The color example was exactly what I was looking for. I have been trying to explain this to a sales person for months. (I work for a machine vision company.) Your explanation is perfect. Thank you!
Lowell — March 11, 2008 @ 7:08 pm
THX
grace — April 16, 2008 @ 3:20 pm
@Lowell: I’m glad you enjoyed it! Happy you were able to make use of it — I like the color example because it appears to have “nothing” to do with geometry.
@Grace: You’re welcome.
Kalid — April 27, 2008 @ 11:20 am