public static Color GetTheClosestPixel(Color c, Color[] colorsArray) { int distance = int.MaxValue; Color? closestColor = null; for (int i = 0; i < colorsArray.Count(); i++) { var dist = DistanceBetweenPixels(c, colorsArray[i]); if (dist == 0) { closestColor = colorsArray[i]; return (Color) closestColor; } if (distance > dist) { distance = dist; closestColor = colorsArray[i]; } } return closestColor ?? c; }
// Paints the map to the graphics public void Paint(System.Drawing.Graphics g, bool drawBiomes, bool drawRivers, bool drawSites, bool drawCorners, bool drawDelaunay, bool drawVoronoi, bool noisyEdges, bool smoothBlending, bool lighting) { int numSites = centers.Count; // If we want to just see the dalaunay, set the color of the graph to grey System.Drawing.Color[] defaultColors = null; if (!drawBiomes && drawDelaunay) { defaultColors = new System.Drawing.Color[numSites]; for (int i = 0; i < defaultColors.Count(); i++) { defaultColors[i] = System.Drawing.Color.FromArgb(192, 192, 192); } } // Otherwise show colorful voronoi polygons! else if (!drawBiomes) { defaultColors = new System.Drawing.Color[numSites]; for (int i = 0; i < defaultColors.Count(); i++) { defaultColors[i] = System.Drawing.Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)); } } // Draw the voronoi polygons foreach (Center c in centers) { DrawPolygon(g, c, drawBiomes ? GetColor(c.biome) : defaultColors[c.index], noisyEdges, smoothBlending, lighting, drawBiomes); } // Draw the delaunay edges if wanted, draw rivers if wanted foreach (DEdge e in edges) { if (drawDelaunay) { g.DrawLine(System.Drawing.Pens.Yellow, (int)e.d0.loc.x, (int)e.d0.loc.y, (int)e.d1.loc.x, (int)e.d1.loc.y); } if (drawRivers && e.river > 0) { g.DrawLine(new System.Drawing.Pen(RIVER, (1 + (int)Math.Sqrt(e.river * 2))), (int)e.v0.loc.x, (int)e.v0.loc.y, (int)e.v1.loc.x, (int)e.v1.loc.y); } } // Draw the centers of each voronoi polygon if (drawSites) { foreach (Center c in centers) { g.FillEllipse(System.Drawing.Brushes.Black, (int)(c.loc.x - 2), (int)(c.loc.y - 2), 4, 4); } } // Draw the corners of eahc voronoi polygon if (drawCorners) { foreach (Corner c in corners) { g.FillEllipse(System.Drawing.Brushes.Black, (int)(c.loc.x - 2), (int)(c.loc.y - 2), 4, 4); } } // Draw the bounds g.DrawRectangle(System.Drawing.Pens.White, (int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height); }