Ejemplo n.º 1
0
        public static void SaveGraph(WOGraph graph, string filePath)
        {
            using (System.IO.StreamWriter output = new System.IO.StreamWriter(filePath))
            {
                output.WriteLine(graph.VerticesNumber.ToString());

                for (int i = 0; i < graph.VerticesNumber; i++)
                {
                    WOGraphVertice vertice = graph.GetVerticeAt(i);
                    output.WriteLine("{0} {1} {2}", vertice.Label, vertice.Coordinates.X, vertice.Coordinates.Y);
                }

                for (int i = 0; i < graph.VerticesNumber; i++)
                {
                    string buffer = "";
                    for (int j = 0; j < graph.VerticesNumber; j++)
                    {
                        buffer += graph.GetEdgeWeightAt(i, j).ToString();
                        if (j != graph.VerticesNumber - 1)
                        {
                            buffer += " ";
                        }
                    }
                    output.WriteLine(buffer);
                }

                output.Close();
            }
        }
Ejemplo n.º 2
0
        public void FillRandomDataInRegion(int maxX, int maxY, int x0, int y0, int r1, int r2)
        {
            for (int i = 0; i < verticesNumber; i++)
            {
                int x = random.Next(maxX);
                int y = random.Next(maxY);

                while (!(y <= Math.Sqrt(r2 * r2 - (x - x0) * (x - x0)) + y0 &&
                         y >= -Math.Sqrt(r2 * r2 - (x - x0) * (x - x0)) + y0 &&
                         (y >= Math.Sqrt(r1 * r1 - (x - x0) * (x - x0)) + y0 ||
                          y <= -Math.Sqrt(r1 * r1 - (x - x0) * (x - x0)) + y0)))
                {
                    x = random.Next(maxX);
                    y = random.Next(maxY);
                }
                vertices[i] = new WOGraphVertice(i.ToString(), x, y);
            }

            for (int i = 0; i < verticesNumber; i++)
            {
                for (int j = 0; j < verticesNumber; j++)
                {
                    edges[i, j] = Math.Sqrt(Math.Pow(vertices[i].Coordinates.X - vertices[j].Coordinates.X, 2) +
                                            Math.Pow(vertices[i].Coordinates.Y - vertices[j].Coordinates.Y, 2));
                }
            }
        }
Ejemplo n.º 3
0
        public void AddVertice(WOGraphVertice vertice)
        {
            List <WOGraphVertice> list = vertices.ToList();

            list.Add(vertice);
            vertices = list.ToArray();

            verticesNumber++;

            _RecomputeEdges();
        }
Ejemplo n.º 4
0
        public void AddVertice(int x, int y, string lable = "label")
        {
            WOGraphVertice        vertice = new WOGraphVertice(lable, x, y);
            List <WOGraphVertice> list    = vertices.ToList();

            list.Add(vertice);
            vertices = list.ToArray();

            verticesNumber++;

            _RecomputeEdges();
        }
Ejemplo n.º 5
0
        public void FillRandomData(int maxX, int maxY)
        {
            for (int i = 0; i < verticesNumber; i++)
            {
                vertices[i] = new WOGraphVertice(i.ToString(), random.Next(maxX), random.Next(maxY));
            }

            for (int i = 0; i < verticesNumber; i++)
            {
                for (int j = 0; j < verticesNumber; j++)
                {
                    edges[i, j] = Math.Sqrt(Math.Pow(vertices[i].Coordinates.X - vertices[j].Coordinates.X, 2) +
                                            Math.Pow(vertices[i].Coordinates.Y - vertices[j].Coordinates.Y, 2));
                }
            }
        }
Ejemplo n.º 6
0
        public void FillRandomDataOnBezier(int maxX, int maxY, Point[] nodes, int resolution = 1000)
        {
            int x = Painter.BezierPointAt(nodes, 0).X;
            int y = Painter.BezierPointAt(nodes, 0).Y;

            vertices[0] = new WOGraphVertice("0", x, y);

            for (int i = 1; i < verticesNumber - 1; i++)
            {
                double t    = 0;
                double oldT = 0;
                while (t == 0 && oldT == t)
                {
                    while (oldT == t)
                    {
                        t = (double)random.Next((int)Math.Ceiling((double)(i - 1) / (verticesNumber - 2) * resolution),
                                                (int)Math.Floor((double)(i) / (verticesNumber - 2) * resolution)) / resolution;
                    }
                    oldT = t;
                }
                //MessageBox.Show(t.ToString());
                x           = Painter.BezierPointAt(nodes, t).X;
                y           = Painter.BezierPointAt(nodes, t).Y;
                vertices[i] = new WOGraphVertice((i + 1).ToString(), x, y);
            }
            x = Painter.BezierPointAt(nodes, 1).X;
            y = Painter.BezierPointAt(nodes, 1).Y;
            vertices[verticesNumber - 1] = new WOGraphVertice(verticesNumber.ToString(), x, y);

            for (int i = 0; i < verticesNumber; i++)
            {
                for (int j = 0; j < verticesNumber; j++)
                {
                    edges[i, j] = Math.Sqrt(Math.Pow(vertices[i].Coordinates.X - vertices[j].Coordinates.X, 2) +
                                            Math.Pow(vertices[i].Coordinates.Y - vertices[j].Coordinates.Y, 2));
                }
            }

            edges[0, verticesNumber - 1] = edges[verticesNumber - 1, 0] = 0;
        }
Ejemplo n.º 7
0
        public static WOGraph LoadGraph(string filePath)
        {
            WOGraph graph;

            using (System.IO.StreamReader input = new System.IO.StreamReader(filePath))
            {
                int verticesNumber = Convert.ToInt32(input.ReadLine());

                graph = new WOGraph(0);

                for (int i = 0; i < verticesNumber; i++)
                {
                    string   buffer      = input.ReadLine();
                    string[] bufferArray = new string[3];
                    bufferArray = buffer.Split(' ');
                    WOGraphVertice vertice = new WOGraphVertice(bufferArray[0],
                                                                Convert.ToInt32(bufferArray[1]),
                                                                Convert.ToInt32(bufferArray[2]));
                    graph.AddVertice(vertice);
                }

                for (int i = 0; i < verticesNumber; i++)
                {
                    string   buffer      = input.ReadLine();
                    string[] bufferArray = new string[verticesNumber];
                    bufferArray = buffer.Split(' ');

                    for (int j = 0; j < verticesNumber; j++)
                    {
                        graph.SetWeightAt(i, j, Convert.ToDouble(bufferArray[j]));
                    }
                }

                input.Close();
            }

            return(graph);
        }
Ejemplo n.º 8
0
        private void pictureBoxCanvas_MouseClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; graph != null && i < graph.VerticesNumber; i++)
            {
                WOGraphVertice vertice = graph.GetVerticeAt(i);

                if ((e.X < vertice.Coordinates.X + 5 && e.X > vertice.Coordinates.X - 5) &&
                    (e.Y < vertice.Coordinates.Y + 5 && e.Y > vertice.Coordinates.Y - 5))
                {
                    dragVertice   = true;
                    verticeToDrag = i;
                    break;
                }
            }

            if (dragVertice != true)
            {
                Painter.DrawPoint(new Point(e.X, e.Y), pictureBoxCanvas);

                if (graph == null)
                {
                    graph = new WOGraph(0);
                }

                graph.AddVertice(e.X, e.Y);

                textBoxVerticesNumber.Text = graph.VerticesNumber.ToString();
            }
            else
            {
                if (e.Button == MouseButtons.Right)
                {
                    dragVertice = false;
                }
            }
        }