Calculating Distance Readme
Objectives
- Learn about visualizations
- Use python to access data for a visualization.
Calculating the distance
Previously, we used visualizations to evaluate the closest customers to our salesperson, Bob. Now that can work when we only have one salesperson, but when our business grows to have more salespeople, or when it becomes more difficult to approximate who is further away, we need a more formalized approach. So let's see if there's a way that we can figure out the distance between Bob and a customer.
Once again, here are the locations of Bob and our customers:
Name | Avenue # | Block # |
---|---|---|
Bob | 4 | 8 |
Suzie | 1 | 11 |
Fred | 5 | 11 |
Edgar | 6 | 13 |
Steven | 3 | 5 |
Natalie | 5 | 4 |
Test | 10 | 12 |
And here are these locations in the form of a scatter plot.
Now, there are no labels in this particular plot, but we should be able to make sense of it anyway. Our table says that Bob is located at avenue 4 and block 8, and when we look at where the x-axis reaches 4 and the y-axis reaches 8, we see a marker right there - that's Bob.
Now are next task is to calculate the distance between Bob and another customer. Let's choose how far Bob at point (4, 8) is from the customer at point (3, 6).
Calculating distance
Here's our first approach at calculating distance. To go from 8th street and 4th avenue to 6th street and 3rd avenue, we simply go two blocks down and one block to the left for a distance of three blocks.
Now that's a good start, but it's not how mathematicians would calculate distance between points. The definition of distance is the length of shortest path between two points. So imagine, if it helps, that we made our deliveries with the help of a drone. So then which path from (4, 8) to (3,6) is shortest? We won't prove it, but one single straight line between any two points is the shortest path between them. So the shortest path between (4,8) and (3, 6) is a straight diagonal line between them.
That blue diagonal line between the two points is the shortest path between the two points. So by definition, the length diagonal line represents the distance. So now we need to calculate the length of that line.
Bring in the Pythagoros
Ok, to calculate that line, we realize that it forms a right triangle going from the lowest x-value to the highest x-value, 3 to 4 and from the lowest y-value to the highest y-value. Take a look below, a nice triangle.
Formally, a right triangle is any triangle where one of the angles is 90 degrees -- also called the right angle. But you can also just know that, if one of the sides are perfectly vertical and perfectly horizontal we have a right triangle -- and above we drew our triangle to be just like that. The reason why we drew this is because, given the length of two sides of a right triangle we can find the length of the diagonal line - and the length of the diagonal line is the distance between our two points. That diagonal line is called the hypotenuse. The hypotenuse is the always longest line of a right triangle, and it is always directly opposite the right angle.
This is our formula, for calculating the length of the diaganol line: the length of the first side squared plus the length of the second side square equals the length of the hypotenuse squared. We already calculated the horizontal side by subtracting our first x-value from our second x-value, then taking the absolute value. The vertical side we calculated by subtracting the first y-value from the second y-value then taking the absolute value. So we have.
|(x1 - x2)|^2 + |(y1 - y2)|^2 = (len_of_hypotenuse)^2
Which, using the triangle above translates to:
|(3 - 4)|^2 + |(8 - 6)|^2 = (len_of_hypotenuse)^2
1^2 + 2^2 = (len_of_hypotenuse)^2
4 + 1 = (len_of_hypotenuse)^2
So simply by plugging in data, we can see that five equals the length of the hypotenuse, squared.
Squares, Square Roots, and Inverses
Squares - Squaring something simply means multiplying something by itself. So for example, 5 squared equals 5
$\times$ 5. Four squared equals 4$\times$ 4. We denote four squared with the a raised number 2, as in$4^2$ . The two is the number of times we are multiplying four by itself.
Square Roots - Now we can go from a number's square back to the original square with the square root. For example, the square of 4 is 4
$\times$ 4, which equals 16. And the square root of 16 should undo the operation of squaring, so the square root of 16 equals four, and we denote the square root of 16 as$\sqrt{16}$ .
**Inverses ** - In mathematics, we the inverse is anything that undoes the operation. So the inverse of squaring is taking the square root. The inverse of multiplying by ten is dividing by ten. Here is a question:
What is the inverse of putting shoes on?
Well, just think of the definition - inverse means undoing the operation, and the undoing putting shoes on is taking shoes off.
So, now we have the famous Pythagorean Formula, which is in a right triangle, where c is the length of the hypotenuse, and a and b are the other two sides:
Then, we translate the length of the horizontal sides into lengths by using our x and y values of the points:
Now we have a way to solve
or
where c is the length of the hypotenuse, or the distance between our two points.
For the confused, read this
Let's clarify two things:
** 1. Why can we simply take the square root to get the length of the hypotenuse? **
** 2. Why do we no longer take the absolute value in calculating distances **
So now, simply plugging in our data we see that:
which equals
which equals
Writing
Summary
Ok, that was a lot of math, but it was worth it. We now know that to calculate the distance between any two points we can simply square the differences the between the points's x and y values, sum the squares, and then take the square root of the sum to calculate the distance between two points.
We can do this because a single line is the shortest path between two points, also known as the distance between two points. Because the Pythagorean Theorem gives us a formula