views:

164

answers:

1

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.

+3  A: 

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