Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw arrow on line algorithm

Does anyone have an algorithm for drawing an arrow in the middle of a given line. I have searched for google but haven't found any good implementation.

P.S. I really don't mind the language, but it would be great if it was Java, since it is the language I am using for this.

Thanks in advance.

like image 327
nunos Avatar asked Jun 09 '10 23:06

nunos


People also ask

How do you put arrows on a line?

On the “Insert” tab on the Ribbon, click the “Shapes” button. In the Lines group on the drop-down menu, click the “Line Arrow” choice. A crosshair sign will display. Press and hold your mouse button, then drag to draw the arrow.


1 Answers

Here's a function to draw an arrow with its head at a point p. You would set this to the midpoint of your line. dx and dy are the line direction, which is given by (x1 - x0, y1 - y0). This will give an arrow that is scaled to the line length. Normalize this direction if you want the arrow to always be the same size.

private static void DrawArrow(Graphics g, Pen pen, Point p, float dx, float dy)
{
    const double cos = 0.866;
    const double sin = 0.500;
    PointF end1 = new PointF(
        (float)(p.X + (dx * cos + dy * -sin)),
        (float)(p.Y + (dx * sin + dy * cos)));
    PointF end2 = new PointF(
        (float)(p.X + (dx * cos + dy * sin)),
        (float)(p.Y + (dx * -sin + dy * cos)));
    g.DrawLine(pen, p, end1);
    g.DrawLine(pen, p, end2);
}
like image 169
bbudge Avatar answered Sep 20 '22 17:09

bbudge