BoundingSphere { public BoundingSphere merge(BoundingSphere other) { Vector3 centerDiff = other.center.subtract(center); double centerDistance = centerDiff.length(); double newRadius = (centerDistance + radius + other.radius) * 0.5; if (centerDistance >= radiusEpsilon) { double f = (centerDistance + other.radius - radius) / (2*centerDistance); if (0 <= f && f <= 1) { Vector3 newCenter = center.add(centerDiff.multiply(f)); return new BoundingSphere(newRadius, newCenter); } } if (radius > other.radius) { return this; } else { return other; } } BoundingSphere(double radius, Vector3 center); double getRadius(); Vector3 getCenter(); static BoundingSphere create(MeshData data); BoundingSphere merge(BoundingSphere other); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testMergeBoundingSpheresIntersecting() throws Exception { BoundingSphere a = new BoundingSphere(2.0, new Vector3(2.0, 0, 0)); BoundingSphere b = new BoundingSphere(3.0, new Vector3(4.0, 0, 0)); BoundingSphere merged = a.merge(b); assertEquals(new BoundingSphere(3.5, new Vector3(3.5, 0, 0)), merged); }