VecmathUtil { static Vector2d newVectorInLargestGap(final List<Vector2d> vectors) { assert vectors.size() > 1; final double[] extents = VecmathUtil.extents(vectors); Arrays.sort(extents); double max = -1; int index = -1; for (int i = 0; i < vectors.size(); i++) { double extent = extents[(i + 1) % vectors.size()] - extents[i]; if (extent < 0) extent += TAU; if (extent > max) { max = extent; index = i; } } assert index >= 0; double mid = (max / 2); double theta = extents[index] + mid; return new Vector2d(Math.cos(theta), Math.sin(theta)); } private  VecmathUtil();   }
@Test public void largestGapSouthWest() { Vector2d vector = VecmathUtil.newVectorInLargestGap(Arrays.asList(new Vector2d(0, 1), new Vector2d(1, 0))); assertThat(vector.x, closeTo(-0.707d, 0.01)); assertThat(vector.y, closeTo(-0.707d, 0.01)); assertThat(vector.length(), closeTo(1d, 0.01)); }
@Test public void largestGapEast() { Vector2d vector = VecmathUtil.newVectorInLargestGap(Arrays .asList(new Vector2d(1, 1), new Vector2d(1, -1), new Vector2d(-1, -1), new Vector2d(-1, 1), new Vector2d(-1, 0), new Vector2d(0, 1), new Vector2d(0, -1))); assertThat(vector.x, closeTo(1, 0.01)); assertThat(vector.y, closeTo(0, 0.01)); assertThat(vector.length(), closeTo(1d, 0.01)); }