Programming Lesson: Distance Between Two Points

Assignment: Write a program to find the distance between two points.

To find the distance (c) between two points (x1, y1) and (x2, y2) you use the equation:

  c = \sqrt{a^2 + b^2}

where a and b are:

  a = x_2 - x_1
  b = y_2 - y_1

So, let’s find the distance between the points (1,7) and (5, 2) using python:

# define inputs
x1 = 1
y1 = 7
x2 = 5
y2 = 2

# calculate distance
a = x2 - x1
b = y2 - y1

c = (a**2 + b**2)**0.5   

# output the result
print("Distance between two points.")
print("Point 1:", x1, y1)
print("Point 2:", x2, y2)
print("Distance:", c)

The result should look like:

Distance between two points.
Point 1: 1 7
Point 2: 5 2
Distance: 6.40312

Leave a Reply