Skip to content
Binogi logo

Subjects

The distance between two points (programming)

The distance between two points (programming)

Video thumbnail
Play

One point has the coordinate (4,6). What is then the x coordinate?

The distance between two points (programming)

Lina is programming a game where the player is a cat avoiding being caught by a cat catcher. If the distance between the cat and the cat catcher is less than the length of the landing net, the cat is caught. How can the program determine if the cat is caught? We need to know the distance between the cat and the cat catcher. We can split the screen into a grid.

In mathematics we call it a coordinate system. The horizontal axis is called the x-axis and the vertical, the y-axis. We read the x-value and the y-value for the cat and the cat catcher. The cat has the value x equals zero and y equals zero. The cat catcher has x-value 30 and y-value 20.

We need another thing from mathematics, and that is Pythagoras’ theorem. Why do we need this? Well, look here! We draw a line between the cat and the cat catcher. Then a horizontal line from the cat to the cat catchers x-value.

Finally, we draw a vertical line from the cat catcher to the cat’s y-value. What geometric figure is this? Well, a right-angled triangle. And the hypotenuse in it is the distance between the cat and the cat catcher. If the landing net is at least as long as the hypotenuse then the cat is caught!

We use Pythagoras’ theorem to calculate the triangle’s hypotenuse. Before we start with the programming, we write all this down as clear instructions: as pseudocode. We need to know the coordinates for the cat catcher and the cat. Note the x and y-values for the cat catcher. Note the x and y-values for the cat.

Note the length of the landing net. We want to calculate the length of the hypotenuse of the triangle, which here is called ‘c’ in Pythagoras’ theorem. Side ‘a’ we get by calculating the difference between the cat catcher's x-value and the cat's x-value. Create variable ‘a’, assign the value of the cat catcher's x-value minus the cat's x-value. We get side ‘b’ by calculating the difference between the cat catcher's y-value and the cat's y-value.

Create variable ‘a2’ and assign it the value of ‘a’ times ‘a’. The same thing with ‘b’. Calculate ‘b’ squared and save the product in a new variable with name ‘b2’. Create ‘c2’ and assign it the value of: the sum of ‘a2’ and ‘b2’. ‘c2’ is the square of the hypotenuse. To calculate the hypotenuse we take the square root of variable ‘c2’ and save the result in a variable called 'c'.

Now we have ‘c’ which is the distance between the cat and the cat catcher. Time to try it out: If the distance c is longer than the length of the landing net, say the cat escapes. Else, say the cat is captured! Let’s make it clear which rows in the pseudocode go together. We’ll tab the rows that start with “Say”.

This one only executes when the if-row is true, and this one only executes when the else-row is true. Now the only thing is for the cat to keep as far away from the cat catcher as it can.