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