Esempio n. 1
0
        //adding edge
        private void btn_add_edge_Click(object sender, EventArgs e)
        {
            _selectedVerticle = null;
            foreach (var ve in listOfVerticles)
            {
                ve.isSelected = false;
            }
            Drawing_panel.Invalidate();
            var aef = new AddEdgeForm {
                Verticles = listOfVerticles, Edges = listOfEdges
            };

            if (listOfVerticles.Count < 2)
            {
                MessageBox.Show("В графе менее 2 вершин, создать ребро нельзя.", "Ошибка!");
                return;
            }

            aef.ShowDialog();
            if (aef.DialogResult != DialogResult.OK)
            {
                return;
            }
            listOfEdges.Add(aef.Return);
            listOfVerticles.Find(x => x.Equals(aef.Return.A)).connections++;
            listOfVerticles.Find(x => x.Equals(aef.Return.B)).connections++;
            RenewLists();
            RandomizeWeightsPositions(listOfEdges);
            Drawing_panel.Refresh();
        }
Esempio n. 2
0
        private void Drawing_panel_MouseDown(object sender, MouseEventArgs e)
        {
            if (lb_verticle.SelectedIndex != -1)
            {
                lb_verticle.SetSelected(lb_verticle.SelectedIndex, false);
            }
            if (lb_edges.SelectedIndex != -1)
            {
                lb_edges.SetSelected(lb_edges.SelectedIndex, false);
            }
            if (e.Button != MouseButtons.Left)
            {
                wwClick = WhereWasClick.DrawingPanel;
                return;
            }

            Verticle select = null;

            foreach (var verticle in listOfVerticles)
            {
                int dx = e.X - verticle.point.X, dy = e.Y - verticle.point.Y;
                if (!(Math.Sqrt(dx * dx + dy * dy) <= verticleRadius))
                {
                    continue;
                }
                select = verticle;
                break;
            }

            if (Equals(_selectedVerticle, select) || select == null)
            {
                if (_selectedVerticle != null)
                {
                    _selectedVerticle.isSelected = false;
                    _selectedVerticle            = null;
                    Drawing_panel.Invalidate();
                }
                return;
            }
            if (_selectedVerticle == null)
            {
                _selectedVerticle = select;
                if (_selectedVerticle != null)
                {
                    _selectedVerticle.isSelected = true;
                }
                Drawing_panel.Invalidate();
            }
            else
            {
                if (listOfEdges.Any(ed => Equals(ed.A, select) && Equals(ed.B, _selectedVerticle) ||
                                    Equals(ed.A, _selectedVerticle) && Equals(ed.B, select)))
                {
                    return;
                }

                var aef = new AddEdgeForm
                {
                    Verticles           = listOfVerticles,
                    Edges               = listOfEdges,
                    SetDefaultVerticles = true,
                    Va = _selectedVerticle,
                    Vb = select
                };
                aef.ShowDialog();
                if (aef.DialogResult != DialogResult.OK)
                {
                    if (_selectedVerticle != null)
                    {
                        _selectedVerticle.isSelected = false;
                    }
                    _selectedVerticle.isSelected = false;
                    _selectedVerticle            = null;
                    Drawing_panel.Invalidate();
                    return;
                }

                listOfEdges.Add(aef.Return);
                listOfVerticles.Find(x => x.Equals(aef.Return.A)).connections++;
                listOfVerticles.Find(x => x.Equals(aef.Return.B)).connections++;
                RandomizeWeightsPositions(listOfEdges);
                RenewLists();
                if (_selectedVerticle != null)
                {
                    _selectedVerticle.isSelected = false;
                }
                Drawing_panel.Refresh();
                _selectedVerticle = null;
            }
        }