Coordinate help

I need to make a program that will determine the closest distance between coordinates. I have coordinates (4, 3.5), (3.5,7), (7.5, 4) and (7.2,7.2). The user will input their own coordinates and the program has to find which of the above coordinates is closest. I been having trouble with where to start so any tips are greatly appreciated! This is only part of a larger program.
Last edited on
Have a class/struct to store a point.
Store the static points in a std::vector.
Ask the user to a input point.

Assume that the closest point is the first point (index 0) and the closest distance is the distance from user's point and first point.
Loop though all the points in the std::vector and find the distance between user's point and current point.
To find the distance between (x0, y0) and (x1, y1) do
 
sqrt((x0 - x1)² + (y0 - y1)²)

If the distance is less than the closest distance, then update the closest point to be the current index and the closest distance to be the current distance.

The algorithm is similar to one if you were to find the smallest element in an array, for example.
Last edited on
Topic archived. No new replies allowed.