c行中间的箭头
本文关键字:中间 | 更新日期: 2023-09-27 18:29:31
我正在做一个项目,在这个项目中,我必须绘制不同的节点(连接),然后显示它们之间的连接。简单地说,我使用椭圆类在ViewBox
内部的Canvas上绘制坐标为(x,y)的节点。然后,我所做的就是读取链接的起始和结束坐标,并将它们存储在List中,通过读取List,我将它们添加到画布中
我有以下代码在canvas
上画一条线,它读取起点和终点:
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)的路径,即从起点到终点的点。有人能给我指个路吗?
好了,我现在已经解决了自己的问题。我跟随PETZOLD BOOK BLOG
下载了文件并使用了我需要的三个类。
- 箭头线.cs
- ArrowEnds.cs
- 箭头基线.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);
}
然后,我向ArrowLineBase.cs中的函数PathFigure添加了2行代码,作为:
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.