<p>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.</p>

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

<p><a href="https://i.sstatic.net/NjBhk.pngm" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NjBhk.pngm" alt="screenshot"></a></p>

<p>and here is my code to draw arrow endings:</p>

<pre><code>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();
    }
}
</code></pre>

<p>also Normalize and Perpendicu methods from Vector2 class:</p>

<pre><code>    /// &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;
    }
</code></pre>

<opengl><line-drawing><2d-graphics>
Drawing Directed Lines (with arrow endings)
Shine
# Drawing Directed Lines (with arrow endings)

(Asked by: username_0 on 2018-11-18)

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:
    /// 
    /// Normalizes the vector
    /// 
    public void Normalize()
    {
        m_x /= this.Length;
        m_y /= this.Length;
    }

    /// 
    /// Perpedicus the vector
    /// 
    public void Perpendicu()
    {
        float _x = m_x;
        m_x = -m_y;
        m_y = _x;
    }

**Tags:** opengl, line-drawing, 2d-graphics

---

## 0 answers

