Lesson goal: The distance formula

Previous: Getting clicks from the mouse | Home | Next: Square roots and the Pythagorean Theorem

One of the first actual uses of the square root in math is in the distance formula, which is $$d=\sqrt{(x-x_0)^2+(y-y_0)^2}.$$ This formula will tell you the "straight line" distance between any two points on a cartesian coordinate system. As this formula is stated, one point will be $(x,y)$ and the other will be $(x_0,y_0)$. For example, if one point is at $(5,3)$ and the other point is at $(-3,17)$, the distance between them will be $d=\sqrt{(5-(-3))^2+(3-17)^2}$.

In this lesson, we'll see if you can program in the distance formula and have the computer calculate distances for you.

Now you try. Use the print statement to find the distance between two xy points.

Type your code here:


See your results here:

This code will not run as-is. You have to put in differences between x and y points in between the empty ( and ). For these, pick any two xy points on the cartesian coordinate system, and see what distance you can find between them. Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.