Example #1
0
        public UiVertexMenu(Vertex v, Graph g, float x, float y)
        {
            visible = true;
            children = new List<UiComponent>();

            X = (int)x;
            Y = (int)y;

            this.v = v;
            graph = g;
            topMenu = new UiVerticalMenu();
            topMenu.AddItem("Премахни връх", RemoveVertex);

            topMenu.X = 3;
            topMenu.Y = 3;

            Width = topMenu.Width + 6;
            Height = topMenu.Height + 6;

            AddChild(topMenu);

            int yp = topMenu.Height + 6; 

            if(v.edges.Count > 0)
            { // изброява всички ребра
                UiLabel label = new UiLabel("Премахни ребро:", GraphicScheme.font1);
                label.X = 3;
                label.Y = yp;
                yp += label.Height + 3;
                AddChild(label);

                bottomMenu = new UiVerticalMenu();

                bottomMenu.X = 3;
                bottomMenu.Y = yp;

                bottomMenu.Width = topMenu.Width;

                Edge e;
                Vertex vo;                

                for (int i = 0; i < v.edges.Count; i++)
                {
                    e = v.edges[i];
                    vo = e.source == v ? e.destination : e.source;
                    dict.Add(bottomMenu.AddItem(vo.id.ToString(), RemoveEdge).id, e);
                }

                AddChild(bottomMenu);
                yp += bottomMenu.Height;

                Height = yp + 3;
            }
            
        }
Example #2
0
        /// <summary>
        /// Чете граф от файл. Формат - списък на съседи
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="graph"></param>
        public static void ReadGraphFile(string filename, ref Graph graph)
        {
            String[] lines = File.ReadAllLines(filename);

            int lineIdx = 0;
            try
            {
                String[] parts = lines[0].Split(' ');
                int n = int.Parse(parts[0]);
                int m = int.Parse(parts[1]);

                Vertex.ResetCounter();
                for (int i = 0; i < n; i++) graph.vertices.Add(new Vertex());

                lineIdx++;
                for(int i = 0; i < m; i++)
                {
                    parts = lines[lineIdx].Split(' ');
                    int src = int.Parse(parts[0]);
                    int dest = int.Parse(parts[1]);

                    Edge e = graph.AddEdge(src, dest);

                    if(parts.Length > 2)
                    { // част за четене на тегла
                        int val = 0;
                        try
                        {
                            val = int.Parse(parts[2]);
                            int propId = Property.GetPropertyId("тегло");
                            Property.SetSpecialProperty(propId, SpecialProperty.EdgeWeight);
                            e.SetProperty(propId, int.Parse(parts[2]));
                        }
                        catch(Exception exc)
                        {
                            Console.WriteLine(String.Format("Line {0}: third part was NaN: {1}", lineIdx, parts[2]));
                        }
                        
                    }

                    lineIdx++;
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(String.Format("An exception occured on line {0}: {1}", lineIdx, e.Message), e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                graph = new Graph();
                Vertex.ResetCounter();
                return;
            }
            graph.ArrangeInCircle();
        }
Example #3
0
        public MainUI(Connector con)
        {
            connector = con;
            activeGraph = connector.GraphInstance;
            activeGraph.ArrangeInCircle();

            //DumpProperties();

            algoTimer = new Timer(150); // при създаване с конектор(т.е. алгоритъм), се създава и таймера за автоматичен
            algoTimer.Elapsed += algoTimer_Elapsed; // ход
            connector.AlgorithmSuspended += connector_AlgorithmSuspended;
            Start();
        }
Example #4
0
        public UiEdgeMenu(Edge e, Graph g, MainUI ui, float x, float y)
        {
            edge = e;
            graph = g;

            X = (int)x;
            Y = (int)y;

            this.ui = ui;

            AddItem("Премахни", RemoveEdge);
            AddItem("Промени тегло", EditEdge);
        }
Example #5
0
        /// <summary>
        /// Записва граф чрез списък на съседи
        /// </summary>
        /// <param name="filename">Файла</param>
        /// <param name="graph">Графа</param>
        public static void WriteGraphFile(string filename, Graph graph)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine(String.Format("{0} {1}", graph.vertices.Count, graph.edges.Count));

            int offset = graph.vertices.Last().id - graph.vertices.Count + 1;

            foreach (Edge e in graph.edges)
            {
                object w = e.Weight;
                if(e == null) builder.AppendLine(String.Format("{0} {1}", e.source.id - offset, e.destination.id - offset));
                else builder.AppendLine(String.Format("{0} {1} {2}", e.source.id - offset, e.destination.id - offset, w));
            }
            
            File.WriteAllText(filename, builder.ToString());
        }
Example #6
0
 /// <summary>
 /// Зарежда шрифта. Получават се бъгове, ако не се извика
 /// </summary>
 protected void SetupGui()
 {
     if (graph == null) graph = new Graph();
     GraphicScheme.LoadFont();
 }
Example #7
0
 void menu_FileOpen(UiComponent sender, Object arg)
 {
     System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
     if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         Graph g = new Graph();
         IOHandler.ReadGraphFile(ofd.FileName, ref g);
         ChangeGraph(g);
         DisableEdgeAdding();
         DisablePhysics();
     }
 }
Example #8
0
        void menu_Generate(UiComponent sender, Object arg)
        {
            Vertex.ResetCounter();
            Graph g = new Graph();
            int n = random.Next(50);
            int m = random.Next(100);
            for (int i = 0; i < n; i++)
            {
                Vertex v = new Vertex((float)random.NextDouble() * 1000 - 500.0f, (float)random.NextDouble() * 600 - 300.0f);
                g.AddVertex(v);
                if (i == 0) continue;

                int connections = random.Next(1, (int)Math.Min(i, 3));
                for(int j = 0; j < connections; j++)
                {
                    int vo = random.Next(0, i);

                    bool contains = false;
                    foreach(Edge e in v.edges)
                    {
                        if(e.source.id == vo || e.destination.id == vo)
                        {
                            contains = true;
                            break;
                        }
                    }
                    if (contains) j--;
                    else g.AddEdge(v.id, vo);
                }
            }

            

            fs.SetForce(50);

            activeGraph = g;
            fs.SetGraph(g);
        }
Example #9
0
        /// <summary>
        /// Създава първоначален граф
        /// </summary>
        void MakeGraph()
        {
            // ИЗВЪН ЕКСПЛОАТАЦИЯ
            //Property.GetPropertyId("цвят")
            Property.GetPropertyId("име");
            Property.GetPropertyId("цена");
            Property.GetPropertyId("поток");
            Property.GetPropertyId("обратен поток");

            activeGraph = new Graph();
            
            Vertex v = new Vertex(0, 0);
            v.SetProperty(0, "Варна");

            Vertex v1 = new Vertex(100, 0);
            v1.SetProperty(0, "София");
            v1.SetProperty(1, 512.25f);

            Vertex v2 = new Vertex(100, 0);
            v2.SetProperty(0, "Шумен");
            v2.SetProperty(2, 112.125f);

            Vertex v3 = new Vertex(100, 0);
            v3.SetProperty(0, "Стара Загора");
            v3.SetProperty(3, 12.725f);

            activeGraph.AddVertex(v);
            activeGraph.AddVertex(v1);
            activeGraph.AddVertex(v2);
            activeGraph.AddVertex(v3);

            Edge e1 = activeGraph.AddEdge(0, 3);
            e1.SetProperty(2, 200);

        }
Example #10
0
 public MainUI()
 {
     activeGraph = new Graph(false);
     Start();
 }
 public void SetGraph(Graph graph)
 {
     this.graph = graph;
 }
Example #12
0
        /// <summary>
        /// Чете данните за графа и го създава
        /// </summary>
        /// <param name="graphDataPtr">указател към буфера</param>
        void ReadGraphData(IntPtr graphDataPtr)
        {
            GraphicScheme.LoadFont();

            byte[] gdBuff = new byte[9]; // информация за графа: [брой върхове] [брой ребра] [насочен]

            int nRead;

            bool result = NativeMethods.ReadProcessMemory(processHandle, graphDataPtr, gdBuff, 9, out nRead);

            int[] count = new int[2];
            Buffer.BlockCopy(gdBuff, 0, count, 0, 8);

            //MessageBox.Show(String.Format("Vertices: {0}, Edges: {1}", count[0], count[1]));

            graph = new Graph(gdBuff[8]!=0);
            graph.vertices = new List<Vertex>(count[0]);


            Vertex.ResetCounter(); // better safe than sorry
            Edge.ResetCounter();

            for(int i = 0; i < count[0]; i++)
            {
                graph.AddVertex(new Vertex());
            }

            int[] edges = new int[count[1] * 2];

            byte[] edgeBuff = new byte[count[1] * 8];
            result = NativeMethods.ReadProcessMemory(processHandle, graphDataPtr + 9, edgeBuff, count[1] * 8, out nRead);

            Buffer.BlockCopy(edgeBuff, 0, edges, 0, count[1] * 8);

            for(int i = 0; i < count[1]; i++)
            {
                graph.AddEdge(edges[2 * i], edges[2 * i + 1]);
            }
        }
Example #13
0
        public static void SaveGraphPicture(string filename, Graph graph)
        {
            float minX = 999999;
            float maxX = -999999;
            float minY = 999999;
            float maxY = -99999;

            foreach(Vertex v in graph.vertices)
            {
                if (v.x < minX) minX = v.x;
                if (v.x > maxX) maxX = v.x;
                if (v.y < minY) minY = v.y;
                if (v.y > maxY) maxY = v.y;
            }

            minX -= 25;
            maxX += 25;
            minY -= 25;
            maxY += 25;

            uint width = 3*(uint)(maxX - minX);
            uint height = 3*(uint)(maxY - minY);

            RenderTexture tx = new RenderTexture(width, height);
            tx.Clear(Color.Black);

            RenderFrame rf = new RenderFrame();
            rf.width = width;
            rf.height = height;
            rf.xCenter = minX + width;
            rf.yCenter = minY + height;
            rf.zoom = 1.2f;

            graph.DrawSelf(tx, rf);

            tx.Texture.CopyToImage().SaveToFile("graph.png");
        }
Example #14
0
 /// <summary>
 /// Сменя текущия граф с нов
 /// </summary>
 /// <param name="newGraph"></param>
 void ChangeGraph(Graph newGraph)
 {
     lastClickedVertex = null;
     activeGraph = newGraph;
     fs.SetGraph(activeGraph);
 }