Guest User

Voronoi Noise "highest weight out of n closest points"

a guest
Nov 7th, 2017
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7.  
  8. namespace NoiseGeneration {
  9.     class Program {
  10.         static Random rnd;
  11.         static void Main (string[] args) {
  12.  
  13.             int size = 512;
  14.             int pointCount = 50;
  15.  
  16.             rnd = new Random(0);
  17.  
  18.  
  19.             // add points
  20.             List<Point> pointList = new List<Point>();
  21.             while (pointList.Count < pointCount) {
  22.                 Console.Write("add point: ");
  23.                 int x = rnd.Next(size);
  24.                 int y = rnd.Next(size);
  25.                 if (pointList.Where(p => p.x == x && p.y == y).Count() == 0) {
  26.                     pointList.Add(new Point(x, y));
  27.                     Console.WriteLine("success");
  28.                 } else {
  29.                     Console.WriteLine("failed");
  30.                 }
  31.             }
  32.  
  33.             // normal voronoi
  34.             Bitmap bitmap = new Bitmap(size, size);
  35.             Console.Write("start processing: ");
  36.             for (int x = 0; x < size; ++x) {
  37.                 for (int y = 0; y < size; ++y) {
  38.                     Point current = new Point(x, y);
  39.                     Point closest = new Point(-1, -1);
  40.                     double dist = double.MaxValue;
  41.                     foreach (Point p in pointList) {
  42.                         if (Point.Distance(p, current) < dist) {
  43.                             dist = Point.Distance(p, current);
  44.                             closest = p;
  45.                         }
  46.                     }
  47.                     bitmap.SetPixel(x, y, closest.c);
  48.                 }
  49.             }
  50.             Console.WriteLine("done");
  51.  
  52.             bitmap.Save("voronoi.bmp");
  53.  
  54.  
  55.             // weighted voronoi
  56.             Bitmap bitmap_w = new Bitmap(size, size);
  57.             Console.Write("start processing: ");
  58.             for (int x = 0; x < size; ++x) {
  59.                 for (int y = 0; y < size; ++y) {
  60.                     Point current = new Point(x, y);
  61.  
  62.                     // sort list according to distance
  63.                     pointList.Sort(delegate(Point a, Point b) {
  64.                         return Math.Sign(Point.Distance(current, a) - Point.Distance(current, b));
  65.                     });
  66.  
  67.                     List<Point> wList = pointList.GetRange(0, 3);
  68.                     wList.Sort(delegate(Point a, Point b) {
  69.                         return Math.Sign(b.w - a.w);
  70.                     });
  71.  
  72.                     bitmap_w.SetPixel(x, y, wList[0].c);
  73.                 }
  74.             }
  75.             Console.WriteLine("done");
  76.  
  77.             bitmap_w.Save("voronoi_w.bmp");
  78.         }
  79.     }
  80.  
  81.     public struct Point {
  82.         static Random rnd;
  83.         public int x, y, w;
  84.         public Color c;
  85.         public Point (int _x, int _y) {
  86.             x = _x;
  87.             y = _y;
  88.  
  89.             if (rnd == null) {
  90.                 rnd = new Random(0);
  91.             }
  92.             w = rnd.Next(256);
  93.             c = Color.FromArgb(rnd.Next(180, 320) % 256, rnd.Next(180, 320) % 256, rnd.Next(180, 320) % 256);
  94.             //c = Color.FromArgb(w, w, w);
  95.         }
  96.         public static double Distance (Point a, Point b) {
  97.             return Math.Sqrt(Math.Pow(a.x - b.x, 2) + Math.Pow(a.y - b.y, 2));
  98.         }
  99.     }
  100. }
Add Comment
Please, Sign In to add comment