code: static Vector2d getNearestVector(final Vector2d reference, final List<Vector2d> vectors) {
  
          if (vectors.isEmpty()) throw new IllegalArgumentException("No vectors provided");
  
          // to find the closest vector we find use the dot product,
          // for the general case (non-unit vectors) one can use the
          // cosine similarity
          Vector2d closest = vectors.get(0);
          double maxProd = reference.dot(closest);
  
          for (int i = 1; i < vectors.size(); i++) {
              double newProd = reference.dot(vectors.get(i));
              if (newProd > maxProd) {
                  maxProd = newProd;
                  closest = vectors.get(i);
              }
          }
  
          return closest;
      }
docs: Given a list of unit vectors, find the vector which is nearest to a
  provided reference.
  
  @param reference a target vector
  @param vectors list of vectors
  @return the nearest vector
  @throws java.lang.IllegalArgumentException no vectors provided