旋转几何路径

时间:2011-06-17 11:53:09

标签: wpf graphics path geometry

我正在使用Streamgeometry新工作来绘制一个简单的箭头。现在我需要将箭头转到指定的角度。但是如何旋转这个几何体?

 
Dim pt1 As New Point(X1, Me.Y1) 'left point
Dim pt2 As New Point(_X2, Me.Y2) 'right point

Dim pt3 As New Point(_X2 + (HeadWidth * cost - HeadHeight * sint), Y2 + (HeadWidth * sint + HeadHeight * cost)) 'arrow line down
Dim pt4 As New Point(_X2 + (HeadWidth * cost + HeadHeight * sint), Y2 - (HeadHeight * cost - HeadWidth * sint)) 'arrow line up

context.BeginFigure(pt1, True, False)
context.LineTo(pt2, True, True)
context.LineTo(pt3, True, True)
context.LineTo(pt2, True, True)
context.LineTo(pt4, True, True)

1 个答案:

答案 0 :(得分:3)

如果旋转仅用于演示(即您不关心原始几何数据仍然是指向原始方向的箭头),那么您可以对其应用transform

在绘制上下文之后,只需对原始StreamGeometry对象应用转换(C#中的代码,但它也适用于VB.NET):

var geo = new StreamGeometry();
using (var ctx = geo.Open())
{
    ctx.BeginFigure(new Point(0, 20), false, false);
    ctx.LineTo(new Point(100, 20), true, true);
    ctx.LineTo(new Point(80, 40), true, true);
    ctx.LineTo(new Point(80, 0), true, true);
    ctx.LineTo(new Point(100, 20), true, true);
}
geo.Transform = new RotateTransform(45);
var drawing = new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.Black, 1), geo);
image1.Source = new DrawingImage(drawing);

上面的代码会在名为Image的{​​{1}}控件上向下/向右绘制一个箭头。