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 ; }
Given a list of unit vectors find the vector which is nearest to a provided reference .