Drawing Directed Lines (with arrow endings) I am working on a simple graph viewer with OpenGL. My problem with is directed edges. I tried to draw arrow endings with GL_TRIANGLES but somehow my calculations are off. I've used direction cosine vector to calculate triangle position.

Actually triangle positions are almost correct but angles are a bit off for some triangles. Here is a screenshot:



and here is my code to draw arrow endings:

foreach (Edge e in m_graph.EdgeList.Values)
{
    if (e.Source == e.Target)
    {
        //TO DO: loops
    }
    if (e.Directed)
    {
        GL.Begin(PrimitiveType.Triangles);

        Vector2 p1, p2;
        e.Source.GetAttribute("pos", out p1);
        e.Target.GetAttribute("pos", out p2);

        Vector2 direction = p2 - p1;
        direction.Normalize();

        Vector2 perpendicular = (Vector2)direction.Clone();
        perpendicular.Perpendicu();

        Vector2 c_pt = p2 - direction * VERTICLE_SIZE * 3;
        Vector2 pt1 = c_pt - perpendicular * VERTICLE_SIZE;
        Vector2 pt2 = c_pt + perpendicular * VERTICLE_SIZE;

        GL.Vertex2(p2.X, p2.Y);
        GL.Vertex2(pt1.X, pt1.Y);
        GL.Vertex2(pt2.X, pt2.Y);

        GL.End();
    }
}


also Normalize and Perpendicu methods from Vector2 class:

    /// &lt;summary&gt;
    /// Normalizes the vector
    /// &lt;/summary&gt;
    public void Normalize()
    {
        m_x /= this.Length;
        m_y /= this.Length;
    }

    /// &lt;summary&gt;
    /// Perpedicus the vector
    /// &lt;/summary&gt;
    public void Perpendicu()
    {
        float _x = m_x;
        m_x = -m_y;
        m_y = _x;
    }
I don't see anything wrong with your code except for possibly the normalization of the direction. If the length of the vector gets updated automatically, it will change after the division of the x coordinate by the length. What you probably want is

public void Normalize()
{
    float len = this.Length;
    m_x /= len;
    m_y /= len;
}


instead. This would also explain why the arrow endings look more off for horizontal directions than for vertical directions. If the x component of the direction is small, then a division of m_x by the length only leads to a small change in the length of the direction. (Try to change the order of the two divisions in your Normalize function; then it should be the other way around.)