/// <summary> /// 最佳一条新的Link,并返回一条新的路线。 /// </summary> /// <exception cref="ArgumentNullException">如果link为空引用,则跑出该异常。</exception> public MetroPath Append(MetroLink link) { if (link == null) { throw new ArgumentNullException("link"); } MetroPath newPath = new MetroPath(); newPath.Links.AddRange(this.Links); newPath.Links.Add(link); return(newPath); }
/// <summary> /// 绘制地铁站点间的线路。 /// </summary> /// <param name="g">绘图图面。</param> /// <param name="link">地铁站点间的线路。</param> private void PaintLink(Graphics g, MetroLink link, bool IsCurLine) { Point pt1 = new Point(link.From.X, link.From.Y); Point pt2 = new Point(link.To.X, link.To.Y); Pen pen; if (IsCurLine) { pen = new Pen(link.Line.Color, 9); } else { pen = new Pen(link.Line.Color, 7); } using (pen) { pen.LineJoin = LineJoin.Round; if (link.Flag == 0) {//单线 g.DrawLine(pen, pt1, pt2); } else if (link.Flag > 0) {//双线并轨(如果是同向,则Flag分别为1和2,否则都为1) float scale = (pen.Width / 2) / Distance(pt1, pt2); float angle = (float)(Math.PI / 2); if (link.Flag == 2) { angle *= -1; } //平移线段 var pt3 = Rotate(pt2, pt1, angle, scale); var pt4 = Rotate(pt1, pt2, -angle, scale); g.DrawLine(pen, pt3, pt4); } } }
private void BtnAddNodeC_Click(object sender, EventArgs e) { try { bool IsDouLine1 = false; bool IsDouLine2 = false; MetroNode NewNode = new MetroNode(); MetroNode PreNode = new MetroNode(); MetroNode NexNode = new MetroNode(); String Prename = (CobPreNode.Text.ToString() == "海伦路" && CobNexNode.Text.ToString() == "宝山路") || (CobPreNode.Text.ToString() == "宝山路" && CobNexNode.Text.ToString() == "海伦路") ? "3" : CobPreNode.Text.ToString(); String Nexname = (CobPreNode.Text.ToString() == "海伦路" && CobNexNode.Text.ToString() == "宝山路") || (CobPreNode.Text.ToString() == "宝山路" && CobNexNode.Text.ToString() == "海伦路") ? "2" : CobNexNode.Text.ToString(); foreach (var node in metroGraphView1.Graph.Nodes) { if (node.Name == Prename) { PreNode = node; } if (node.Name == Nexname) { NexNode = node; } } NewNode.Name = this.TxtNodeName.Text.ToString(); NewNode.X = (CobPreNode.Text.ToString() == "海伦路" && CobNexNode.Text.ToString() == "宝山路") || (CobPreNode.Text.ToString() == "宝山路" && CobNexNode.Text.ToString() == "海伦路") ? 2033 : int.Parse(this.TxtNodeX.Text.ToString()); NewNode.Y = (CobPreNode.Text.ToString() == "海伦路" && CobNexNode.Text.ToString() == "宝山路") || (CobPreNode.Text.ToString() == "宝山路" && CobNexNode.Text.ToString() == "海伦路") ? 750 : int.Parse(this.TxtNodeY.Text.ToString()); if ((Math.Abs(NewNode.X - PreNode.X) > 200 || Math.Abs(NewNode.Y - PreNode.Y) > 200 || Math.Abs(NewNode.X - NexNode.X) > 200 || Math.Abs(NewNode.Y - NexNode.Y) > 200) && (PreNode.Name != "" && NexNode.Name != "")) { MessageBox.Show("距离太远了哦,请重新设定坐标(´▽`ʃ♡ƪ)"); this.TxtNodeX.Text = ""; this.TxtNodeY.Text = ""; return; } for (int i = 0; i < PreNode.Links.Count; i++) { if (PreNode.Links[i].Flag == 1 && PreNode.Links.Count > 3) { IsDouLine1 = true; } } for (int i = 0; i < NexNode.Links.Count; i++) { if (NexNode.Links[i].Flag == 1 && NexNode.Links.Count > 3) { IsDouLine2 = true; } } if (!IsDouLine2 || !IsDouLine1) { MetroLine NewLine = new MetroLine(); foreach (var line in metroGraphView1.Graph.Lines) { if (line.Name == CobNodeLine.Text.ToString()) { NewLine = line; } } if (!((CobPreNode.Text == "" || CobPreNode.Text == null) && NexNode.Links.Count <= 1)) { MetroLink NewLink = new MetroLink(NewLine, NewNode, PreNode); NewLink.Weight = 3; NewNode.Links.Add(NewLink); NewLink.Flag = 0; } if (!((CobNexNode.Text == "" || CobNexNode.Text == null) && PreNode.Links.Count <= 1)) { MetroLink NewLink = new MetroLink(NewLine, NewNode, NexNode); NewLink.Flag = 0; NewLink.Weight = 3; NewNode.Links.Add(NewLink); } if (!((CobPreNode.Text == "" || CobPreNode.Text == null) && NexNode.Links.Count <= 1)) { for (int i = 0; i < PreNode.Links.Count; i++) { if (PreNode.Links[i].To.Name == NexNode.Name) { PreNode.Links.RemoveAt(i); } } } PreNode.Links.Add(new MetroLink(NewLine, PreNode, NewNode)); if (!((CobNexNode.Text == "" || CobNexNode.Text == null) && PreNode.Links.Count <= 1)) { for (int i = 0; i < NexNode.Links.Count; i++) { if (NexNode.Links[i].To.Name == PreNode.Name) { NexNode.Links.RemoveAt(i); } } } NexNode.Links.Add(new MetroLink(NewLine, NexNode, NewNode)); metroGraphView1.Graph.Nodes.Add(NewNode); } else { for (int i = 0; i < PreNode.Links.Count; i++) { if (PreNode.Links[i].To.Name == NexNode.Name) { MetroLine NewLine = new MetroLine(); NewLine = PreNode.Links[i].Line; MetroLink NewLink = new MetroLink(NewLine, PreNode, NewNode); NewLink.Flag = PreNode.Links[i].Flag; MetroLink SNewLink = new MetroLink(NewLine, NewNode, NexNode); SNewLink.Flag = PreNode.Links[i].Flag; SNewLink.Weight = 3; NewNode.Links.Add(SNewLink); PreNode.Links.RemoveAt(i); PreNode.Links.Add(NewLink); } } for (int i = 0; i < NexNode.Links.Count; i++) { if (NexNode.Links[i].To.Name == PreNode.Name) { MetroLine NewLine = new MetroLine(); NewLine = NexNode.Links[i].Line; MetroLink NewLink = new MetroLink(NewLine, NexNode, NewNode); NewLink.Flag = NexNode.Links[i].Flag; MetroLink SNewLink = new MetroLink(NewLine, NewNode, PreNode); SNewLink.Flag = NexNode.Links[i].Flag; SNewLink.Weight = 3; NewNode.Links.Add(SNewLink); NexNode.Links.RemoveAt(i); NexNode.Links.Add(NewLink); } } metroGraphView1.Graph.Nodes.Add(NewNode); } //metroGraphView1.SaveToFile(Application.StartupPath + "\\MetroGraph.xml"); metroGraphView1.Invalidate(); this.CobPreNode.Text = ""; this.CobNexNode.Text = ""; this.TxtNodeName.Text = ""; this.TxtNodeX.Text = ""; this.TxtNodeY.Text = ""; this.TxtLineName.Text = ""; this.CobNodeLine.Text = ""; this.colorDialog1.Reset(); this.LabNexXY.Text = ""; this.LabPreXY.Text = ""; } catch { MessageBox.Show("请检查是否有误哦(✿◕‿◕✿)"); return; } this.PnlAddNode.Visible = false; this.BtnMetroHelp.Visible = true; this.BtnAdd.Visible = true; this.BtnSave.Visible = true; }