Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Otter;
  8.  
  9. namespace Voronoi
  10. {
  11.     class VoronoiScene : Scene
  12.     {
  13.         private int width;
  14.         private int height;
  15.         private const int NumPoints = 50;
  16.         private List<Entity> points;
  17.         private List<Entity> cells;
  18.  
  19.         public VoronoiScene() : base()
  20.         {
  21.             Surface voronoiSurface = new Surface(width, height, Color.Black);
  22.  
  23.             var points = GeneratePoints(NumPoints);
  24.             var graph = GeneratePlanarGraph(points);
  25.             Draw.SetTarget(voronoiSurface);
  26.             foreach (var edge in graph.GetEdges())
  27.             {
  28.                 Draw.Line(edge.node1.point.X, edge.node1.point.Y, edge.node2.point.X, edge.node2.point.Y, Color.Cyan, 2);
  29.             }
  30.  
  31.             Draw.Line(0, width, (float) height/2, (float) height/2, Color.Cyan, 30);
  32.  
  33.             voronoiSurface.Render();
  34.  
  35.             AddMultiple(points.ToArray());
  36.             AddSurface(voronoiSurface);
  37.             /*AddMultiple(GenerateCells().ToArray());*/
  38.         }
  39.  
  40.         private List<Entity> GeneratePoints(int numSites)
  41.         {
  42.             Random r = new Random();
  43.             List<Entity> points = new List<Entity>();
  44.  
  45.             int i = 0;
  46.             while(i < numSites)
  47.             {
  48.                 float randX = (float) r.NextDouble() * DataSingleton.Instance.Width;
  49.                 float randY = (float) r.NextDouble() * DataSingleton.Instance.Height;
  50.                 VoronoiPoint point = new VoronoiPoint(randX, randY);
  51.                 if (!point.IsValid(points))
  52.                 {
  53.                     continue;
  54.                 }
  55.                 Console.WriteLine("Putting point at x:{0} y:{1}", randX, randY);
  56.                 points.Add(new VoronoiPoint(randX, randY));
  57.                 i++;
  58.             }
  59.  
  60.             this.points = points;
  61.             return points;
  62.         }
  63.  
  64.         private VoronoiGraph GeneratePlanarGraph(List<Entity> points)
  65.         {
  66.             var graph = new VoronoiGraph();
  67.  
  68.             foreach (var point in points)
  69.             {
  70.                 var tempPoint = new VoronoiPoint(point.X, point.Y);
  71.                 graph.AddVertex(tempPoint);
  72.                 if (graph.GetVertices().Count > 1)
  73.                 {
  74.                     /* "Shorthand"...?
  75.                      * foreach (VoronoiGraph.Edge tempEdge
  76.                         in from vertex in graph.GetVertices()
  77.                             select new VoronoiGraph.Edge(tempPoint, vertex.point)
  78.                             into tempEdge
  79.                             where graph.GetEdges().Count > 1
  80.                                 let validEdge = graph.GetEdges().All(edge => !edge.Intersects(tempEdge))
  81.                                     where validEdge
  82.                                         select tempEdge)
  83.                     {
  84.                         graph.AddEdge(tempEdge);
  85.                     }
  86.                      */
  87.                     foreach (var vertex in graph.GetVertices())
  88.                     {
  89.                         VoronoiGraph.Edge tempEdge = new VoronoiGraph.Edge(tempPoint, vertex.point);
  90.                         if (graph.GetVertices().Count <= 1) continue;
  91.                         bool validEdge = graph.GetEdges().All(edge => !edge.Intersects(tempEdge));
  92.  
  93.                         if (validEdge)
  94.                         {
  95.                             graph.AddEdge(tempEdge);
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.  
  101.             return graph;
  102.         }
  103.  
  104.         /*private List<Entity> GenerateCells()
  105.         {
  106.             //make a new cell for each point generated
  107.             foreach (var point in points)
  108.             {
  109.                 cells.Add(new VoronoiCell((VoronoiPoint)point));
  110.             }
  111.             //start with the first cell, find the nearest cell to it
  112.             foreach (var cell in cells)
  113.             {
  114.                 var otherCells = cells.Where(c => c != cell).ToList();
  115.                 Entity closestCell = null;
  116.                 foreach (var otherCell in otherCells)
  117.                 {
  118.                     if (closestCell == null
  119.                         ||
  120.                         Util.GetDistance(otherCell.X, otherCell.Y, cell.X, cell.Y) <
  121.                         Util.GetDistance(closestCell.X, closestCell.Y, cell.X, cell.Y))
  122.                     {
  123.                         closestCell = otherCell;
  124.                     }
  125.                 }
  126.  
  127.             }
  128.         }*/
  129.  
  130.         public override void Begin()
  131.         {
  132.             Console.WriteLine("And we're in!");
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement