private void DrawWalls(TriGrid grid) { float halfSideSize = sideSize / 2; int halfSideSizeInt = (int)halfSideSize; float height = TriangleHeight; float halfHeight = height / 2.0f; for (int i = 0; i < grid.height; ++i) { int centerY = tex.height - ((int)(halfHeight + i * height)); int north = (int)(centerY + halfHeight); int south = north - (int)height; // upright for (int j = (i % 2); j < grid.width; j += 2) { int centerX = halfSideSizeInt + j * halfSideSizeInt; int west = Mathf.Max(0, centerX - halfSideSizeInt); int east = centerX + halfSideSizeInt; int currentVertex = grid.PositionToVertex(i, j); if (!grid.Graph.AreLinked(currentVertex, grid.EastOf(currentVertex))) { tex.Line(new Vector2Int(east, south), new Vector2Int(centerX, north), wallColor); } if (!grid.Graph.AreLinked(currentVertex, grid.SouthOf(currentVertex))) { tex.HorizontalLine(west, east, south, wallColor); } } // !upright for (int j = ((i + 1) % 2); j < grid.width; j += 2) { int centerX = (int)(halfSideSize + j * halfSideSize); int west = centerX - halfSideSizeInt; int east = centerX + halfSideSizeInt; int currentVertex = grid.PositionToVertex(i, j); if (!grid.Graph.AreLinked(currentVertex, grid.EastOf(currentVertex))) { tex.Line(new Vector2Int(east, north), new Vector2Int(centerX, south), wallColor); } if (!grid.Graph.AreLinked(currentVertex, grid.NorthOf(currentVertex))) { tex.HorizontalLine(west, east, north, wallColor); } } } }
private void PaintDistances(TriGrid triGrid, Color[] vertexDistanceColors) { float halfSideSize = sideSize / 2; int halfSideSizeInt = (int)halfSideSize; float height = TriangleHeight; float halfHeight = height / 2.0f; Vector2 texPoint = new Vector2(); for (int y = 0; y != tex.height; ++y) { int row = (int)(((float)(tex.height - y)) / height); // y texture to grid row texPoint.y = y + 0.5f; int centerY = tex.height - ((int)(halfHeight + row * height)); float north = centerY + halfHeight + 0.5f; float south = north - height - 0.5f; for (int x = 0; x != tex.width; ++x) { Color color = backgroundColor; texPoint.x = x + 0.5f; int halfSizeCount = (int)(x / halfSideSize); int fromCol = Mathf.Max(0, halfSizeCount - 1); int maxCol = Mathf.Min(halfSizeCount + 2, triGrid.width); for (int col = fromCol; col != maxCol; ++col) { int vertex = triGrid.PositionToVertex(row, col); int centerX = halfSideSizeInt + col * halfSideSizeInt; int west = Mathf.Max(0, centerX - halfSideSizeInt); int east = centerX + halfSideSizeInt; Vector2 a = new Vector2(); Vector2 b = new Vector2(); Vector2 c = new Vector2(); if (triGrid.IsUpright(vertex)) { a.Set(centerX + 0.5f, north); b.Set(east + 0.5f, south); c.Set(west + 0.5f, south); } else { a.Set(centerX + 0.5f, south); b.Set(west + 0.5f, north); c.Set(east + 0.5f, north); } if (PointTest2D.IsInsideTri(texPoint, a, b, c)) { color = vertexDistanceColors[vertex]; break; } } tex.SetPixel(x, y, color); } } }