Tag Archives: #Quiz09

Quiz Week 9: The distance between coordinates

The task for today was only creating a function that could calculate the distance between two pair of coordinates on the cartesian coordinates plane that were given for the user. In the quiz was not necessary to create the entire program, but the function cause it was the only part required.

To make this part of the code, I utilized the Pythagoras Theorem cause if we conect the points given, we form a right triangle, of which we know its opposite and adjacent sides, and therefore we can calculate its hypotenuse. But for doing this we have to make a substraction between “x” points and then “y” points, so we have the exactly mesures. Then we apply the Pythagoras Theorem. And that’s all.

This is my resulting function:

float distance(float x1, float y1, float x2, float y2)
{
  float D;
  D= sqrt(pow((x2-x1),2) + pow((y2-y1),2));

  return D;
}