Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- namespace NoiseGeneration {
- class Program {
- static Random rnd;
- static void Main (string[] args) {
- int size = 512;
- int pointCount = 50;
- rnd = new Random(0);
- // add points
- List<Point> pointList = new List<Point>();
- while (pointList.Count < pointCount) {
- Console.Write("add point: ");
- int x = rnd.Next(size);
- int y = rnd.Next(size);
- if (pointList.Where(p => p.x == x && p.y == y).Count() == 0) {
- pointList.Add(new Point(x, y));
- Console.WriteLine("success");
- } else {
- Console.WriteLine("failed");
- }
- }
- // normal voronoi
- Bitmap bitmap = new Bitmap(size, size);
- Console.Write("start processing: ");
- for (int x = 0; x < size; ++x) {
- for (int y = 0; y < size; ++y) {
- Point current = new Point(x, y);
- Point closest = new Point(-1, -1);
- double dist = double.MaxValue;
- foreach (Point p in pointList) {
- if (Point.Distance(p, current) < dist) {
- dist = Point.Distance(p, current);
- closest = p;
- }
- }
- bitmap.SetPixel(x, y, closest.c);
- }
- }
- Console.WriteLine("done");
- bitmap.Save("voronoi.bmp");
- // weighted voronoi
- Bitmap bitmap_w = new Bitmap(size, size);
- Console.Write("start processing: ");
- for (int x = 0; x < size; ++x) {
- for (int y = 0; y < size; ++y) {
- Point current = new Point(x, y);
- // sort list according to distance
- pointList.Sort(delegate(Point a, Point b) {
- return Math.Sign(Point.Distance(current, a) - Point.Distance(current, b));
- });
- List<Point> wList = pointList.GetRange(0, 3);
- wList.Sort(delegate(Point a, Point b) {
- return Math.Sign(b.w - a.w);
- });
- bitmap_w.SetPixel(x, y, wList[0].c);
- }
- }
- Console.WriteLine("done");
- bitmap_w.Save("voronoi_w.bmp");
- }
- }
- public struct Point {
- static Random rnd;
- public int x, y, w;
- public Color c;
- public Point (int _x, int _y) {
- x = _x;
- y = _y;
- if (rnd == null) {
- rnd = new Random(0);
- }
- w = rnd.Next(256);
- c = Color.FromArgb(rnd.Next(180, 320) % 256, rnd.Next(180, 320) % 256, rnd.Next(180, 320) % 256);
- //c = Color.FromArgb(w, w, w);
- }
- public static double Distance (Point a, Point b) {
- return Math.Sqrt(Math.Pow(a.x - b.x, 2) + Math.Pow(a.y - b.y, 2));
- }
- }
- }
Add Comment
Please, Sign In to add comment