# Distance between two points

### From Devipedia

For visual learners, it is often helpful to turn symbols into pictures. For some programmers, it is helpful to see mathematics expressed as source code. Here I attempt to combine both concepts, using Python to codify proofs and the Turtle library to visually demonstrate the concepts.

We will use the Pythagorean theorem, which describes the relation among three sides of a right triangle, in the next few examples.

http://upload.wikimedia.org/math/3/a/e/3ae71ab3eb71d3d182a3b9e437fba6ee.png

First we need two points. Imagine a piece of graph paper or a simple grid. We'll use cartesian coordinates to select two points. Any two points will do, but I'll use (12,8) and (2,3). Twelve and two are X coordinates (horizontal) and eight and three are Y coordinates (vertical). If you are indeed attempting to visualize this, you must imagine that some central point on the grid is (0,0). If you have an actual piece of paper (rare in these digital times) you could actually draw a dot and mark it as (0,0). Then to find the point (12,8) you would count cells to the right of the zero point until you get to the twelfth point, then count up the cells for eight steps. Now you have located the (12,8) point. The process is the same to find (2,3)---start at the zero and count your way to the desired point.

Here we use Turtle to visualize. Use python to run the following:

from turtle import * setworldcoordinates(-15,-15,15,15) penup() goto(2,3) pendown() dot(10, 'red') goto(12,8) dot(10, 'red')

When you run you will see a window appear with two red dots (the points) and a line between them.

To find the distance between two points (the red dots) we solve for c, which is the hypotenuse of the right triangle (the line you see connecting the two red dots).

http://upload.wikimedia.org/math/4/9/a/49a65217b7d35663efc6e558c0ffdba0.png

So how do we get a and b?

a = x1 - x2 --> a = 12 - 2 b = y1 - y2 --> b = 8 - 3

So a = 10 and b = 5

Expressed as code:

from math import * x1 = 12.0 x2 = 2.0 y1 = 8.0 y2 = 3.0 c = sqrt( pow((x1-x2),2) + pow((y1-y2),2)) print c

When you run this program you get 11.180339887498949.

Next we'll combine the two code snippets and test our result...

from turtle import * from math import * setworldcoordinates(-15,-15,15,15) # define our points x1 = 12.0 x2 = 2.0 y1 = 8.0 y2 = 3.0 penup() goto(x1,y1) pendown() dot(10, 'red') # the first point goto(x2,y2) # draws a thin line to the second point dot(10, 'red') # the second point # calculate the length of the hypotenuse c = sqrt( pow((x1-x2),2) + pow((y1-y2),2)) # Draw a line forward the length of c and visually check # our result by comparing the length to our original line. forward(c) # draws a line from the second point to the right for a distance of c

Are you convinced that the second line is the same length as the line between the two red dots? In the golden olden days, you might have used a ruler to check your results. You can do the same thing now: just hold it up against the computer screen and measure each line.