旋转几何路径

Nas*_*aer 3 wpf graphics geometry path

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

Isa*_*avo 5

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

在您绘制上下文后,只需在原始 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);
Run Code Online (Sandbox Code Playgroud)

上面的代码将在Image名为的控件上绘制一个向下/向右的箭头image1