Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have arrow symbol on a line in C#/WPF?

Tags:

wpf

I am new to graphics in C#/WPF application.

I am having a WPF application and using Canvas for drawing various object at the runtime with the help of mouse. I am facing problem in drawing a line with arrow (like below):

(A) ------------>---- (B)

In this arrow sign should be at the 3rd part of the line (and should always point towards the moving mouse). For example if I click mouse at point "A" and move towards point "B" then arrow sign should point towards "B" as shown above.

Any help will be highly appreciated.

Best Regards, Moon

like image 383
Moon Avatar asked Dec 05 '22 23:12

Moon


1 Answers

Thanks for all your help and support. I have resolved my problem as below::

private static Shape DrawLinkArrow(Point p1, Point p2)
{
    GeometryGroup lineGroup = new GeometryGroup();
    double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;

    PathGeometry pathGeometry = new PathGeometry();
    PathFigure pathFigure = new PathFigure();
    Point p = new Point(p1.X + ((p2.X - p1.X) / 1.35), p1.Y + ((p2.Y - p1.Y) / 1.35));
    pathFigure.StartPoint = p;

    Point lpoint = new Point(p.X + 6, p.Y + 15);
    Point rpoint = new Point(p.X - 6, p.Y + 15);
    LineSegment seg1 = new LineSegment();
    seg1.Point = lpoint;
    pathFigure.Segments.Add(seg1);

    LineSegment seg2 = new LineSegment();
    seg2.Point = rpoint;
    pathFigure.Segments.Add(seg2);

    LineSegment seg3 = new LineSegment();
    seg3.Point = p;
    pathFigure.Segments.Add(seg3);

    pathGeometry.Figures.Add(pathFigure);
    RotateTransform transform = new RotateTransform();
    transform.Angle = theta + 90;
    transform.CenterX = p.X;
    transform.CenterY = p.Y;
    pathGeometry.Transform = transform;
    lineGroup.Children.Add(pathGeometry);

    LineGeometry connectorGeometry = new LineGeometry();
    connectorGeometry.StartPoint = p1;
    connectorGeometry.EndPoint = p2;
    lineGroup.Children.Add(connectorGeometry);
    System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
    path.Data = lineGroup;
    path.StrokeThickness = 2;
    path.Stroke = path.Fill = Brushes.Black;

    return path;
}

Thanks,

Moon

like image 76
Moon Avatar answered Jan 12 '23 01:01

Moon