Exemple #1
0
        void drawPoint(Brush brush, VoronoiStruct.Point pos)
        {
            // this func is just a short hand for writting FillEllipse
            Point pos1 = new Point(pos.x - circleSize.Width / 2, pos.y - circleSize.Height / 2);

            g.FillEllipse(brush, new RectangleF(pos1, circleSize));
        }
Exemple #2
0
        void performLloyd()
        {
            VoronoiStruct.Voronoi newMap = new VoronoiStruct.Voronoi(vmap.width, vmap.height);
            Rectangle             regin  = new Rectangle(0, 0, vmap.width, vmap.height);

            foreach (var poly in vmap.polygons)
            {
                List <VoronoiStruct.Point> points = new List <VoronoiStruct.Point>();
                foreach (var edge in poly.edges)
                {
                    VoronoiStruct.Point a = edge.line.a;
                    VoronoiStruct.Point b = edge.line.b;
                    if (!points.Contains(a))
                    {
                        points.Add(a);
                    }
                    if (!points.Contains(b))
                    {
                        points.Add(b);
                    }
                }
                int cx = points.Sum((point) => { return(point.x); }) / points.Count;
                int cy = points.Sum((point) => { return(point.y); }) / points.Count;
                newMap.polygons.Add(new VoronoiStruct.Polygon(new VoronoiStruct.Point(cx, cy)));
            }
            vmap      = newMap;
            sweepLine = null;
            drawVoronoi(vmap);
            ++mapIterations;
            label_iterations.Text = string.Format("{0}-th iterations", mapIterations);
        }
Exemple #3
0
 void drawLine(Pen pen, VoronoiStruct.Point pos1, VoronoiStruct.Point pos2)
 {
     if (pos1.x < 0 && pos2.x < 0 || pos1.y < 0 && pos2.y < 0)
     {
         return;
     }
     g.DrawLine(pen, pos1.x, pos1.y, pos2.x, pos2.y);
 }
        // get parabola's x position on y = y
        private double parabolaX(VoronoiStruct.Point focus, double y)
        {
            double k = focus.y;
            double h = (L + focus.x) / 2.0;
            double c = -(L - focus.x) / 2.0;

            if (c >= 0)
            {
                return(double.MinValue);
            }
            else
            {
                double x = Math.Pow(y - k, 2) / (4 * c) + h;
                return(x);
            }
        }
Exemple #5
0
 public Line(VoronoiStruct.Point a, VoronoiStruct.Point b)
 {
     this.a = a;
     this.b = b;
 }
Exemple #6
0
 public Polygon(List <Edge> edges, VoronoiStruct.Point focus)
 {
     this.edges = new List <Edge>();
     this.focus = focus;
 }
Exemple #7
0
 void drawLine(Pen pen, VoronoiStruct.Point pos1, VoronoiStruct.Point pos2)
 {
     g.DrawLine(pen, pos1.x, pos1.y, pos2.x, pos2.y);
 }
Exemple #8
0
        int river_generator(VoronoiStruct.Point pos) //產生河流
        {
            //float [,]temp;
            List <VoronoiStruct.Point> connect = new List <VoronoiStruct.Point>();
            List <VoronoiStruct.Line>  line    = new List <VoronoiStruct.Line>();
            float temp = 1000;

            foreach (var item1 in vmap.polygons)
            {
                foreach (var item2 in item1.edges)
                {
                    if (pos.x == item2.line.a.x && pos.y == item2.line.a.y)
                    {
                        connect.Add(item2.line.b);
                        line.Add(item2.line);
                        //allRiver.Add(item2.line.b);
                        if (item2.line.b.elevation <= temp)
                        {
                            temp = item2.line.b.elevation;
                        }
                    }
                    if (pos.x == item2.line.b.x && pos.y == item2.line.b.y)
                    {
                        connect.Add(item2.line.a);
                        line.Add(item2.line);
                        //allRiver.Add(item2.line.a);
                        if (item2.line.a.elevation <= temp)
                        {
                            temp = item2.line.a.elevation;
                        }
                    }
                }
                //connect.Clear();
                //line.Clear();
            }

            if (pos.elevation <= temp)
            {
                return(-1);
            }
            int i = 0;

            for (; i < connect.Count; ++i)
            {
                if (connect[i].elevation == temp)
                {
                    break;
                }
            }
            foreach (var item in line)
            {
                if (item.a.elevation == temp)
                {
                    foreach (var item1 in vmap.polygons)
                    {
                        foreach (var item2 in item1.edges)
                        {
                            if (item.a.x == item2.line.a.x && item.a.y == item2.line.a.y &&
                                item.b.x == item2.line.b.x && item.b.y == item2.line.b.y)
                            {
                                item2.line.isRiver = true;
                                allRiver.Add(item.a);
                                return(river_generator(connect[i]));
                            }
                        }
                    }
                }
                else if (item.b.elevation == temp)
                {
                    foreach (var item1 in vmap.polygons)
                    {
                        foreach (var item2 in item1.edges)
                        {
                            if (item.a.x == item2.line.a.x && item.a.y == item2.line.a.y &&
                                item.b.x == item2.line.b.x && item.b.y == item2.line.b.y)
                            {
                                item2.line.isRiver = true;
                                allRiver.Add(item.b);
                                return(river_generator(connect[i]));
                            }
                        }
                    }
                }
            }
            //return river_generator(connect[i]);
            return(0);
        }
 public static double distance(VoronoiStruct.Point a, PointF b)
 {
     return(Math.Sqrt(Math.Pow(a.x - b.X, 2) + Math.Pow(a.y - b.Y, 2)));
 }
        public void optimize(Rectangle regin)
        {
            // fixing edge lines
            List <VoronoiStruct.Point> borderEdges = new List <VoronoiStruct.Point>();

            for (int i = 0; i < edges.Count; i++)
            {
                var edge = edges[i];
                if (!regin.Contains(edge.line.a.x, edge.line.a.y) && !regin.Contains(edge.line.b.x, edge.line.b.y))
                {
                    // edge is out of regin
                    edges.Remove(edge);
                    --i;
                    continue;
                }
                if (regin.Contains(edge.line.a.x, edge.line.a.y) && regin.Contains(edge.line.b.x, edge.line.b.y))
                {
                    continue;
                }
                double mx = edge.line.b.x - edge.line.a.x;
                double my = edge.line.b.y - edge.line.a.y;
                double y0 = 0, ym = 0;
                if (mx != 0)
                {
                    y0 = edge.line.a.y + (0 - edge.line.a.x) * (my / mx);
                    ym = edge.line.a.y + (regin.Right - 1 - edge.line.a.x) * (my / mx);
                }
                if (edge.line.a.x < 0)
                {
                    edge.line.a = new Point(0, (int)y0);
                }
                else if (edge.line.b.x < 0)
                {
                    edge.line.b = new Point(0, (int)y0);
                }
                else if (edge.line.a.x >= regin.Right)
                {
                    edge.line.a = new Point(regin.Right - 1, (int)ym);
                }
                else if (edge.line.b.x >= regin.Right)
                {
                    edge.line.b = new Point(regin.Right - 1, (int)ym);
                }
                double x0 = 0, xm = 0;
                if (my != 0)
                {
                    x0 = edge.line.a.x + (0 - edge.line.a.y) * (mx / my);
                    xm = edge.line.a.x + (regin.Bottom - 1 - edge.line.a.y) * (mx / my);
                }
                if (edge.line.a.y < 0)
                {
                    edge.line.a = new Point((int)x0, 0);
                }
                else if (edge.line.b.y < 0)
                {
                    edge.line.b = new Point((int)x0, 0);
                }
                else if (edge.line.a.y >= regin.Bottom)
                {
                    edge.line.a = new Point((int)xm, regin.Bottom - 1);
                }
                else if (edge.line.b.y >= regin.Bottom)
                {
                    edge.line.b = new Point((int)xm, regin.Bottom - 1);
                }
                if (edge.line.a.x == 0 || edge.line.a.x == regin.Bottom - 1)
                {
                    borderEdges.Add(edge.line.a);
                }
                else if (edge.line.b.x == 0 || edge.line.b.x == regin.Bottom - 1)
                {
                    borderEdges.Add(edge.line.b);
                }
                if (edge.line.a.y == 0 || edge.line.a.y == regin.Bottom - 1)
                {
                    borderEdges.Add(edge.line.a);
                }
                else if (edge.line.b.y == 0 || edge.line.b.y == regin.Bottom - 1)
                {
                    borderEdges.Add(edge.line.b);
                }
            }
            if (borderEdges.Count == 2)
            {
                if (borderEdges[0].x == borderEdges[1].x)
                {
                    var bar = new Edge();
                    bar.deAbstract();
                    bar.line.a = borderEdges[0];
                    bar.line.b = borderEdges[1];
                    this.edges.Add(bar);
                }
                else if (borderEdges[0].y == borderEdges[1].y)
                {
                    var bar = new Edge();
                    bar.deAbstract();
                    bar.line.a = borderEdges[0];
                    bar.line.b = borderEdges[1];
                    this.edges.Add(bar);
                }
                else
                {
                    var endPoint = new VoronoiStruct.Point();
                    if (borderEdges[0].x == 0 || borderEdges[0].x == regin.Bottom - 1)
                    {
                        endPoint.x = borderEdges[0].x;
                    }
                    else
                    {
                        endPoint.x = borderEdges[1].x;
                    }
                    if (borderEdges[0].y == 0 || borderEdges[0].y == regin.Bottom - 1)
                    {
                        endPoint.y = borderEdges[0].y;
                    }
                    else
                    {
                        endPoint.y = borderEdges[1].y;
                    }
                    var bar1 = new Edge();
                    var bar2 = new Edge();
                    bar1.deAbstract();
                    bar2.deAbstract();
                    bar1.line.a = borderEdges[0];
                    bar1.line.b = endPoint;
                    bar2.line.a = borderEdges[1];
                    bar2.line.b = endPoint;
                    this.edges.Add(bar1);
                    this.edges.Add(bar2);
                }
            }
            sortEdges();
        }
Exemple #11
0
 public Line(VoronoiStruct.Point a, VoronoiStruct.Point b)
 {
     this.a  = a;
     this.b  = b;
     isRiver = false;
 }