/// <summary> /// Get the biome to use for the given chunk /// </summary> /// <param name="chunkID"></param> /// <returns></returns> public Biome getBiomeForChunk(Coordinate chunkID) { /// get the closest centers by distance that may be intersecting this chunk List <Vertex> closestBiomeCenters = biomeVoronoiCenters.Keys .OrderBy(vertex => ((Coordinate)vertex).distance(chunkID.xz)) .Take(MaxBiomesPerSlicedChunk).ToList(); // get the bounds of this chunk from above as a rectangle Coordinate chunkMin = chunkID * Chunk.Diameter; Coordinate chunkMax = chunkMin + Chunk.Diameter; var chunkBounds = (chunkMax, chunkMin); /// find all intersecting cells by their edges List <Vertex> intersectingVoronoiCellCenters = new List <Vertex>(); foreach (Vertex vornoiCenter in closestBiomeCenters) { vornoiCenter.centerPointOf.forEachEdgeUntil((edge) => { if (Delaunay.LineIntersectsRectangle(chunkBounds, (edge.start, edge.end))) { intersectingVoronoiCellCenters.Add(vornoiCenter); return(false); } return(true); }); } // if there are no intersecting cells, we're in the cell if (intersectingVoronoiCellCenters.Count == 0) { return(biomeVoronoiCenters[closestBiomeCenters[0]]); } else if (intersectingVoronoiCellCenters.Count == 1) { World.Debug.logAndThrowError <System.MissingMemberException>( $"Only one cell found to intersect this chunk, that shouldn't happen as all half edges are two sided" ); return(default);