0

i need to draw an arrow representing the rotation of a circle in WPF. The best i got so far is this one. But it seems that the arrowhead is not correctly aligned with the angle of the arrowarc.

private void InternalDrawArrowGeometry(StreamGeometryContext context)
    {
        var angleWidth = 45;
        var startAngle = Rotation + 90;
        var endAngle = Rotation + angleWidth + 90;

        var xEnd = Radius * Math.Cos(startAngle * Math.PI / 180.0);
        var yEnd = Radius * Math.Sin(startAngle * Math.PI / 180.0);

        var xStart = Radius * Math.Cos(endAngle * Math.PI / 180.0);
        var yStart = Radius * Math.Sin(endAngle * Math.PI / 180.0);

        var b = angleWidth * Math.PI/180;

        var pt1 = new Point(CentreX + xStart, CentreY - yStart);
        var pt2 = new Point(CentreX + xEnd, CentreY - yEnd);
        var len2 = 1;
        const int angle = 45;

        var pt3 = new Point(
            pt2.X + (len2 / b) * ((pt1.X - pt2.X) * Math.Cos(angle) + (pt1.Y - pt2.Y) * Math.Sin(angle)),
            pt2.Y + (len2 / b) * ((pt1.Y - pt2.Y) * Math.Cos(angle) - (pt1.X - pt2.X) * Math.Sin(angle)));

        var pt4 = new Point(
            pt2.X + (len2 / b) * ((pt1.X - pt2.X) * Math.Cos(angle) - (pt1.Y - pt2.Y) * Math.Sin(angle)),
            pt2.Y + (len2 / b) * ((pt1.Y - pt2.Y) * Math.Cos(angle) + (pt1.X - pt2.X) * Math.Sin(angle)));

        context.BeginFigure(pt1,
            false,   // Filled
            false);  // Closed
        context.ArcTo(pt2,
            new Size(Radius, Radius),
            0.0,     // rotationAngle
            startAngle - endAngle > 180,   // greater than 180 deg?
            SweepDirection.Clockwise,
            true,    // isStroked
            false);
        context.LineTo(pt3, true, false);
        context.LineTo(pt2, true, false);
        context.LineTo(pt4, true, false);
    }

enter image description here

Has anyone coded something like this correctly and give me the code or can tell me whats wrong with my code?

Semkado
  • 25
  • 1
  • 5
  • It's not entirely clear how you're getting the angle from pt2 to pt3, and from pt2 to pt4. I think you need to be taking the angle of the radius at pt2, and then adding/subtracting a certain amount to get the angles from pt2 and pt3 (which should be 45 degrees apart) – canton7 Mar 19 '21 at 16:51
  • 4
    You may perhaps better draw the shape at some fixed angle, e.g. 12 o'clock, and then rotate it around the center point by an appropriate RotateTransform. – Clemens Mar 19 '21 at 17:00
  • see [Apply Rotation to Cylinder based on Tube Ending Normal](https://stackoverflow.com/a/39674497/2521214) Its 3D but maybe you could port it to your 2D case... – Spektre Mar 20 '21 at 09:20

0 Answers0