Template Class closest_pair2D

Class Documentation

template<typename T>
class closest_pair2D

Calculate the closest two points from a set of 2D points in O(NLogN) (kRecursive).

Also has a brute force (O(N^3)) algorithm (kBruteForce) that can be used for comparision.

Returns the two closest points, along with the distance between them (result_type2D).

Template Parameters:

T – Type of point in 2D plane. Can be any class, but must provide the following methods: x(), y(), operator==(). See math::vector2 for example implementation).

Public Functions

template<typename TDistFunc>
inline result_type2D<T> brute_force(const std::vector<T> &points, const TDistFunc &dist_func)

Find the closest pair of points using brute force.

Parameters:
  • points – The set of points to search.

  • dist_func – The comparision function to use.

Returns:

The two closest points, along with the distance between them.

template<typename TDistFunc>
inline result_type2D<T> operator()(const std::string &method, std::vector<T> points, const TDistFunc &dist_func)

Run the calculation algorithm.

Parameters:
  • method – The method to use: “brute_force” or “recursive”.

  • points – A vector of points through which to search.

  • dist_func – A function that can be used to calculate the distance between two points.

template<typename TDistFunc>
inline result_type2D<T> recursive(const std::vector<T> &points, std::vector<T> &strip, const TDistFunc &dist_func)

Find the closest pair of points using recursion.

Parameters:
  • points – The set of points to search through.

  • strip – Space for the strip.

  • dist_func – The comparision function to use.

Returns:

The two closest points, along with the distance between them.

Public Static Attributes

static const std::string kBruteForce = "brute_force"
static const std::string kRecursive = "recursive"