/// <summary> /// Thêm đỉnh vào đồ thị /// </summary> /// <param name="point"></param> public TopGraphPoint addPoint(int r,Color color,int x,int y) { if (countPoint >= Points.Length) throw new Exception("Vượt quá số đỉnh tối đa của đồ thị"); else { int index = countPoint; if (countPoint < alphabet.Length) { index = countPoint%alphabet.Length; } TopGraphPoint point= new TopGraphPoint(new string(alphabet[index], 1), r, color, x, y); this.Points[countPoint] = point; this.countPoint++; return point; } }
private bool inblacklist(TopGraphPoint point1,TopGraphPoint point2) { foreach (var item in blackList) { if (item[0] == point1 && item[1] == point2) return true; } return false; }
/// <summary> /// Nối 2 điểm sử dụng khi vẽ cây khung nhỏ nhất /// </summary> /// <param name="point1"></param> /// <param name="point2"></param> /// <param name="color"></param> /// <param name="size"></param> /// <param name="pictureBox"></param> private void connectPoint(TopGraphPoint point1, TopGraphPoint point2, Color color) { Graphics G = pictureBox.CreateGraphics(); G.SmoothingMode = SmoothingMode.AntiAlias; Pen p = new Pen(color, 4); p.StartCap = LineCap.Round; p.EndCap = LineCap.ArrowAnchor; p.DashCap = DashCap.Triangle; int x1 = point1.x, y1 = point1.y, x2 = point2.x, y2 = point2.y; if (point1.x < point2.x) { x1 = point1.x + point1.r; x2 = point2.x - point1.r; } else { x1 = point1.x - point1.r; x2 = point2.x + point2.r; } if (point1.y < point2.y) { y1 = point1.y + point1.r; y2 = point2.y - point2.r; } else { y1 = point1.y - point1.r; y2 = point2.y + point2.r; } #region Vẽ đồ thị có hướng if (this.GrapType == 1) { if (point1 > point2) { x1 += 10; y1 += 10; x2 += 10; y2 += 10; } else { x1 -= 10; y1 -= 10; x2 -= 10; y2 -= 10; } } #endregion #region Vẽ đồ thị vô hướng else { //G.DrawLine(p, x1, y1, x2, y2); } #endregion G.DrawLine(p, x1, y1, x2, y2); }
/// <summary> /// Nối 2 điểm với nhau /// </summary> /// <param name="point1"></param> /// <param name="point2"></param> /// <param name="color"></param> /// <param name="pictureBox"></param> private void connectPoint(TopGraphPoint point1, TopGraphPoint point2,int value) { Graphics G = pictureBox.CreateGraphics(); G.SmoothingMode=SmoothingMode.AntiAlias; Pen p; #region Vẽ đồ thị có hướng int x1 = point1.x, y1 = point1.y, x2 = point2.x, y2 = point2.y; if (this.GrapType == 1) { p = new Pen(point1.color, 3); p.StartCap = LineCap.Round; p.EndCap = LineCap.ArrowAnchor; p.DashCap = DashCap.Triangle; if (point1.x < point2.x) { x1 = point1.x + point1.r; x2 = point2.x - point1.r; } else { x1 = point1.x - point1.r; x2 = point2.x + point2.r; } if (point1.y < point2.y) { y1 = point1.y + point1.r; y2 = point2.y - point2.r; } else { y1 = point1.y - point1.r; y2 = point2.y + point2.r; } if (point1 > point2) { x1 += 10; y1 += 10; x2 += 10; y2 += 10; } else { x1 -= 10; y1 -= 10; x2 -= 10; y2 -= 10; } } #endregion #region Vẽ đồ thị vô hướng else { p = new Pen(point1.color, 2); if (point1.x < point2.x) { x1 = point1.x + point1.r; x2 = point2.x - point1.r; } else { x1 = point1.x - point1.r; x2 = point2.x + point2.r; } if (point1.y < point2.y) { y1 = point1.y + point1.r; y2 = point2.y - point2.r; } else { y1 = point1.y - point1.r; y2 = point2.y + point2.r; } } int x_text = (x1 + x2) / 2 - point1.r / 2; int y_text = (y1 + y2) / 2 - point1.r / 2; G.DrawLine(p, x1, y1, x2, y2); Rectangle rectangle = new Rectangle(x_text, y_text, point1.r, point1.r); G.FillEllipse(new SolidBrush(Color.White), rectangle); TextRenderer.DrawText(G, value.ToString(), new Font("Arial", 10, FontStyle.Bold), rectangle, point2.color); G.DrawEllipse(new Pen(point1.color), rectangle); #endregion }
private bool checkPointInPrim(TopGraphPoint point) { foreach (TopGraphPoint topGraphPoint in point_in_graph) { if (point==topGraphPoint) return true; } return false; }
private bool addPointoLIst(TopGraphPoint point1,TopGraphPoint point2) { foreach (TopGraphPoint topGraphPoint in point_in_graph) { if(point1==topGraphPoint||point2==topGraphPoint||point1==point2) return false; } point_in_graph.Add(point1); //point_in_graph.Add(point2); return true; }
/// <summary> /// Tìm cây khung nhỏ nhất và vẽ /// </summary> /// <param name="pictureBox"></param> public void PrimDraw() { #region Xử lý đồ thị vô hướng if (this.GrapType == 0) { int curent_point=0; List<int> backup_point=new List<int>(); int num_try = 0; bool flag = false; while (point_in_graph.Count < Points.Length-1) { if (num_try > Points.Length-1) { point_in_graph.Remove(Points[curent_point]); curent_point = backup_point[backup_point.Count-1]; backup_point.Remove(backup_point[backup_point.Count - 1]); rollback(); num_try = 0; } TopGraphPoint[] tmp = new TopGraphPoint[2]; tmp[0] = Points[curent_point]; tmp[1] = Points[getMin(curent_point)]; if (addPointoLIst(tmp[0], tmp[1])) { backup_point.Add(curent_point); curent_point = getMin(curent_point); couplePoint.Add(tmp); } if (backup_point.Count == 0) { if (flag&&curent_point<Points.Length-1) { //Đổi điểm xuất phát mới curent_point++; point_in_graph.Clear(); blackList.Clear(); couplePoint.Clear(); flag = false; } else { point_in_graph.Remove(point_in_graph[0]); flag = true; } } num_try++; } bool first = true; foreach (var topGraphPointse in couplePoint) { if (first) { topGraphPointse[0].color=Color.Black; topGraphPointse[0].Draw(pictureBox); first = false; } this.connectPoint(topGraphPointse[0],topGraphPointse[1],Color.FromArgb(150,Color.Black)); Thread.Sleep(1000); } } #endregion #region Xử lý đồ thị có hướng (Chưa viết được) else { int curent_point = 0; List<int> backup_point = new List<int>(); int num_try = 0; bool flag = false; while (point_in_graph.Count < Points.Length - 1) { if (num_try > Points.Length - 1) { point_in_graph.Remove(Points[curent_point]); curent_point = backup_point[backup_point.Count - 1]; backup_point.Remove(backup_point[backup_point.Count - 1]); rollback(); num_try = 0; } TopGraphPoint[] tmp = new TopGraphPoint[2]; tmp[0] = Points[curent_point]; tmp[1] = Points[getMin(curent_point)]; if (addPointoLIst(tmp[0], tmp[1])) { backup_point.Add(curent_point); curent_point = getMin(curent_point); couplePoint.Add(tmp); } if (backup_point.Count == 0) { if (flag && curent_point < Points.Length - 1) { //Đổi điểm xuất phát mới curent_point++; point_in_graph.Clear(); blackList.Clear(); couplePoint.Clear(); flag = false; } else { point_in_graph.Remove(point_in_graph[0]); flag = true; } } num_try++; } bool first = true; foreach (var topGraphPointse in couplePoint) { if (first) { topGraphPointse[0].color = Color.Black; topGraphPointse[0].Draw(pictureBox); first = false; } this.connectPoint(topGraphPointse[0], topGraphPointse[1], Color.FromArgb(150,Color.Black)); Thread.Sleep(1000); } } #endregion MessageBox.Show("Đã vẽ cây khung nhỏ nhất"); Thread.CurrentThread.Abort(); }