Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate a Geometry Path

I'm working newly with a Streamgeometry to draw a simple arrow. Now I need to turn the arrow to a specified angle. But how to rotate this geometry?

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)
like image 404
Nasenbaer Avatar asked Jul 20 '26 02:07

Nasenbaer


1 Answers

If the rotation is only for presentation (i.e. you don't care that the original geometry data is still an arrow pointing in the original direction) then you can apply a transform to it.

After you've drawn on your context, just apply the transform on the original StreamGeometry object (code in C# but it applies to VB.NET too):

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);

The above code will draw an arrow pointing down/right on an Image control named image1.

like image 97
Isak Savo Avatar answered Jul 22 '26 04:07

Isak Savo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!