I am trying to figure out direction vectors of the arrowheads of an arrow. Basically I'm given a normalized direction vector (u,v,w) and I need the normalized direction vectors of the its two arrow heads which make a 15 degree angle.

My plan is to first start off with a simple normalized vector (0,0,1). The direction vectors of its arrow heads are (-sin(15), 0, -cos(15)) and (sin(15), 0, -cos(15)), and then rotate (0,0,1) so its parallel to the given (u,v,w). I do this by projecting (u,v,w) on its x-axis, and getting its angle relative to (0,0,1), then projecting on the y-axis, and getting its angle relative to (0,0,1), then I use the 3d rotation matrices to use those found angles to rotate the arrow head direction vector.

I have this pythoncode below, but its not working properly. Does anyone see whats wrong?


        ra = math.radians(15) 
        ca = math.cos(ra)
        sa = math.sin(ra)

        px = (0,v,w)
        if u!=1:
            px = [i/float(math.sqrt(v**2 + w**2)) for i in px]

        py = (u,0,w)
        if v!=1:
            py = [i/float(math.sqrt(u**2 + w**2)) for i in py]

        pxangle = math.acos(px[2])
        pyangle = math.acos(py[2])

        cpx = math.cos(pxangle)
        spx = math.sin(pxangle)
        cpy = math.cos(pyangle)
        spy = math.sin(pyangle)

        def rotatefunction(ah):
            xr = (ah[0], -spx*ah[2], cpx*ah[2])
            return (cpy*xr[0]+spy*xr[2], xr[1], -spy*xr[0]+cpy*xr[2]) 

        lah = rotatefunction((-sa, 0, -ca))
        rah = rotatefunction((sa, 0, -ca))
  • 1
    \$\begingroup\$ Did you know that you can use a rotation matrix to rotate arbitrary points? \$\endgroup\$ – Panda Pajama Apr 17 '14 at 2:19
  • \$\begingroup\$ I saw that but don't know how to use it in my case... \$\endgroup\$ – omega Apr 17 '14 at 2:41
  • \$\begingroup\$ If you represent a point in space as a vector, and you multiply it by a rotation matrix, the result is a vector which represents the point rotated around the origin. There should be plenty of matrix transform tutorials online, you could check out some of them. \$\endgroup\$ – Panda Pajama Apr 17 '14 at 2:52
  • \$\begingroup\$ I did, but couldn't find one that shows how to rotate a vector to create the arrow heads that I'm looking for. I think those assumes you rotate on (0,0,0). \$\endgroup\$ – omega Apr 17 '14 at 2:54
  • 2
    \$\begingroup\$ A rotation matrix will rotate a point around the origin. If you wish to rotate around another point, you can use a translation matrix to move the point so it rotates around the origin, use a rotation matrix to rotate it, and then use another translation matrix to get the point back to where it was. Those three transforms (translation->rotation->translation) can be multiplied (matrix multiplication is associative), and therefore you will have only one matrix you can use for all points. It may seem complicated, but it is much more simple than analytically rotating your points using trigonometry. \$\endgroup\$ – Panda Pajama Apr 17 '14 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.