//TODO: Unbounded problem could manifest itself here public static VFace closestCell(float x, float y, TriangleNet.Voronoi.BoundedVoronoi voronoi) { int startingIndex = voronoi.Faces.Count / 2; VFace currentCell = voronoi.Faces[startingIndex]; float currentDistance = Mathf.Sqrt((float)(currentCell.generator.X - x) * (float)(currentCell.generator.X - x) + (float)(currentCell.generator.Y - y) * (float)(currentCell.generator.Y - y)); while (true) { TriangleNet.Topology.DCEL.Face nextCell = null; bool foundNeighbour = false; foreach (TriangleNet.Topology.DCEL.HalfEdge edge in currentCell.EnumerateEdges()) { VFace nCell = edge.twin.face; float neighbourX = (float)nCell.generator.X; float neighbourY = (float)nCell.generator.Y; float distanceFromNeighbour = Mathf.Sqrt((neighbourX - x) * (neighbourX - x) + (neighbourY - y) * (neighbourY - y)); if (distanceFromNeighbour < currentDistance) { foundNeighbour = true; currentDistance = distanceFromNeighbour; nextCell = nCell; } } if (!foundNeighbour) { break; } currentCell = nextCell; } return(currentCell); }
private VFace ClosestCell(float x, float y, BoundedVoronoi voronoi) { int startingIndex = voronoi.Faces.Count / 2; VFace currentCell = voronoi.Faces[startingIndex]; float xDiff = (float)(currentCell.Generator.X - x); float yDiff = (float)(currentCell.Generator.Y - y); float currentDist = Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff); while (true) { VFace nextCell = null; bool foundNeighbour = false; foreach (VHEdge edge in currentCell.EnumerateEdges()) { VFace nCell = edge.Twin.Face; float xDiffNeighbor = (float)(nCell.Generator.X - x); float yDiffNeighbor = (float)(nCell.Generator.Y - y); float distFromNeighbour = Mathf.Sqrt(xDiffNeighbor * xDiffNeighbor + yDiffNeighbor * yDiffNeighbor); if (distFromNeighbour < currentDist) { foundNeighbour = true; currentDist = distFromNeighbour; nextCell = nCell; } } if (!foundNeighbour) { break; } currentCell = nextCell; } return(currentCell); }