예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VoronoiGenerator"/> class.
        /// </summary>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        /// <param name="pkt">Pkt.</param>
        public VoronoiGenerator(int width, int height, Vector2[] pkt, int relax)
        {
            this.width  = width;
            this.height = height;


            // Create your sites (lets call that the center of your polygons)
            List <Vector2f> points = UsePoints(pkt);

            // Create the bounds of the voronoi diagram
            // Use Rectf instead of Rect; it's a struct just like Rect and does pretty much the same,
            // but like that it allows you to run the delaunay library outside of unity (which mean also in another tread)
            Rectf bounds = new Rectf(0, 0, width, height);

            // There is a two ways you can create the voronoi diagram: with or without the lloyd relaxation
            // Here I used it with 2 iterations of the lloyd relaxation
            Voronoi voronoi = new Voronoi(points, bounds, relax);

            // Now retreive the edges from it, and the new sites position if you used lloyd relaxtion
            sites = voronoi.SitesIndexedByLocation;
            edges = voronoi.Edges;

            tx            = DrawVoronoiDiagram();
            relaxedPoints = voronoi.GetRelaxedPoints();
        }