VecmathUtil { static Vector2d getNearestVector(final Vector2d reference, final List<Vector2d> vectors) { if (vectors.isEmpty()) throw new IllegalArgumentException("No vectors provided"); 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; } private  VecmathUtil();   }
@Test public void testGetNearestVector1() throws Exception { Vector2d nearest = VecmathUtil.getNearestVector(new Vector2d(0, 1), Arrays.asList(new Vector2d(0.5, 0.5), new Vector2d(0.5, -0.5))); assertThat(nearest.x, closeTo(0.5d, 0.01)); assertThat(nearest.y, closeTo(0.5d, 0.01)); }
@Test public void testGetNearestVector2() throws Exception { Vector2d nearest = VecmathUtil.getNearestVector(new Vector2d(0, -1), Arrays.asList(new Vector2d(0.5, 0.5), new Vector2d(0.5, -0.5))); assertThat(nearest.x, closeTo(0.5d, 0.01)); assertThat(nearest.y, closeTo(-0.5d, 0.01)); }
@Test(expected = IllegalArgumentException.class) public void testGetNearestVectorComplainsWhenNoVectorsProvided() throws Exception { VecmathUtil.getNearestVector(new Vector2d(1, 0), Collections.<Vector2d> emptyList()); }
@Test public void testGetNearestVectorFromBonds() throws Exception { IAtom a1 = mock(IAtom.class); IAtom a2 = mock(IAtom.class); IAtom a3 = mock(IAtom.class); IAtom a4 = mock(IAtom.class); IBond b1 = mock(IBond.class); IBond b2 = mock(IBond.class); IBond b3 = mock(IBond.class); when(b1.getOther(a1)).thenReturn(a2); when(b2.getOther(a1)).thenReturn(a3); when(b3.getOther(a1)).thenReturn(a4); when(a1.getPoint2d()).thenReturn(new Point2d(0, 0)); when(a2.getPoint2d()).thenReturn(new Point2d(0, 1)); when(a3.getPoint2d()).thenReturn(new Point2d(1, 0)); when(a4.getPoint2d()).thenReturn(new Point2d(1, 1)); Vector2d nearest = VecmathUtil.getNearestVector(new Vector2d(0.5, 0.5), a1, Arrays.asList(b1, b2, b3)); assertThat(nearest.x, closeTo(0.707d, 0.01)); assertThat(nearest.y, closeTo(0.707d, 0.01)); }