allwpilib icon indicating copy to clipboard operation
allwpilib copied to clipboard

Pose2d "nearest" method

Open Oblarg opened this issue 2 years ago • 3 comments

would be convenient for Pose2d to have a nearest(List<Pose2d> poses) method that finds the nearest of a set of reference poses to the calling pose.

Oblarg avatar Mar 28 '22 22:03 Oblarg

In C++, this is just https://en.cppreference.com/w/cpp/algorithm/min_element with a custom comparator for translation distance.

calcmogul avatar Mar 28 '22 22:03 calcmogul

Same goes for Java with Collections.min(Collection<T>, Comparator<T>).

Starlight220 avatar Mar 29 '22 10:03 Starlight220

public Translation2d nearest(List<Translation2d> others) {
  return Collections.min(others, Comparator.comparing(this::getDistance));
}

jonahsnider avatar Apr 12 '22 23:04 jonahsnider