omega
omega

Reputation: 43833

Python program to rotate a line not working

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 code below, but its not working properly. Does anyone see whats wrong?

Thanks

        ra = 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))

Upvotes: 0

Views: 297

Answers (1)

Taro Sato
Taro Sato

Reputation: 1452

First of all, you need to convert degrees into radians (= degree * (pi / 180)) before passing to math.cos, math.sin, etc.

Suppose the normalized direction vector of the arrow is given by

enter image description here

The unit vector parallel to the z-axis is given by

enter image description here

The orthonormal vector perpendicular to both the unit vectors are given by the cross product

enter image description here

and the angle between the two vector is given by the scalar product

enter image description here

Basically the rotation is about this u (uhat) axis by angle theta. The corresponding rotation matrix is given by

enter image description here

so this is the matrix you need to multiply the arrowhead vectors with.

Upvotes: 0

Related Questions