2016-01-08 36 views
-1

我正在一個項目中,我必須繪製不同的節點(路口),然後顯示它們之間的連接。簡單地說,我用橢圓類ViewBox裏面的Canvas裏面畫了座標爲(x,y)的節點。然後,我所做的就是讀取鏈接的開始和結束座標,並將它們存儲在列表中,並通過閱讀列表中,我將它們添加到畫布上。
我有以下代碼借鑑canvas的一行字:起點和終點:在行中間的箭頭c#

foreach(LineProperty lnp in lstLnPro){ 
      Line ln = new Line(); 
      ln = ds.drawLine(lnp.x1, lnp.y1, lnp.x2, lnp.y2); 
      ln.MouseEnter += ln_MouseEnter; 
      ln.MouseLeave += ln_MouseLeave; 


      canvasName.Children.Add(ln); 
     } 

和DS對象調用的drawLine功能。

public Line drawLine(double x1, double y1, double x2, double y2) { 
     Line ln = new Line(); 
     ln.X1 = x1; 
     ln.Y1 = y1; 
     ln.X2 = x2; 
     ln.Y2 = y2; 
     ln.StrokeThickness = 1; 
     ln.Visibility = System.Windows.Visibility.Visible; 
     ln.Stroke = System.Windows.Media.Brushes.Green; 
     return ln; 
    } 

現在,我需要從起點到終點涉及這些畫線即具有在其中示出了從(X1,Y1)到(X2,Y2)的路徑的中間的箭頭即點。有人能給我一個方向嗎?

+0

看到這個答案http://stackoverflow.com/a/16714169/1538014 – mkb

+0

但我想要一個箭頭在行中。我試圖自定義代碼,但我失敗了。 – Skaranjit

回答

1

好吧,我現在已經解決了我自己的問題。我遵循PETZOLD BOOK BLOG

並下載了該文件並使用了我需要的三個類。

  • ArrowLine.cs
  • ArrowEnds.cs
  • ArrowLineBase.cs

然後,我改變了我的代碼:

 foreach(LineProperty lnp in lstLnPro){ 
      ArrowLine line= new ArrowLine(); 
      line.Stroke = Brushes.Green; 
      line.StrokeThickness = 1; 
      line.X1 = lnp.x1; 
      line.Y1 = lnp.y1; 
      line.X2 = lnp.x2; 
      line.Y2 = lnp.y2; 
      line.ArrowLength = 3; 

      canvasName.Children.Add(line); 

}

然後,我添加將2行代碼添加到Ar中的函數PathFigure中rowLineBase.cs爲:

PathFigure CalculateArrow(PathFigure pathfig, Point pt1, Point pt2) 
    { 
     Matrix matx = new Matrix(); 



     Vector vect = pt1 - pt2; 
     vect.Normalize(); 
     vect *= ArrowLength; 


     PolyLineSegment polyseg = pathfig.Segments[0] as PolyLineSegment; 
     polyseg.Points.Clear(); 
     matx.Rotate(ArrowAngle/2); 

     //added code starts 
     //places the position of the arrow on the midpoint 
     pt2.X = (pt2.X + pt1.X)/2; 
     pt2.Y = (pt2.Y + pt1.Y)/2; 
     //added code ends 

     pathfig.StartPoint = pt2 + vect * matx; 
     polyseg.Points.Add(pt2); 

     matx.Rotate(-ArrowAngle); 
     polyseg.Points.Add(pt2 + vect * matx); 
     pathfig.IsClosed = IsArrowClosed; 

     return pathfig; 
    } 

添加的代碼將箭頭的位置放在繪製線的中點。只需使用中點公式。您可以通過在ArrowEnds.cs中添加枚舉來標準化代碼,並添加邏輯ArrowLineBase.cs