// Recursively verify that when a rectangle is split into two pieces, the // centroids of the children sum to give the centroid of the parent. static void TestCentroidSplitting(S2LatLngRect r, int splits_left) { S2LatLngRect child0, child1; if (S2Testing.Random.OneIn(2)) { double lat = S2Testing.Random.UniformDouble(r.Lat.Lo, r.Lat.Hi); child0 = new S2LatLngRect(new R1Interval(r.Lat.Lo, lat), r.Lng); child1 = new S2LatLngRect(new R1Interval(lat, r.Lat.Hi), r.Lng); } else { Assert.True(r.Lng.Lo <= r.Lng.Hi); double lng = S2Testing.Random.UniformDouble(r.Lng.Lo, r.Lng.Hi); child0 = new S2LatLngRect(r.Lat, new S1Interval(r.Lng.Lo, lng)); child1 = new S2LatLngRect(r.Lat, new S1Interval(lng, r.Lng.Hi)); } Assert.True((r.Centroid() - child0.Centroid() - child1.Centroid()).Norm() <= 1e-15); if (splits_left > 0) { TestCentroidSplitting(child0, splits_left - 1); TestCentroidSplitting(child1, splits_left - 1); } }