Search code examples
iphoneopengl-es

Draw an arrow in opengl ES


1) I know the start point of the arrow and I know the endpoint.

I have no idea how to draw the head of the arrow.. I'm assuming that the other two points of the head are 45 degree angles away from the end point...

Anyone know of the formulas needed to do this?


Solution

  • Wade gives a good answer for the OpenGL portion of this question; I'll try to answer the vector math portion. Here's some pseudocode off the top of my head:

    Triangle GenerateArrowHead(vec2 p1, vec2 p2)
    {
        // Compute the vector along the arrow direction
        vec2 v = Normalize(p2 - p1)
    
        // Compute two perpendicular vectors to v
        vec2 vPerp1 = vec2(-v.y, v.x)
        vec2 vPerp2 = vec2(v.y, -v.x)
    
        // Compute two half-way vectors
        vec2 v1 = Normalize(v + vPerp1)
        vec2 v2 = Normalize(v + vPerp2)
    
        Triangle tri;
        tri.a = p2;
        tri.b = p2 + ArrowHeadSize * v1;
        tri.c = p2 + ArrowHeadSize * v2;
        return tri;
    }