private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { Refresh(); var need = true; var click = new Point(e.X - _offset, e.Y - _offset); if (e.Button == MouseButtons.Left) { if (!_graph.GetVerts().Any()) { var v = _graph.AddVert(new Vert(click)); dataGridView1.Rows.Add(dataGridView1.RowCount, (v.Position.X - pictureBox1.Width / 2) / 2, (-v.Position.Y + pictureBox1.Height / 2) / 2); _edgeFrom = v; _edging = true; } else { Vert connectTo = null; foreach (var v in _graph.GetVerts()) { if (Helper.InCircle(v.Position, click, Vert.R)) { need = false; connectTo = v; } } if (need) { connectTo = _graph.AddVert(new Vert(click)); dataGridView1.Rows.Add(dataGridView1.RowCount, (connectTo.Position.X - pictureBox1.Width / 2) / 2, (-connectTo.Position.Y + pictureBox1.Height / 2) / 2); if (!_edging) { _edgeFrom = connectTo; _edging = true; } else { if (!_edgeFrom.LinkedVers.Contains(connectTo)) { _edgeFrom.AddAsLinked(connectTo); } if (!connectTo.LinkedVers.Contains(_edgeFrom)) { connectTo.AddAsLinked(_edgeFrom); } _edging = false; } } else { if (_edging) { if (!_edgeFrom.LinkedVers.Contains(connectTo)) { _edgeFrom.AddAsLinked(connectTo); } if (!connectTo.LinkedVers.Contains(_edgeFrom)) { connectTo.AddAsLinked(_edgeFrom); } _edging = false; } else { _edgeFrom = connectTo; _edging = true; } } } } if (e.Button == MouseButtons.Right) { foreach (var v in _graph.GetVerts()) { if (Helper.InCircle(v.Position, click, Vert.R)) { dataGridView1.Rows.RemoveAt(_graph.GetVerts().IndexOf(v)); _graph.RemoveVert(v); foreach (var ver in _graph.GetVerts().Where(ver => ver.LinkedVers.Contains(v))) { ver.LinkedVers.Remove(v); } for (var i = 0; i < dataGridView1.RowCount - 1; i++) { dataGridView1.Rows[i].Cells[0].Value = i + 1; } break; } } } Refresh(); }
/// <summary> /// Удалить вершину /// </summary> /// <param name="vert">Удаляемая вершина</param> public void RemoveVert(Vert vert) { _verts.Remove(vert); }
/// <summary> /// Добавить связанную вершину /// </summary> /// <param name="vert">Вершина</param> public void AddAsLinked(Vert vert) { LinkedVers.Add(vert); }
///<summary> /// Добавляет вершину в граф /// </summary> ///<returns>Возвращает добавленную вершину</returns> public Vert AddVert(Vert vert) { _verts.Add(vert); return(vert); }